Convert decimal values to hex and put in a string

Discussion in 'C++' started by Angus, Nov 20, 2007.

  1. Angus

    Angus Guest

    Hello

    I have an unsigned char[14] and I want to convert the individual array
    values into hex and copy to a std::string.

    The array does not always hold 14 values - but I do know the length of
    the data - obviously up to 14 chars.

    An example is an array of six items with decimal values: 0, 10, 228,
    164, 72, 11. I want to convert these values to hex and copy to a
    string. Eg 000AE4A4480B - which the astute might recognise as a MAC
    address.

    I tried:

    unsigned char cval = 10; // A
    std::strstream str;
    str << std::hex << cval << std::endl;
    std::cout << str.str;

    But that outputs 1 for some reason.

    As does this:
    unsigned char cvals[6];
    // populate values
    str << std::hex << cvals << std::endl;
    std::cout << str.str;

    also outputs 1 !

    How should I be doing this?
     
    Angus, Nov 20, 2007
    #1
    1. Advertisements

  2. Angus

    anon Guest

    Your examples do not compile for me, therefore I have no idea how you
    made them output whatever you got there.
     
    anon, Nov 20, 2007
    #2
    1. Advertisements

  3. Angus

    Angus Guest

    I meant a std::eek:stringstream.

    I am now doing this:

    std::eek:stringstream strm;
    for(int nMAC = 0; nMAC < MACLength; nMAC++)
    {
    strm << std::hex << std::setw(2) << std::setfill('0')
    << static_cast<int>(byarrMAC[nMAC]);
    }

    m_strMAC = strm.str();
    //convert to uppercase
    std::transform(m_strMAC.begin(), m_strMAC.end(), m_strMAC.begin(),
    toupper);

    And it works.
     
    Angus, Nov 20, 2007
    #3
  4. Angus

    anon Guest

    Sorry, it still does not compile for me.
    Check this link:
    http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
     
    anon, Nov 20, 2007
    #4
  5. Angus

    David Harmon Guest

    On Tue, 20 Nov 2007 02:35:03 -0800 (PST) in comp.lang.c++, Angus
    All of the char based stream formatting operators assume that you want
    to see the character. To see the number, cast it to an int first.

    str << std::hex << (int)cval << '\n';

    Don't abuse endl by using it where its special behavior is not needed.
     
    David Harmon, Nov 20, 2007
    #5
  6. Angus

    Angus Guest

    What is wrong with using endl? Most C++ texts use it.
     
    Angus, Nov 20, 2007
    #6
  7. * Angus:
    Nothing wrong.

    On the contrary it's practical to use endl as default, to get into the
    habit. Helps to ensure that output generally has been flushed if
    something bad happens, and is perhaps easier to see than '\n'.

    If a profiler shows that those flushes somehow affect performance very
    adversely, then consider using just "\n", but in that case, it might be
    a good idea to consider something else than standard library iostreams.


    Cheers, & hth.,

    - Alf
     
    Alf P. Steinbach, Nov 20, 2007
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.