Getting sizeof(an int)

in ascii character code line feed (LF) is 10 in decimal or 'a' in hex.
if I have

int C;
C = 10;

how can I get sizeof(C) but I need C to be the hex value and not decimal.
in ascii character code line feed (LF) is 10 in decimal or 'a' in hex.

Not sure if I understand your question:
hex value is a character ???

Ok, in this case
1
2
3
4
5
6
int C;
C = 10;
int nSize = sizeof(C); //Always correct
printf("%c",C); //You need this ?
unsigned int hex = C; //DWORD 32-bit
printf("%X",hex); //You need this ? 


Or anything else ???
(Please, more information.)
Last edited on
@Jackson Marie (9)

You have done 9 posts so far, and already I suspect you are trolling.
sizeof(C) is the same as sizeof(int).

Whether the value you assigned to it is decimal or hex is not relevant.

As far as trolling, I'm pretty sure this is our former troll "dadabe", although he hasn't waxed philosophical yet.
I search the web for the meaning of troll and it says, a troll is someone who posts inflammatory off topic messages in the net.

I can assure you that is not my intention, I am just a student at UCSD who didn't quite understand my professor's lecture and I thought the FORUM is my next best source of correct information coming from the experienced programmers.

And when I post a topic sometimes I declare in the beginning that it is a homework and If you feel like not giving me a hint on my homework that I am OK with it.

Back to my question:
Anyway maybe I have to rephrase my question. can I declare an int variable and it force it to store hex even if I inputted a decimal.

I know I could do this, cin >> hex >> X andwhen I enter 30 it saves it as decimal 48
but can I do the reverse, I enter a number and it stores it as hex.
@mendozae

My comment was directed to @Jackson Marie (9) hence the @. I am sure cire meant the same . I did this because I believe this person was polluting you thread with bad advice / provocative comments. So just trying to help you out.

So back to your question - the computer stores numbers in binary - did you mean output a number in hex?

http://www.cplusplus.com/reference/iostream/manipulators/hex/


Hope all goes well.
@mendozae:
The comments about trolling concerned Jackson Marie, not yourself.

Numbers are not stored as hex or decimal. In fact, they're (literally) stored as binary, as one might expect of a computer. It's probably more useful to think of numbers stored as a value that is independent of any representation. For instance, I have the same number of fingers on my hands whether I choose to represent the value in hexadecimal, decimal or binary form - so too will the bit pattern of a number be the same whether you consider it in hexadecimal, decimal, or binary.
Last edited on
I appreciate all the hints, now I am able to go on the right direction and finished my assignment as shown below.Originally I thought if I read a character in a file I can store it in 4 different formats, decimal, hex,octal and binary. With this in mind I planned to manipulate the character to satisfy the specification and cout it. obviously I am going on the wrong direction and bottom line I hit a brick wall.

After reading your hints, I regrouped, abandoned my original idea, followed your hints and was able to go on the right direction. This is a good day because I learned something very important.

I hope you do not get tired answering my topic because, I have more coming.
FYI. If not for this FORUM I would still be in beginning C++ and from the bottom of my heart, I would like to say that I deeply appreciate all the hints that you have provided.those helped a lot.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream> 
using namespace std;

//constant declaration
const int MAX_CHAR_PER_LINE = 16;
const int SINGLE_SPACE = 1;
const int DOUBLE_SPACE = 2;
const int HIGHEST_SINGLE_CHAR_HEX = 0x0f;

//Display the value of each character in the file in hexadecimal format
void ListHex(ifstream &inFile)
{
    //local variable declaration
    int charFromFile, charPerLine = 0; 
    //converts all the character in its hex form and output it with everything align perfectly
    while (inFile.good())      
    { 
        if ((charFromFile = inFile.get()) == EOF) 
            break; 
        //display 16 hex per line
        if (charPerLine++ == MAX_CHAR_PER_LINE) 
        {
            cout << '\n';
            charPerLine = 1;
        }
        // if the hex is 1 character long, pad it with 1 zero and a space in front
        // if the hex is 2 characters long, pad it with a space in front
        cout << setfill(' ');
        if (charFromFile <= HIGHEST_SINGLE_CHAR_HEX)
            cout << setw(SINGLE_SPACE + 1) << '0' << charFromFile;
        else
            cout << setw(DOUBLE_SPACE + 1) << hex << charFromFile;
    }
    return;
}
Topic archived. No new replies allowed.