ABXZone Computer  Forums



Reply
 
Thread Tools Display Modes
Old 07-17-2008, 05:27 PM   #1
Pasch
Registered User
 
Pasch's Avatar
 
Join Date: Jun 2004
Posts: 427
c++ cout: don't trim "zero" bytes

Hi there,

I'm implementing SHA256 in C++, but I've got a tiny problem. I'm using cout to output the result of the hash operation in hex, but sometimes a "zero" byte has to be printed by cout. By "zero" byte I mean a byte that has 00 as its hexadecimal value. When a byte like that occurs, cout just prints a single 0, however I want it to print 00, not the shorthand single 0. Does anybody know of a way to force cout to do this?

I would be very happy with any help.

Regards.
(Offline)   Reply With Quote

Advertisement [Remove Advertisement]
Old 07-17-2008, 06:15 PM   #2
k0NG0
Remembering TQ
 
k0NG0's Avatar
 
Join Date: Mar 2001
Location: Sweden
Posts: 13,749
Re: c++ cout: don't trim "zero" bytes

Quick and dirty answer:
Code:
cout.setf(ios::hex, ios::basefield); //set to display stuff as hex
cout << setw(2) << setfill('0') << whatever << endl;
Requires you to include <iomanip>.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Use
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
-
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Feeling multicore elation? Remember this correlation:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
(Offline)   Reply With Quote
Old 07-17-2008, 06:29 PM   #3
k0NG0
Remembering TQ
 
k0NG0's Avatar
 
Join Date: Mar 2001
Location: Sweden
Posts: 13,749
Re: c++ cout: don't trim "zero" bytes

Code:
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
  unsigned char bytes[4] = {0, 1, 0x09, 0xfe};

  cout.setf(ios::hex, ios::basefield);
  for (int i = 0; i < 4; ++i) {
    cout << setw(2) << setfill('0') << (int)bytes[i] << endl;
  }
}
This yields:
00
01
09
fe
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Use
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
-
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Feeling multicore elation? Remember this correlation:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
(Offline)   Reply With Quote
Old 07-27-2008, 05:53 PM   #4
Pasch
Registered User
 
Pasch's Avatar
 
Join Date: Jun 2004
Posts: 427
Re: c++ cout: don't trim "zero" bytes

Quote:
Originally Posted by k0NG0 View Post
Code:
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
  unsigned char bytes[4] = {0, 1, 0x09, 0xfe};

  cout.setf(ios::hex, ios::basefield);
  for (int i = 0; i < 4; ++i) {
    cout << setw(2) << setfill('0') << (int)bytes[i] << endl;
  }
}
This yields:
00
01
09
fe
Thank you for your reply! I'm sorry for replying so late, for some reason e-mail notification didn't work. In the meantime I wrote myself a nice function that makes the hex strings for me, as I needed to return them as a string rather than print them to the screen in the eventual program, but I'll be sure to remember this for the future. The project worked out by the way, I've got all SHA versions up and running now and giving correct results. Thanks again for the reply!
(Offline)   Reply With Quote
Old 08-22-2016, 09:36 PM   #5
fvbounty
Registered User
 
fvbounty's Avatar
 
Join Date: Nov 2002
Location: Alameda Ca
Posts: 20,444
Re: c++ cout: don't trim "zero" bytes

Spam
__________________
Asus ROG RAMPAGE VI APEX, Intel I7 7820K Cooled by Noctua NH D15S, 32 Gigs Corsair Dominator Platinum CMD32GX4M4A2666C15, Asus GTX 980 TI Strix, Samsung 950 Pro 512 Gig NVMe SSD W10 PRO, Samsung 840 EVO 500 gig data, 1 TB WD Black data, Corsair 540 case with Corsair HXi750 PS, Dell UltraSharp U3415W 34" monitor, Ducky Shine 5 RGB Keyboard, Mionix Naos 7000 mouse, AudioEngine A5+ Speakers driven by Asus Xonar U7 MKII USB sound card, AKG 7XX Red Limited Edition Headphones ....and some other stuff!


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Offline)   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com

© 2006 - 2016 ABXZone Forums | About ABX Zone Forums | Advertisers | Investors | Legal | A member of the Crowdgather Forum Community