473,480 Members | 4,852 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Obtain padded string containing number in hex form

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

Jun 24 '06 #1
7 4389

Eric Lilja wrote:
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.


I solved it using sprintf. This program shows how:
#include <cstdio>
#include <iostream>

int
main()
{
unsigned long l = 0x0a87d7d4;
char buffer[9];

// %[flags][width][.precision][modifiers]type
// type = lx = Unsigned long hexadecimal integer
// flags =none
// width = 08

std::sprintf(buffer, "%08lx", l);

std::cout << buffer << std::endl;

return 0;
}

Output:
$ ./conv.exe
0a87d7d4

But I still would like to know how to solve it using streams too, for
learning purposes.

/ E

Jun 24 '06 #2
In article <11**********************@b68g2000cwa.googlegroups .com>,
mi********@gmail.com says...
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):


#include <iomanip>
#include <sstream>
#include <iostream>

int main() {

unsigned crc32 = 0xa87d7d4;
std::stringstream ss;
std::string s;

ss << std::hex << std::setfill('0')
<< std::setw(8) << std::setprecision(8)
<< crc32;

// _obviously_ better than '%8.8x'

s = ss.str();
std::cout << s << std::endl;
return 0;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 24 '06 #3

Jerry Coffin wrote:
In article <11**********************@b68g2000cwa.googlegroups .com>,
mi********@gmail.com says...
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):


#include <iomanip>
#include <sstream>
#include <iostream>

int main() {

unsigned crc32 = 0xa87d7d4;
std::stringstream ss;
std::string s;

ss << std::hex << std::setfill('0')
<< std::setw(8) << std::setprecision(8)
<< crc32;

// _obviously_ better than '%8.8x'

s = ss.str();
std::cout << s << std::endl;
return 0;
}


Seems to work just fine, thanks Jerry! Now I know how to do it using
both C++ streams and C style.

/ E

Jun 24 '06 #4
ax
On Sat, 24 Jun 2006 09:30:40 -0600, Jerry Coffin <jc*****@taeus.com>
wrote:
#include <iomanip>
#include <sstream>
#include <iostream>

int main() {

unsigned crc32 = 0xa87d7d4;
std::stringstream ss;
std::string s;

ss << std::hex << std::setfill('0')
<< std::setw(8) << std::setprecision(8)
<< crc32;
don't will be better:?

cout << (ss << fmt("0x%8x", '0') << crc32);

// _obviously_ better than '%8.8x'

s = ss.str();
std::cout << s << std::endl;
return 0;
}

Jun 25 '06 #5
In article <4k********************************@4ax.com>, aa@aa.aaa
says...
On Sat, 24 Jun 2006 09:30:40 -0600, Jerry Coffin <jc*****@taeus.com>


[ ... ]
ss << std::hex << std::setfill('0')
<< std::setw(8) << std::setprecision(8)
<< crc32;


don't will be better:?

cout << (ss << fmt("0x%8x", '0') << crc32);


If you have the 'fmt' function available, and it does what it looks
like it would above, it may be quite useful. But be aware that it's
not part of the standard library, nor even part of TR1, so it's not
what you'd usually think of as particularly portable.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 25 '06 #6
If you have the 'fmt' function available, and it does what it looks
like it would above, it may be quite useful. But be aware that it's
not part of the standard library, nor even part of TR1, so it's not
what you'd usually think of as particularly portable.


Is boost::format part of TR1? At any rate, it may be a useful
alternative here.
Jun 25 '06 #7
In article <%f****************@weber.videotron.net>, sp**@flarn2.com
says...

[ ... ]
Is boost::format part of TR1?
I'd have to re-check to be absolutely sure, but I don't think so.
At any rate, it may be a useful alternative here.


Quite true -- there are quite a few alternatives, but I don't believe
any of them has been standardized.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 25 '06 #8

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

Similar topics

12
5210
by: Kin®sole | last post by:
Hi I'm very new to VB (using VB6) I have two lists one blank and one containing names in the format of surname and then forename.I also have a combo box containing forenames.When I select a...
2
2519
by: Chandra Mohan | last post by:
Getting 0 padded values in the columns. Hi All, I have a requirement to convert a integer to string and display it in Sql server with fixed length say 3 chars. (in c, we wud use %03d in...
2
2367
by: Bob Quintal | last post by:
Given the requirement of writing data to a binary file padded with null characters to the field boundaries, is there a better way than With rsMembres Do Until .EOF mrubid = !rubID Put #2, ,...
6
7591
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
9
3094
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
8
11349
by: Paul Bromley | last post by:
Thanks for your tolerance on this list. I asked the question regarding Commercial Copy Protection along with Unique PC Idnetifier and obtaining the active IP address. This was all to identify and...
1
3034
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
8
1322
by: ThunderMusic | last post by:
Hi, We need to serialize (binary) objects containing generic collections. The thing is, when we try to get the objects back (deserialize) with a different instance of the application, we receive...
3
3239
by: 6afraidbecause789 | last post by:
If able, can someone please help make a Where clause that strings together IDs in a multi-select listbox AND includes a date range. I wasn’t thinking when I used the code below that strings...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7041
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,...
1
6736
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
6908
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...
1
4772
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...
0
4478
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...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
178
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...

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.