Connecting Tech Pros Worldwide Forums | Help | Site Map

Dec to Hex

Josh Parker
Guest
 
Posts: n/a
#1: Jul 22 '05
What is a good way for me to convert a Decimal to Hexadecimal. I need
the number to be stored in a variable, it will not be printed to the
screen. Any help that can be provided will help. Thank you

Mike Wahler
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Dec to Hex



"Josh Parker" <jcp1217@mail.ecu.edu> wrote in message
news:f42d5dca.0402231156.35005b4d@posting.google.c om...[color=blue]
> What is a good way for me to convert a Decimal to Hexadecimal. I need
> the number to be stored in a variable, it will not be printed to the
> screen. Any help that can be provided will help. Thank you[/color]

All values are stored in binary. "Decimal" and "Hexadecimal"
are *textual* representations which can be used for 'human
readable' input and output.

int i = 42; // stored 'internally' as 101010
std::cout << i << '\n'; // prints 42 (by default)
std::cout << std::hex << i << '\n'; // prints 2A


The following two statements have exactly the same effect:

int i = 42;
int i = 0x2A;

-Mike


David Rubin
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Dec to Hex


Josh Parker wrote:
[color=blue]
> What is a good way for me to convert a Decimal to Hexadecimal. I need
> the number to be stored in a variable, it will not be printed to the
> screen. Any help that can be provided will help. Thank you[/color]

All integer values are the same, regardless of the base. The only[1] time you
need to know the base is when you print (e.g., printf with %x) or convert (e.g.,
strtol with base 16). IOW, there is no difference between storing an integer as
a decimal (i.e., base 10) or hexadecimal value.

/david

[1] also when you read with scanf, but this is not recommended.

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Closed Thread