E
Eric Lilja
Hello, I have an unsigned long that I need to convert to a std::string.
The unsigned long holds 32-bit checksums and sometimes the most
significant byte is 0 and in those cases the string should be zero
padded (it should always contain 8 chars) and it should display its
value in hex form without 0x in the beginning. So if the unsigned long
holds the value 0xa87d7d4 the string should contain "0a87d7d4".
I tried a combination of iomanip and stringstreams but this code
snippet still yields a string containing a decimal number (and I'm not
sure my 0-padding is working either):
std::string s;
std::stringstream ss;
ss << crc32; // crc32 is of type unsigned long
ss >> std::setw(8) >> std::hex >> s;
I guess I could read the checksum four bits at a time converting each
bit group into the corresponding hexadecimal number and gradually build
the string, but I wanted to ask you experts if there was a neater way
involving the standard library first.
/ E
The unsigned long holds 32-bit checksums and sometimes the most
significant byte is 0 and in those cases the string should be zero
padded (it should always contain 8 chars) and it should display its
value in hex form without 0x in the beginning. So if the unsigned long
holds the value 0xa87d7d4 the string should contain "0a87d7d4".
I tried a combination of iomanip and stringstreams but this code
snippet still yields a string containing a decimal number (and I'm not
sure my 0-padding is working either):
std::string s;
std::stringstream ss;
ss << crc32; // crc32 is of type unsigned long
ss >> std::setw(8) >> std::hex >> s;
I guess I could read the checksum four bits at a time converting each
bit group into the corresponding hexadecimal number and gradually build
the string, but I wanted to ask you experts if there was a neater way
involving the standard library first.
/ E