Convert decimal values to hex and put in a string

A

Angus

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?
 
A

anon

Angus said:
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?

Your examples do not compile for me, therefore I have no idea how you
made them output whatever you got there.
 
A

Angus

Angus said:
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.
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?

Your examples do not compile for me, therefore I have no idea how you
made them output whatever you got there.- Hide quoted text -

- Show quoted text -

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.
 
A

anon

Angus said:
Angus said:
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?
Your examples do not compile for me, therefore I have no idea how you
made them output whatever you got there.- Hide quoted text -

- Show quoted text -

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.

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
 
D

David Harmon

On Tue, 20 Nov 2007 02:35:03 -0800 (PST) in comp.lang.c++, Angus
str << std::hex << cval << std::endl;
std::cout << str.str;

But that outputs 1 for some reason.

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.
 
A

Angus

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.

What is wrong with using endl? Most C++ texts use it.
 
A

Alf P. Steinbach

* Angus:
What is wrong with using endl? Most C++ texts use it.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top