Hex char to string of text?

This is a discussion on Hex char to string of text? within the C++ Programming forums, part of the General Programming Boards category; I'm getting inoput from a binary file, in hex. And I want it to say the first nine characters... like ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    76

    Hex char to string of text?

    I'm getting inoput from a binary file, in hex.
    And I want it to say the first nine characters... like "0x05 0x53,0x84 " ... and so on. So how do I turn a char in hex to a string in text?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, it's a bit ugly, but:
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main() {
        char c = 0x05;
        std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << (int)c << std::endl;
        return 0;
    }
    There may be an easier way. Of course, you could always use the C functions for this.
    Code:
    printf("0x%02x\n", c);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    What about mutiple hex numbers?
    Like:
    char bob[3] = {0x82,0xF4,0xED};
    Last edited by azjherben; 08-27-2009 at 02:36 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, you just do it in a loop! Repeated executing
    Code:
    std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << (int)bob[x] << ' ';
    should do what you need.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    What if I can't use cout.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by azjherben View Post
    What if I can't use cout.
    Then you shouldn't have posted in the C++ forum. And the first reply had the C version too.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you need to use C++, but want to generate a std::string to use later, you can use stringstreams. See the FAQ for details.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21