Connecting Tech Pros Worldwide Help | Site Map

Dec to Hex

  #1  
Old July 22nd, 2005, 07:48 AM
Josh Parker
Guest
 
Posts: n/a
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
  #2  
Old July 22nd, 2005, 07:48 AM
Mike Wahler
Guest
 
Posts: n/a

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


  #3  
Old July 22nd, 2005, 07:48 AM
David Rubin
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
BCD List to HEX List Philippe Martin answers 67 August 3rd, 2006 09:45 AM
How to convert dec to hex and binary Soon Lee answers 1 November 22nd, 2005 06:28 PM
convert dec number to HEX and oct and bin without c library function whatluo answers 7 November 14th, 2005 11:36 PM
How to convert dec to hex and binary Soon Lee answers 1 July 21st, 2005 10:21 PM