AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ASCII Binary Converter

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Veovis



Joined: 13 Feb 2006
Posts: 390
Location: Utah

PostPosted: Thu Mar 09, 2006 10:36 pm    Post subject: ASCII Binary Converter Reply with quote

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



Code:
#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%
}

_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Thu Mar 09, 2006 11:07 pm    Post subject: Reply with quote

Such functions do exist. Find them herehttp://www.autohotkey.com/forum/viewtopic.php?t=5016
they are under converstions and are named bintotxt and txttobin as they convert entire strings
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu Mar 09, 2006 11:15 pm    Post subject: Reply with quote

But this one has a nice GUI.
Back to top
View user's profile Send private message
Invalid User



Joined: 14 Feb 2005
Posts: 442
Location: Texas, Usa

PostPosted: Thu Mar 09, 2006 11:47 pm    Post subject: Reply with quote

yeah that is pretty, huh?! Rolling Eyes
_________________
my lame sig Smile
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Fri Mar 10, 2006 9:12 am    Post subject: Reply with quote

The utility escapes me, but it is indeed nice and funny. I suppose that's enough for its existence... Smile
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Fri Mar 10, 2006 5:00 pm    Post subject: Reply with quote

Invalid User wrote:
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.
Code:
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
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group