-7

I want to convert an unsigned char arrays content to a hexadecimal string representation.
Can anyone help with a function or a way out?

unsigned char codeslink[5] ={ 0x33, 0x74, 0x74, 0x73, 0x72};
std::string my_std_string(reinterpret_cast<const char *>(codeslink), 5);
std::string my_std_string((const char *)codeslinks);

I want to get a return values 3374747372 as char or string.

update: This is the function i came up with using @πάνταῥεῖ: answer i want to make it better and make it robust to handle unsigned char like 0x00,0x04,0x00,0x11 of various length size.

std::string Return_Uchar_To_String(unsigned char uns_char, size_t uns_char_len)
    {
        std::ostringstream oss;
        oss << std::hex << std::setw(2) << std::setfill('0');
        for(size_t i = 0; i < uns_char_len; ++i)
        {
            oss << (unsigned int)uns_char[i];
        }


        return oss.str();
    }
DavidoLLP
  • 45
  • 8
  • `std::string my_std_string = std::to_string(3374747372);`? – R_Kapp Mar 15 '16 at 16:46
  • That's not done through casting. – πάντα ῥεῖ Mar 15 '16 at 16:47
  • 2
    That is not going to work: `0x33 != "33"`. Otherwise this is a duplicate of: http://stackoverflow.com/questions/4691608/copying-non-null-terminated-unsigned-char-array-to-stdstring – NathanOliver Mar 15 '16 at 16:47
  • @R_Kapp `std::string my_std_string = std::to_string(codeslink);` dont understand how you mean? @πάντα ῥεῖ how is it best done? @NatanOliver that didnt work for me either – DavidoLLP Mar 15 '16 at 17:05
  • @R_Kapp's comment is nonsense, regarding hex values. – πάντα ῥεῖ Mar 15 '16 at 17:07
  • @DavidoLLP I wrote an answer for what you need. – πάντα ῥεῖ Mar 15 '16 at 17:08
  • @πάνταῥεῖ: He said he wanted a specific return value as a string; maybe I'm misunderstanding the question (most likely so), but just use `std::to_string` and make it one? Or better yet, `std::string my_std_string = "3374747372";`? – R_Kapp Mar 15 '16 at 17:08
  • If you have a new question, you should make a new question and ask it there (provided it meets the standards of this site). Don't ask new questions on an old question that already has an answer. – Weak to Enuma Elish Mar 15 '16 at 19:38

1 Answers1

1

I want to get a return values 3374747372 as char or string.

Casting doesn't work in this case.

You can use text formatting IO to get a hex string representation of the arrays content:

unsigned char codeslink[5] ={ 0x33, 0x74, 0x74, 0x73, 0x72};
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for(size_t i = 0; i < 5; ++i) {
    oss << std::setw(2) << (unsigned int)codeslink[i];
}
std::string result = oss.str();

Live Demo

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • kudos for editing the title of the post, i have posted a function i derived form it ot make it flexible for various unsigned char size can you look at the function and help me refactor it to be robust? – DavidoLLP Mar 15 '16 at 17:47
  • @DavidoLLP You seem to have a small mistake in your function signature: `std::string Return_Uchar_To_String(unsigned char uns_char, size_t uns_char_len)` should be `std::string Return_Uchar_To_String(const unsigned char* uns_char, size_t uns_char_len)`, Otherwise it looks OK and will be _robust_. – πάντα ῥεῖ Mar 15 '16 at 17:51
  • @ πάντα ῥεῖ thanks for the update i tried my function with this value `unsigned char codeslink[4] = { 0x00, 0x00, 0x02, 0x11 };` i got this `000211` instead of `00000211` function is ` Return_Uchar_To_String(codeslink, sizeof(codeslink));` similary with calls like `unsigned char codeslink[4] = { 0x10, 0x00, 0x02, 0x11 };` any thoughts, is that a bug? – DavidoLLP Mar 15 '16 at 18:00
  • @DavidoLLP Sorry, I forgot that `setw()` isn't sticky to the stream, but just changes the behavior for the next output field. – πάντα ῥεῖ Mar 15 '16 at 18:09
  • @ πάντα ῥεῖ no offense taken, you have being helpful thanks – DavidoLLP Mar 15 '16 at 18:12