473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Convert decimal values to hex and put in a string

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?

Nov 20 '07 #1
5 3602
Angus wrote:
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.
Nov 20 '07 #2
On Nov 20, 1:01 pm, anon <a...@no.nowrote:
Angus wrote:
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::ostringstream.

I am now doing this:

std::ostringstream 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.

Nov 20 '07 #3
Angus wrote:
On Nov 20, 1:01 pm, anon <a...@no.nowrote:
>Angus wrote:
>>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::ostringstream.

I am now doing this:

std::ostringstream 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-lit...t.html#faq-5.8
Nov 20 '07 #4
On Tue, 20 Nov 2007 02:35:03 -0800 (PST) in comp.lang.c++, Angus
<an*********@gmail.comwrote,
>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.
Nov 20 '07 #5
On Nov 20, 4:54 pm, David Harmon <sou...@netcom.comwrote:
On Tue, 20 Nov 2007 02:35:03 -0800 (PST) in comp.lang.c++, Angus
<anguscom...@gmail.comwrote,
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.
What is wrong with using endl? Most C++ texts use it.
Nov 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Bubba | last post by:
I know it's possible, just don't know how to do it. I have a spreadsheet that I imported into access. Two of the columns in the table have Hard Drive space values listed for example 2.45 GB and 453...
2
by: KB | last post by:
Hi guys, In my DataGrid I have a column that displays decimal values as currency ( I set the Data Formatting expression of that column to {0:C}). So the actual string displayed in the grid looks...
3
by: JenHu | last post by:
Hi all, I have to read from a text file and generate values and insert to database. But first of all, when the text file contains '0000000000', I received a sEfundAmt value = 0D instead of 0.0...
5
by: Ray | last post by:
I have a table with some audit date and time columns. Problem is the developer who stored the data left them as DECIMAL type instead of DATE and TIME. Is there a way I can convert the DECIMAL type...
10
by: satishrajana | last post by:
Hi, My SQL returns a NULL in a datefield if there is no date in that field. If there is a NULL in this column, I want to replace it with spaces in my SELECT statement when I am selecting these...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
1
by: karanbikash | last post by:
Hi , I have a requirement where in I need to display the decimal value of 0.00 as '######.00' SELECT case when DECIMAL(AMT_CHEQUE,31,2) = 0.00 THEN 9999999999999.9999999 ELSE ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.