Thread: Typecast from int to hexadecimal...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question Typecast from int to hexadecimal...

    I know how to type cast from int to char, but how would you do the same thing from int to hex, any ideas, thanks in advance...


  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It certainly wouldn't be typecasting. char and int are data types, and hex is not a data type. As far as C++ is concerned, hex is just a format in which to print numbers. Search the boards - there have been many discussions on the subject of going the other way, and these should give you some ideas.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but how would you do the same thing from int to hex
    Code:
    int dec = 27;
    int hex = dec;
    Any questions? The only difference between decimal and hexadecimal is how you represent the value. 27 and 1B are the same exact same value, they're just represented in a different way (such as when you print with the hex modifier).
    My best code is written with the delete key.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    ...what they said.


    If you want to output an int value in hex format you can do this:

    Code:
    #include <iostream>
    #include <iomanip>
    ...
    int value = 32;
    std::cout << std::hex << value << std::endl;
    Should output: 20
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Quote Originally Posted by hk_mp5kpdw
    ...what they said.


    If you want to output an int value in hex format you can do this:

    Code:
    #include <iostream>
    #include <iomanip>
    ...
    int value = 32;
    std::cout << std::hex << value << std::endl;
    Should output: 20
    and if you want to show the base output std::showbase to the stream so it would show 0x20

    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM