Binary to Text?

Hi,
I am trying to convert a textarea containing binary, to plain text, but am struggling :frowning:
Can anyone point me in the right direction, as to how to convert binary to plain text?

Thank you all in advance.

a “textarea containing binary”… exactly what do you mean?
A textarea contains text, are you saying the text is “100111010010” and you want to make it “ABCD”?
perhaps some examples might clear up the problem you wish to solve.

Yes - that’s exactly what I meant :slight_smile:
I have a textarea which contains for example:

01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100

And I need to convert it back to “Hello World”.

Hope that was clearer :slight_smile:

dim s as string
dim i as integer
for i=1 to len(textarea.text) step 8
s=s+=chrb(val("&b"+mid(textarea.text,i,8))
next i
msgbox s

now that snippet makes a HUGE assumption… and that is that the first character does in fact start a 8 bit sequence… .off by 1 bit and the answer will be gibberish
and if there are spaces, you might want to make that STEP 9

Thank you Dave.
However, there seems to be a syntax error there somewhere?

I tried changing:

s=s+=chrb(val("&b"+mid(textarea.text,i,8))

to

s=s+chrb(val("&b"+mid(textarea.text,i,8))

But it still gives a syntax error?

missing )

Arghhhhhh :frowning:
I get this back when trying to convert:

h2[?Fy?nor6??

I guess this is what you meant by gibberish.
Now I’m even more lost than before :frowning:

Would it help if I showed my code which converts plain text to binary?
Then it may be easier to help convert it back.

Thanks for trying to help.

BUT if you didn’t compenstate for the SPACES (step 9 not step 8) then the first 2 returns would be “h2”

dim s as string
dim i as integer
for i=1 to len(textarea.text) step 8
s=s+=chrb(val("&b"+mid(replaceall(textarea.text," ",""),i,8))) // just take the spaces out
next i
msgbox s

single space in quotes, followed by NOthing between the quotes

Those pesky spaces :slight_smile:
All works perfectly now Dave - thank you very much for spending the time to help - it is much appreciated!

Thank you.

FYI, I included EncodeBin and DecodeBin in M_String.

http://www.mactechnologies.com/index.php?page=downloads#m_string

but if you KNOW there are spaces… here is a faster way

dim i as integer
dim s as string
dim v() as string
v=split(textarea.text," ")
for i=0 to v.ubound
s=s+chrb(val("&b"+v(i)))
next i

I will not know if there will be spaces, so I guess I need to stick with the original solution.
Thanks anyway.

If you’re intercepting a bitstream and are not sure whether you have started the capture at a byte boundary, you can try different starting offsets. For each starting offset, check the stream of converted bytes for values greater than 127. They should occur very rarely, or not at all. If you get a high count of these high byte values, it’s a sign that you’ve started in the wrong spot. So, increment the offset and start over again.

I used this technique years ago to crack a crude password encryption scheme. :slight_smile:

assuming you’re dealing with pure ascii and not something like iso latin 1 or one of the many other single byte encodings which make use of all those value 127-255 with some degree of frequency

True, but the frequency should still be much lower than 50% because the frequency of accented characters is quite low relative to unaccented characters. However if the decoding begins in the wrong spot, the frequency of bytes starting with a one will be about 50% on average. So, it should be quite easy to tell the difference.

This should convert the binary correctly even if the binary text starts with extraneous bits.

[code] dim i,offset,hiCount,ChrCount,cval,oBest As Integer
dim ratio,bestRatio As Single
dim txt,sBest As String
txt=ef_input.Text.ReplaceAll(" “,”")
bestRatio=10.0
oBest=0
for offset=0 to 7
ChrCount=(len(txt)-offset)\8
hiCount=0
for i=1 to min(800,ChrCount8) step 8
if mid(txt,i+offset,1)=“1” then
hiCount=hiCount+1
end if
next i
ratio=hiCount/ChrCount
if ratio < bestRatio then
bestRatio=ratio
oBest=offset
end if
next
sBest=""
ChrCount=(len(txt)-oBest)\8
for i=1 to ChrCount
8 step 8
sBest=sBest+chrb(val("&b"+mid(txt,i+oBest,8)))
next i

ef_output.Text = sBest
ef_output.AppendText EndOfLine
ef_output.AppendText EndOfLine+"Total character count: "+str(ChrCount)
ef_output.AppendText EndOfLine+"Best Ratio: "+str(bestRatio)
ef_output.AppendText EndOfLine+"Best Offset: "+str(oBest)
[/code]