473,545 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ 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::stringstre am 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 4396

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::stringstre am 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(bu ffer, "%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************ **********@b68g 2000cwa.googleg roups.com>,
mi********@gmai l.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::stringstre am ss;
std::string s;

ss << std::hex << std::setfill('0 ')
<< std::setw(8) << std::setprecisi on(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************ **********@b68g 2000cwa.googleg roups.com>,
mi********@gmai l.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::stringstre am ss;
std::string s;

ss << std::hex << std::setfill('0 ')
<< std::setw(8) << std::setprecisi on(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::stringstre am ss;
std::string s;

ss << std::hex << std::setfill('0 ')
<< std::setw(8) << std::setprecisi on(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::setprecisi on(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.vide otron.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
5213
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 forename from my combo box I need to add the corresponding surname into the blank list box.What is the best way to do this? hope this make sense TIA
2
2523
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 printf) If the number is small say, 9 then it has to be displayed as 009, 56 -> 056, 897-> 897, 6786 -> xxx
2
2370
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, , mrubid
6
7595
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, Karthi
9
3100
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 be inserted into a standard web address in the table (the filed name is link) in ddw1 Example address ---
8
11361
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 tie down software to each PC on a network. I have just realised that the unique identity that I need to go for on the network and should be easy to...
1
3042
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 a Visual Basic 2005 solution that manipulates strings. It will parse a string containing a list of items within a text box and put the individual...
8
1325
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 an exception stating the constructor of the generic class does not exist. So Is there a way to obtain the resulting classes of the generics we use so...
3
3251
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 together the IDs of Clients from a multi-select listbox in an unbound text field, txtCriteria, on a form that is used to pick different reports. It...
0
7499
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
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...
0
7943
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6022
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...
0
5076
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...
0
3490
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
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 we have to send another system
0
743
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...

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.