Connecting Tech Pros Worldwide Help | Site Map

Dec to Hex

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:48 AM
Josh Parker
Guest
 
Posts: n/a
Default Dec to Hex

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, 06:48 AM
Mike Wahler
Guest
 
Posts: n/a
Default 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, 06:48 AM
David Rubin
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.