473,655 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3615
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.nowrot e:
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::ostringstr eam.

I am now doing this:

std::ostringstr eam 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.nowrot e:
>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::strstrea m 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::ostringstr eam.

I am now doing this:

std::ostringstr eam 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*********@gm ail.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...@gm ail.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
9741
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 numeric value according to an arbitrary regular expression.
2
15859
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 MB, you get the picture. Both the GB and MB assorted values exist in both columns. Is there anyway to convert the GB or MB to 0's, or even better yet decimal places so I can look for values < or > to 6gb. I already tried a query, but it seems...
2
10584
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 like $2,295.99. For each row I need to do some additional calculations based on this value, so I need to convert it back to decimal type. But I cannot find a way to get rid of the currency formatting and revert the value back to decimal. I tried...
3
12912
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 on sEfundAmt = Decimal.Parse(Mid$(sRetLine, 30, 10)) / 100.0 For example, in the text file, value is '0000150776', I want to
5
43845
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 to DATE or TIME? The column data is in the date form YYYYMMDD (i.e. 20060308 = March 8 2006). I want to get the data into a DATE type. I tried TO_DATE('20060308','YYYYMMDD') but I cannot get it to work. What else can I do to conver the data...
10
39002
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 records. Can any of you experts out there help me with a sample SQL.
10
4948
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
2210
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus (+) is also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `: the String deciding...
1
3706
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 DECIMAL(AMT_CHEQUE,31,2) END AS AMT_CHEQUE FROM DB2ADMIN.RPT_BA_PADC161
0
8296
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.