boost::format, print hex
Hi, With the following program I am trying to print the hex of "a". But it is not printing it correctly. I have gone through the boost format sysntax specification. But I dont have any clue, what am I doing wrong? char a=0x11; stringstream ss; ss<<boost::format("%|02X|")%a; cout<<ss.str()<<endl; Thanks a lot, Lloyd ______________________________________ Scanned and protected by Email scanner
With the following program I am trying to print the hex of "a". But it is not printing it correctly. I have gone through the boost format sysntax specification. But I dont have any clue, what am I doing wrong?
char a=0x11; stringstream ss; ss<<boost::format("%|02X|")%a; cout<<ss.str()<<endl;
Assuming your format string is correct (I haven't checked against the doc), try casting your char to int: (int)a
Thansk Igor, it works in that case. But it is not working in this case... char a[10]; a[0]=0xFA; stringstream ss; ss<<boost::format("%|02X|")%(int)(a[0]); cout<<ss.str()<<endl; It prints FFFFFFFA Thanks a lot, Lloyd ----- Original Message ----- From: "Igor R" <boost.lists@gmail.com> To: <boost-users@lists.boost.org> Sent: Friday, July 09, 2010 7:59 PM Subject: Re: [Boost-users] boost::format, print hex
With the following program I am trying to print the hex of "a". But it is not printing it correctly. I have gone through the boost format sysntax specification. But I dont have any clue, what am I doing wrong?
char a=0x11; stringstream ss; ss<<boost::format("%|02X|")%a; cout<<ss.str()<<endl;
Assuming your format string is correct (I haven't checked against the doc), try casting your char to int: (int)a _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
______________________________________ Scanned and protected by Email scanner
change to "unsigned char". You're getting bitten by sign extension. On 7/9/2010 9:57 AM, Lloyd wrote:
Thansk Igor, it works in that case. But it is not working in this case...
char a[10]; a[0]=0xFA; stringstream ss; ss<<boost::format("%|02X|")%(int)(a[0]); cout<<ss.str()<<endl;
It prints FFFFFFFA
changing int to simpe "unsigned char" did not work (It prints '0-'). I changed it to ss<<boost::format("%|02X|")%(int)(unsigned char)(a[0]); Now it works. But looks dirty, is it the right way? Thanks a lot, Lloyd ----- Original Message ----- From: "Eric J. Holtman" <eric@holtmans.com> To: <boost-users@lists.boost.org> Sent: Friday, July 09, 2010 8:32 PM Subject: Re: [Boost-users] boost::format, print hex
change to "unsigned char".
You're getting bitten by sign extension.
On 7/9/2010 9:57 AM, Lloyd wrote:
Thansk Igor, it works in that case. But it is not working in this case...
char a[10]; a[0]=0xFA; stringstream ss; ss<<boost::format("%|02X|")%(int)(a[0]); cout<<ss.str()<<endl;
It prints FFFFFFFA
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
______________________________________ Scanned and protected by Email scanner
On 7/9/2010 10:09 AM, Lloyd wrote:
changing int to simpe "unsigned char" did not work (It prints '0-'). I changed it to
ss<<boost::format("%|02X|")%(int)(unsigned char)(a[0]);
Now it works. But looks dirty, is it the right way?
Change the declaration of a from char a[10]; to unsigned char a[10];
participants (3)
-
Eric J. Holtman
-
Igor R
-
Lloyd