I’ve got a question here. I like to convert a file output result printed in binary into equivalent ASCII characters in C. For instance, 00000100 00000100 00000100 00000110 giving me [] [] [] []. The ASCII nonprintable characters usually give nothing or an equivalent Box [] character. An example of either side, binary to ASCII and vice versa conversion calculator is given at http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/Binary_To_Text.asp
Could this also, when say, the fourth box in the given example, when converted back to binary give me the different binary compared to its previous/neighboring box characters (the calculator maintains this property)?
The following is a function that I found which converts binary to ASCII:
[FONT=Calibri][COLOR=green][FONT=Courier New][SIZE=2]//This function turns a character array in binary at Source to an ASCII string at destination[/SIZE][/FONT][/COLOR][/FONT]
[FONT=Calibri][COLOR=green][FONT=Courier New][SIZE=2]//Destination must be (Source * 2 + 1) bytes long or you will get buffer overruns[/SIZE][/FONT][/COLOR]
[COLOR=green][FONT=Courier New][SIZE=2]//The destination buffer should be "char" instead of "unsigned char". I also swapped the high- and low-byte. [/SIZE][/FONT][/COLOR]
[SIZE=2][COLOR=blue][FONT=Courier New]void[/FONT][/COLOR][FONT=Courier New] Bin2Ascii([COLOR=blue]unsigned[/COLOR] [COLOR=blue]char[/COLOR] Source[], [COLOR=blue]int[/COLOR] SourceLength, [COLOR=blue]unsigned[/COLOR] [COLOR=blue]char[/COLOR] Destination[])[/FONT][/SIZE]
[FONT=Courier New][SIZE=2]{[/SIZE][/FONT]
[FONT=Courier New][SIZE=2]Destination[SourceLength * 2] = [COLOR=#a31515]'\0'[/COLOR];[/SIZE][/FONT]
[FONT=Courier New][SIZE=2][COLOR=blue]while[/COLOR](SourceLength != 0)[/SIZE][/FONT]
[FONT=Courier New][SIZE=2]{[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 2] = Source[SourceLength - 1];[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 1] = Source[SourceLength - 1];[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 2] &= 0x0f;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 1] &= 0xf0;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 1] >>= 0x4;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] [COLOR=blue]if[/COLOR](Destination[SourceLength * 2 - 2] <= 0x9)[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 2] |= 0x30;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] [COLOR=blue]else[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2] {[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 2] |= 0x60;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 2] -= 0x9;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] }[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] [COLOR=blue]if[/COLOR](Destination[SourceLength * 2 - 1] <= 0x9)[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 1] |= 0x30;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] [COLOR=blue]else[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=2] {[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 1] |= 0x60;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] Destination[SourceLength * 2 - 1] -= 0x9;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] }[/SIZE][/FONT]
[FONT=Courier New][SIZE=2] SourceLength--;[/SIZE][/FONT]
[FONT=Courier New][SIZE=2]}[/SIZE][/FONT]
[FONT=Courier New][SIZE=2]}[/SIZE][/FONT]
[/FONT]
The output file is the following which I generated from my data compression algorithm code. I would like to use the last two column results and select eight 0’s, e.g., 00 00 00 11 (colored in red) and then convert it into ASCII. In this case, by the above function, it should result in Null, which I prefer to have it in a unique box character for future use. Any help would be appreciated.
str[ 0] = 'H' 48 01001000;0000#1010;[COLOR=red]00[/COLOR]#[COLOR=red]00[/COLOR] str[ 1] = 'e' 65 01100101;0000#1111;[COLOR=red]00[/COLOR]#[COLOR=red]11[/COLOR] str[ 2] = 'l' 6C 01101100;0010#1110;00#10 str[ 3] = 'l' 6C 01101100;0010#1110;00#10 str[ 4] = 'o' 6F 01101111;0011#1111;01#11 str[ 5] = ' ' 20 00100000;0000#0100;00#10 str[ 6] = 'W' 57 01010111;0001#1111;01#11 str[ 7] = 'o' 6F 01101111;0011#1111;01#11 str[ 8] = 'r' 72 01110010;0100#1101;10#11 str[ 9] = 'l' 6C 01101100;0010#1110;00#10 str[10] = 'd' 64 01100100;0000#1110;00#10 str[11] = '!' 21 00100001;0000#0101;00#11 str[12] = ' ' 0A 00001010;0000#0011;00#11


Sign In
Create Account


Back to top









