Jump to content

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

ASCII Binary Converter


  • Please log in to reply
5 replies to this topic
Veovis
  • Members
  • 389 posts
  • Last active: Mar 17 2009 12:24 AM
  • Joined: 13 Feb 2006
Here is a little script I made.
Just a basic ACSCII <-> Binary converter. I wasnt sure if there was a BinToDec or a DecToBin converter so I just made my own. Enjoy!

Download :arrow: ASCII_Binary.ahk

Posted Image

#singleinstance force

gui, add, edit, r10 w350 vInput, ASCII <--> Binary Converter`nBy Veovis`nWritten in AutoHotkey`nhttp://www.autohotkey.com
gui, add, text, ,Padding between bytes:
gui, add, edit, xp+130 w100 vpadding
gui, add, button, x50 w100 gASCBin, ASCII -> Binary
gui, add, button, xp+150 w100 gBinASC, Binary -> ASCII
gui, show
return

ASCBin:
	output = 
	gui, submit, nohide
	loopcount := strlen(Input)
	loop, %loopcount%
	{
		num := Asc(Input) ;obtains the ASCII value of the FIRST char in the input string
		nextbyte = % Binary(num)
		output := output nextbyte padding 
		stringTrimLeft, Input,Input,1
	}
	Guicontrol,,Input,%output%

return

BinASC:
	output = 
	gui, submit, nohide
	if padding
		Stringreplace,Input,Input,%padding%,,A
	SetFormat, Float,0.2
	loopcount := strlen(Input)/8
	test := mod(Strlen(Input),8)
	if test <> 0
	{
		msgbox, The value entered is not proper 8 bit ASCII (last byte is %test% bits)`nYou might want to check your padding settings
		return
	}
	loop, %loopcount%
	{
		StringLeft, NextByte, Input, 8
		NextByte := Decimal(NextByte)
		if NextByte = not_bin
		{
			msgbox, Binary Error`nBinary can only contain 1s and 0s.
			return
		}
		nextbyte := Chr(NextByte)
		output := output nextbyte
		StringTrimLeft, Input, Input, 8
	}
	Guicontrol,,Input,%output%
return

GuiClose:
ExitApp

Binary(num)
{
	power = 0
	output = 0
	loop
	{
		SetFormat, Float,0.1
		num := num/2
		SetFormat, Float,0.0
		bit := (num - Floor(num))*2
		num := Floor(num)
		output := output + (bit*10**power)
		power++
		if num = 0
			break
	}
	SetFormat, Float, 08.0
	output += 0.0
	return %output%
}

Decimal(num)
{
	power = 0
	output = 0
	loop
	{
		SetFormat, Float, 0.1
		num := num/10
		bit := (num - Floor(num))*10
		if (bit <> 1 AND bit <> 0)
			return "not_bin"
		num := Floor(num)
		output := output + (bit*2**power)
		power++
		if num = 0
			break
	}
	SetFormat, Float, 0.0
	output += 0
	return %output%
}

Posted Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."

Invalid User
  • Members
  • 447 posts
  • Last active: Mar 27 2012 01:04 PM
  • Joined: 14 Feb 2005
Such functions do exist. Find them herehttp://www.autohotke...opic.php?t=5016
they are under converstions and are named bintotxt and txttobin as they convert entire strings
my lame sig :)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
But this one has a nice GUI.

Invalid User
  • Members
  • 447 posts
  • Last active: Mar 27 2012 01:04 PM
  • Joined: 14 Feb 2005
yeah that is pretty, huh?! :roll:
my lame sig :)

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
The utility escapes me, but it is indeed nice and funny. I suppose that's enough for its existence... :-)
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

Such functions do exist.

Well, they do, but they are unnecessarily complicated and long. Furthermore, one of them turns AutoTrim on, which might have undesirable side effects. Here they are in shorter form.
Text = AaB-รพ

Bin := TxtToBin(Text)

MsgBox % Text "`n" Bin "`n" BinToTxt(Bin)



TxtToBin(txt) {

   Loop Parse, txt

      Loop 8

         bin := bin (Asc(A_LoopField) >> (8 - A_Index) & 1)

   Return bin

}



BinToTxt(bin) {

   Loop Parse, bin

   {

      x += x + (A_LoopField = "1")

      If !Mod(A_Index,8) {

         txt := txt Chr(x)

         x = 0

      }

   }

   Return txt

}