c++ - How do I put a space in between every four characters in this output? -
I am writing a program where I have to display binary representations of different data types. I need binary output for one place after every four numbers. For example: <0011 1111 1000 1110 1011 1000 0101 0010
Below is sample of a function that I am using to display binary code. What's the best way to format output with spaces?
Zero print room (four testers) {unsigned char mask = pow (2, ((shape) (four) * 8) - 1)); Cout & lt; & Lt; "Binary representation" & lt; & Lt; The tester & lt; & Lt; " is "; (Int count = 7; count & gt; = 0; count--) {If ((testChar and mask)! = 0) {cout & lt; & Lt; "1"; } And {cout & lt; & Lt; "0"; } Mask = (Masks> 1); } Cout & lt; & Lt; Endl; }
You are already being a counter, so you only use it To do the character you are on, for example:
if (count == 3) {cout & lt; & Lt; ""; } Just add this if before your if-else statement. This way, once you have outputed 4 characters, then count will be 3, so you should know that you have to produce a space Note: this value It's been that you're only output 8 characters at a time, because your code suggests.
Comments
Post a Comment