const char* to hex char

Discussion in 'C++' started by Philliam Auriemma, Feb 2, 2010.

  1. Hey guys,

    Say I have a const char* that equals "10". How would I go about
    turning that into '\x10'? I've tried itoa and ssprintf or something,
    and have been at it for an entire day but I can't figure it out.
     
    Philliam Auriemma, Feb 2, 2010
    #1
    1. Advertising

  2. Philliam Auriemma

    Jerry Coffin Guest

    In article <904e9ffb-95dd-4324-b789-cf0fd0e1b5f8
    @d27g2000yqn.googlegroups.com>, says...
    >
    > Hey guys,
    >
    > Say I have a const char* that equals "10". How would I go about
    > turning that into '\x10'? I've tried itoa and ssprintf or something,
    > and have been at it for an entire day but I can't figure it out.


    This will convert the string to an unsigned long, assuming the input
    is in hexadecimal:

    unsigned long value = strtoul("10", NULL, 16);

    Printing that value back out in hexadecimal with a leading '0x' looks
    something like:

    std::cout << std::hex << std::showbase << value << "\n";

    --
    Later,
    Jerry.
     
    Jerry Coffin, Feb 2, 2010
    #2
    1. Advertising

  3. On Feb 1, 9:42 pm, Jerry Coffin <> wrote:
    > In article <904e9ffb-95dd-4324-b789-cf0fd0e1b5f8
    > @d27g2000yqn.googlegroups.com>, says...
    >
    >
    >
    > > Hey guys,

    >
    > > Say I have a const char* that equals "10". How would I go about
    > > turning that into '\x10'? I've tried itoa and ssprintf or something,
    > > and have been at it for an entire day but I can't figure it out.

    >
    > This will convert the string to an unsigned long, assuming the input
    > is in hexadecimal:
    >
    > unsigned long value = strtoul("10", NULL, 16);
    >
    > Printing that value back out in hexadecimal with a leading '0x' looks
    > something like:
    >
    > std::cout << std::hex << std::showbase << value << "\n";
    >
    > --
    >     Later,
    >     Jerry.


    WOW THANK YOU, this is a life saver. Perfect.
     
    Philliam Auriemma, Feb 2, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,223
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    473
    Juha Autero
    Aug 19, 2003
  3. Replies:
    24
    Views:
    839
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,062
    Ian Collins
    May 9, 2006
  5. Javier
    Replies:
    2
    Views:
    568
    James Kanze
    Sep 4, 2007
Loading...

Share This Page