Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Binary to ASCII Conversion from an Output File including Control Characters

binary ascii

  • Please log in to reply
2 replies to this topic

#1 UniBoy

UniBoy

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 22 March 2010 - 11:27 PM

Hi there,

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

  • 0

#2 bobdark

bobdark

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 155 posts

Posted 23 March 2010 - 01:33 AM

So what exactly is the question? You want to write a function, based on the one that you provided, that reads 0 and 1's from a file and converts them in appropriate way to ASCII characters and lets say stores it somewhere or prints to console?
  • 0

#3 UniBoy

UniBoy

    CC Newcomer

  • Just Joined
  • PipPip
  • 20 posts

Posted 23 March 2010 - 05:01 AM

So what exactly is the question? You want to write a function, based on the one that you provided, that reads 0 and 1's from a file and converts them in appropriate way to ASCII characters and lets say stores it somewhere or prints to console?


Yes, the aim is to convert appropriately and as you specified above, that is the case... however, if the resultant character if for nonprintable or control character, I like it to be in form of a square or box character not just a null. The choice of the 0 and 1’s, I have already highlighted in red from the file; we take the specified pairs and for every 8 bits, the equivalent ASCII is to be generated.
  • 0





Also tagged with one or more of these keywords: binary, ascii

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download