Issue with string length

Hi,
I have a simple code which takes an integer, and for each byte of integer is concatenated into a string.

----------
unsigned long a = 0x1234567812345678;
unsigned char *a1;

std::stringstream ss;
a1 = new unsigned;
a1 = (unsigned char *)&a;

ss << a1;
std::cout << "String length=" << (ss.str()).length() << std::endl;
----------------

The program shows string length as 11. I was hoping to see as an 8, as variable "a" has been assigned a 64 bit value. When I pront the content of the string, the 1st 8B come out OK. But then it prints additional 3 bytes.

Any inputs to fix that?
you are treating "a" as a null terminated char string, which is not. operator<< will stop reading it as string the first time if finds a zero-byte.
To do this sort of things, use unformatted output ( http://www.cplusplus.com/reference/iostream/ostream/write/ )
Topic archived. No new replies allowed.