hex question

Started by
2 comments, last by kordova 21 years, 4 months ago
I''m playing around with binary, octal, hex and decimal numbers in c right now and had two questions. Is there a "0x" or "h" equivalent for a binary value being assigned to an integer? ex. int x = 0x20, y = 101b; ? Also, if I have an integer of any given value how can I assign that in another system to another variable ex. int dec = 10, hex=0xdec; ?
Advertisement
1. There is no way to set a variable by writing it in straight binary in C or C++.

2. I also am not sure if I understand your question. Do you want it to display as hex/dec/oct or do you mean you want to convert it from one base to another logically?

If this code:
int dec = 10, hex = 0xdec;

is supposed to set dec to 10 and hex to 0xA then you can just write instead:

int dec = 10, hex = dec;

because logically they are the same.


The hackers must have gotten into the system through the hyperlink!!
Invader''s Realm
Ok, thanks.

About the second, I meant assign the hex value in decimal format to hex. (If dec was 20 then hex would equal 32)? Thanks ahead.
You mean that if dec is 10 you want hex to be ''A'' and not 0xA? If so, then hex needs to be a string and not an int because 10 means the same thing in every base and is stored the same way on your computer no matter if it is 10, 0xA, or whatever. To assign hex the string representation of dec you''ll have to either write your own conversion function, use stringstreams (C++), or something else.


The hackers must have gotten into the system through the hyperlink!!
Invader''s Realm

This topic is closed to new replies.

Advertisement