Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Text to Binary (and back) Function


  • Please log in to reply
8 replies to this topic
Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
I can't think of a real world use for this. I just saw it as a challenge to see if I could make a function to convert text to binary.

It will convert a string to binary.

string=this is a test
Binary:=binary(string)
msgbox, %Binary%


binary(string)
{

Loop, parse, string
{
var=128

Transform, tobin, Asc, %A_LoopField%

loop, 8
{
oldtobin=%tobin%
tobin:=tobin-var

value=1

if tobin<0
{
tobin=%oldtobin%
value=0
}

var/=2
allvalues=%allvalues%%value%
}
}
return allvalues
}


polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Nice! :)
Would be even cooler if it converted more than a char.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004

Nice! :)
Would be even cooler if it converted more than a char.


This will convert a whole string to binary 8)

string=this is a test
Binary:=binary(string)
msgbox, %Binary%


binary(string)
{

Loop, parse, string
{
var=128

Transform, tobin, Asc, %A_LoopField%

loop, 8
{
oldtobin=%tobin%
tobin:=tobin-var

value=1

if tobin<0
{
tobin=%oldtobin%
value=0
}

var/=2
allvalues=%allvalues%%value%
}
}
return allvalues
}


polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Freaky, I just done the exact same thing (parsing loop) before I posted; I could've made a silly typo bleh.

Great script Jon!

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
Thanks :)

You can convert it back to text again with this script-

binarystring=01110100011001010111001101110100

text:=bin2txt(binarystring)
msgbox, %text%



bin2txt(binarystring)
{
autotrim, off
loop
{
var=128
ascii=0

StringRight, byte, binarystring, 8

if byte=
break

StringTrimRight, binarystring, binarystring, 8

Loop, parse, byte
{
if a_loopfield = 1
ascii+=%var%
var/=2
}

transform, text, Chr, %ascii%
alltext=%text%%alltext%
}
autotrim, on
return alltext
}


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Titan, I accidentally deleted your reply that contained the GUI front end for the converter (I pressed "Edit" instead of "Quote" again :oops:). Please repost it if you have it.

Chris should post your txt2bin func to the showcase, a function example would be good to have.

It would be good to have something with functions eventually, but the showcase focuses on more practical scripts with mass appeal. So it's probably best to wait for one of them to come along.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
~ repost

Gui version of Jon's bin2txt (and vise versa)
; bin2txt | txt2bin
; By Jon

Menu, Tray, Add
Menu, Tray, Add, Show, gshow
Menu, Tray, Default, Show
Menu, Tray, Click, 1
Gui, +ToolWindow
Gui, Font, bold
Gui, Add, Text, w25, String:
Gui, Font
Gui, Add, Edit, vstring w175
Gui, Add, Button, section gConv default, Convert!
Gui, Add, Radio, vcb2t ys+5, bin2text
Gui, Add, Radio, vct2b ys+5 checked, txt2bin
gshow:
Gui, Show,, 0110001000110010011101002
Return

Conv:
Gui, Submit, NoHide
if cb2t  = 1
	txt := bin2txt(string)
else
	txt := txt2bin(string)
clipboard = %txt%
Msgbox, %txt%
Return

bin2txt(string) 
{ 
autotrim, off 
loop 
{ 
var=128 
ascii=0
StringRight, byte, string, 8 
if byte= 
break 
StringTrimRight, string, string, 8 
Loop, parse, byte 
{ 
if a_loopfield = 1 
ascii+=%var% 
var/=2 
} 
transform, text, Chr, %ascii% 
alltext=%text%%alltext% 
} 
autotrim, on 
return alltext 
}
txt2bin(string) 
{ 
Loop, parse, string 
{ 
var=128 
Transform, tobin, Asc, %A_LoopField% 
loop, 8 
{ 
oldtobin=%tobin% 
tobin:=tobin-var 
transform, isnegative, Log, %tobin% 
value=1 
if isnegative= 
{ 
tobin=%oldtobin% 
value=0 
} 
var/=2 
allvalues=%allvalues%%value% 
} 
} 
return allvalues 
}
Return
GuiClose:
ExitApp

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
Hi Titan, Thanks for putting a GUI together. I did think of making a GUI for it but you beat me to it :)

It would be good to have something with functions eventually, but the showcase focuses on more practical scripts with mass appeal. So it's probably best to wait for one of them to come along.


It would be good to have some sort of functions showcase as some point. I agree that this isn't suitable for a showcase though since I can't think of a practical use for it :)

01000101011110000110001101100101011100000111010000100000010001010110111001100011011100100111100101110000011101000110100101101110011001110010000001110100011001010111100001110100001000000011101000101001

Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
I found an autoit function to convert text to binary and converted it to AHK. It is less code but I don't understand how the Bit operations work.

string=test
msgbox, % TextToBin(string)

TextToBin(char)
{
Loop, parse, char
{
Transform, dec, Asc, %A_LoopField%
allbin=
   Loop, 8
   {
   Transform, bin, BitAND,dec, 1
   Transform, dec, BitShiftRight,dec,1
   allbin= %bin%%allbin%
   }
binary=%binary%%allbin%
}
Return binary
}