Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 30, 2016, 10:10:29 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Encode/decode cipher/decipher crypter/decrypter ASCII HEX Binary Base64 URL etc.  (Read 7199 times)

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
I'm shocked I've not been able to find a programme to convert between these schemes.

I can only find online converters that feature more than a few encoding schemes.
ASCII to Hex ...and other free text conversion tools
swiss converter tool

I haven't coded in such an age, but it seems like all of the conversion code is already out there, just needs to be put into a nice little interface.

Would love to see this! :-*

/edit: links now link
« Last Edit: February 05, 2015, 11:29:14 AM by ashecorven »

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,751
    • View Profile
    • Donate to Member
A lot of programmers editors have features like these, either directly built in or as a plugin. At least Notepad++ has plugins for this

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
Yes, npp has plugins to do it (TextFX being the main one I've used). I've just played with it again to see if anything has changed and it is still messy, does not work properly nor provide multiple direction conversion.

In npp, converting the following is not fun.

30 31 30 30 31 30 30 31 20 30 31 31 30 30 31 31 30 20 30 30 31 30 30 30 30 30 20 30 31 31 31 30 31 30 30 20 30 31 31 30 31 30 30 30 20 30 31 31 30 31 30 30 31 20 30 31 31 31 30 30 31 31 20 30 30 31 30 30 30 30 30 20 30 31 31 31 30 31 31 31 20 30 31 31 30 30 30 30 31 20 30 31 31 31 30 30 31 31 20 30 30 31 30 30 30 30 30 20 30 31 31 30 31 30 30 31 20 30 31 31 30 31 31 31 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 30 30 30 31 20 30 30 31 30 30 30 30 30 20 30 31 31 31 30 31 30 31 20 30 31 31 31 30 31 30 30 20 30 31 31 30 31 30 30 31 20 30 31 31 30 31 31 30 30 20 30 31 31 30 31 30 30 31 20 30 31 31 31 30 31 30 30 20 30 31 31 31 31 30 30 31 20 30 30 31 30 30 30 30 30 20 30 31 31 30 31 30 31 30 20 30 31 31 31 30 31 30 31 20 30 31 31 31 30 30 31 31 20 30 31 31 31 30 31 30 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 30 31 31 30 20 30 31 31 30 31 31 31 31 20 30 31 31 31 30 30 31 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 30 30 31 31 20 30 31 31 30 31 31 31 31 20 30 31 31 30 31 31 31 30 20 30 31 31 31 30 31 31 30 20 30 31 31 30 30 31 30 31 20 30 31 31 31 30 30 31 30 20 30 31 31 31 30 30 31 31 20 30 31 31 30 31 30 30 31 20 30 31 31 30 31 31 31 31 20 30 31 31 30 31 31 31 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 31 30 30 31 20 30 31 31 31 30 31 30 30 20 30 30 31 30 30 30 30 30 20 30 31 31 31 30 31 31 31 20 30 31 31 30 31 31 31 31 20 30 31 31 31 30 31 30 31 20 30 31 31 30 31 31 30 30 20 30 31 31 30 30 31 30 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 30 30 31 30 20 30 31 31 30 30 31 30 31 20 30 30 31 30 30 30 30 30 20 30 31 31 30 30 30 30 31 20 30 30 31 30 30 30 30 30 20 30 31 31 30 31 30 30 30 20 30 31 31 30 30 31 30 31 20 30 31 31 30 31 31 30 30 20 30 31 31 30 31 31 30 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 31 31 31 31 20 30 31 31 30 30 31 31 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 30 30 30 31 20 30 30 31 30 30 30 30 30 20 30 31 31 30 31 31 30 30 20 30 31 31 30 31 31 31 31 20 30 31 31 31 30 31 30 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 30 31 30 31 20 30 31 31 30 30 30 30 31 20 30 31 31 31 30 30 31 31 20 30 31 31 30 31 30 30 31 20 30 31 31 30 30 31 30 31 20 30 31 31 31 30 30 31 30 20 30 30 31 30 30 30 30 30 20 30 31 31 30 30 30 30 31 20 30 31 31 30 31 31 31 30 20 30 31 31 30 30 31 30 30 20 30 30 31 30 30 30 30 30 20 30 31 31 31 30 30 30 30 20 30 31 31 30 30 31 30 31 20 30 31 31 31 30 30 31 30 20 30 31 31 30 30 31 31 30 20 30 31 31 30 30 31 30 31 20 30 31 31 30 30 30 31 31 20 30 31 31 31 30 31 30 30 20 30 30 31 30 30 30 30 31

A simple utility just to handle conversion would be welcome and as far as I can see, does not exist.

Functionality like ASCII to Hex ...and other free text conversion tools would be nice.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,751
    • View Profile
    • Donate to Member
In npp, converting the following is not fun.

The npp standard plugin Converter turned that into this:

01001001 01100110 00100000 01110100 01101000 01101001 01110011 00100000 01110111 01100001 01110011 00100000 01101001 01101110 00100000 01100001 00100000 01110101 01110100 01101001 01101100 01101001 01110100 01111001 00100000 01101010 01110101 01110011 01110100 00100000 01100110 01101111 01110010 00100000 01100011 01101111 01101110 01110110 01100101 01110010 01110011 01101001 01101111 01101110 00100000 01101001 01110100 00100000 01110111 01101111 01110101 01101100 01100100 00100000 01100010 01100101 00100000 01100001 00100000 01101000 01100101 01101100 01101100 00100000 01101111 01100110 00100000 01100001 00100000 01101100 01101111 01110100 00100000 01100101 01100001 01110011 01101001 01100101 01110010 00100000 01100001 01101110 01100100 00100000 01110000 01100101 01110010 01100110 01100101 01100011 01110100 00100001
 
:o

And back into hex in about as many milliseconds it took to convert to ascii...

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
That's step one, now do binary to ASCII...

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,751
    • View Profile
    • Donate to Member
Please give a reasonable use-case for such a tool?

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
Exactly, it's not fun converting each byte in binary to ASCII. Notepad++ isn't the answer. Need a new tool.

Easy, Malware analysis.

I need something like this quite often, this should also explain the need to be offline at times.

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,634
    • View Profile
    • Donate to Member
If I can find some time, I should be able to do this one.  How patient are you?   :P

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
Champion!

Look forward to it.

"Patience is the art of hoping!"

BTW: Have been using epCheck for some time now.

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,634
    • View Profile
    • Donate to Member
Look forward to it.

You mentioned malware.  However, please keep in mind that whatever I come up with isn't going to handle any sort of binary file input (if that's what you envisioned).

BTW: Have been using epCheck for some time now.

Good to hear.  I like the way that app turned out.  If you're brave, feel free to try the beta.

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
You mentioned malware.  However, please keep in mind that whatever I come up with isn't going to handle any sort of binary file input (if that's what you envisioned).

Hehehe, no, that would be a BIG ask. I'm more than happy with the programmes I use and one day hope I know how to use half of the functionality they provide.

Good to hear.  I like the way that app turned out.  If you're brave, feel free to try the beta.

Trying it out now.

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
Hey how's it going?

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,634
    • View Profile
    • Donate to Member
Hey how's it going?

Unfortunately, my regular job hasn't left me with much coding time lately.  Apologies.

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
Hello again, any progress?

lifeweaver

  • Member
  • Joined in 2014
  • **
  • Posts: 22
    • View Profile
    • Donate to Member
Hi ashecorven,

Seeing as Skywire has been busy I decided to attempt your request.

Note: Autohotkey is need to run this script, although you could compile it and merely have an .exe.
Script runs on: Windows 7 Professional, AutoHotkey 1.1.15.04 Unicode build

Spoiler
Note: a lot of these functions I found posted by AutoHotkey community members, when so I documented it with a url or name.
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance ignore

Help(wParam, lParam, Msg)
{
  MouseGetPos,,,, OutputVarControl

  IfEqual, OutputVarControl, Button1
Help := "Will try to convert the input multiple times if detected as Hex/Binary."
  else IfEqual, OutputVarControl, Button2
Help := "Will replace your current clipboard"

  ToolTip % Help
}

determineEncoding(string)
{
  BINARY_REGEX := "^(\d{8}\s?)*$"
  ;~ BASE64_REGEX := "^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$"
  HEX_REGEX := "^([0-9A-Fa-f]{2}\s?)+$"
  ;~ DECIMAL_REGEX := "^([0-9]{2,3}\s?)+$"
  
  if(RegExMatch(string, BINARY_REGEX))
    return "Binary"
  else if(RegExMatch(string, HEX_REGEX))
    return "Hex"
  else
    return false
}

StrPutVar(string, ByRef var, encoding)
{
    ; Ensure capacity.
    VarSetCapacity( var, StrPut(string, encoding)
        ; StrPut returns char count, but VarSetCapacity needs bytes.
        * ((encoding="utf-16"||encoding="cp1200") ? 2 : 1) )
    ; Copy or convert the string.
    return StrPut(string, &var, encoding)
}

determineEncodingFlag(encoding, crypt)
{
  if(crypt = "encrypt")
  {
    if(encoding = "Binary")
      return 0x00000002
    else if(encoding = "BASE64")
      return 0x40000001
    else if(encoding = "Hex")
      return 0x00000004
  }
  else if (crypt = "decrypt")
  {
    if(encoding = "Binary")
      return 0x00000002
    else if(encoding = "BASE64")
      return 0x00000006
    else if(encoding = "Hex")
      return 0x00000008
  }
}

encodeUTF8( ByRef OutData, InData, inputEncoding)
{
  if(inputEncoding = "Binary")
  {
    OutData := TxtToBin(InData)
    return StrLen(OutData)
  }
  
  encoding := determineEncodingFlag(inputEncoding, "encrypt")
  InDataLen := StrPutVar(InData, InData, "UTF-8") - 1
  DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,encoding, UInt,0, UIntP,TChars, "CDECL Int" )
  VarSetCapacity( OutData, Req := TChars * ( A_IsUnicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,encoding, Str,OutData, UIntP,Req, "CDECL Int" )
  
  if(inputEncoding = "Hex")
  {
    StringReplace, OutData, OutData, %A_Space%%A_Space%, %A_Space%, All
    
    ; Not sure if I really want to remove the newlines for hex
    StringReplace, OutData, OutData, `r`n,,All
  }
  
  Return TChars
}

decodeUTF8( ByRef OutData, InData, inputEncoding)
{
  if(inputEncoding = "Binary")
  {
    OutData := BinToTxt(InData)
    return StrLen(OutData)
  }
  
  encoding := determineEncodingFlag(inputEncoding, "decrypt")
  DllCall( "Crypt32.dll\CryptStringToBinaryW", UInt,&InData, UInt,StrLen(InData), UInt,encoding, UInt,0, UIntP,Bytes, Int,0, Int,0, "CDECL Int" )
  VarSetCapacity( OutData, Req := Bytes * ( A_IsUnicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptStringToBinaryW", UInt,&InData, UInt,StrLen(InData), UInt,encoding, Str,OutData, UIntP,Req, Int,0, Int,0, "CDECL Int" )
  OutData := StrGet(&OutData, "cp0")
  Return Bytes
}

TxtToBin(txt)
{ ; http://www.autohotkey.com/board/topic/7835-ascii-binary-converter/?p=48740
  Loop Parse, txt
  {
    Loop 8
    {
      bin := bin (Asc(A_LoopField) >> (8 - A_Index) & 1)
    }
    bin .= " "
  }
   Return bin
}

BinToTxt(bin)
{ ; http://www.autohotkey.com/board/topic/7835-ascii-binary-converter/?p=48740
   StringReplace, bin, bin, %A_Space%,,All
   Loop Parse, bin
   {
      x += x + (A_LoopField = "1")

      If !Mod(A_Index,8)
      {
         txt := txt Chr(x)
         x = 0
      }
   }
   Return txt
}

Rot13(string)
{ ; from LinearSpoon's Translation of The Worlds Shortest C Implementation of Rot13 http://rosettacode.org/wiki/Rot-13#AutoHotkey
  Output := ""
  Loop, Parse, string
  {
    a := ~Asc(A_LoopField)
    Output .= Chr(~a-1//(~(a|32)//13*2-11)*13)
  }
  return Output
}

ascii2Decimal(string)
{
  encoding := ""
  Loop, Parse, string
  {
    encoding .= Asc(A_LoopField) . " "
  }
  return SubStr(encoding, 1, StrLen(encoding) - 1)
}

decimal2Ascii(string)
{
  decoding := ""
  Loop, Parse, string, %A_Space%
  {
    decoding .= Chr(A_LoopField)
  }
  return decoding
}

uriEncode(str)
{ ; v 0.3 / (w) 24.06.2008 by derRaphael / zLib-Style release http://www.autohotkey.com/board/topic/6199-url-encoding/?p=193258
b_Format := A_FormatInteger
data := ""
SetFormat,Integer,H
Loop,Parse,str
if ((Asc(A_LoopField)>0x7f) || (Asc(A_LoopField)<0x30) || (asc(A_LoopField)=0x3d))
data .= "%" . ((StrLen(c:=SubStr(ASC(A_LoopField),3))<2) ? "0" . c : c)
Else
data .= A_LoopField
SetFormat,Integer,%b_format%
return data
}

uriDecode(str)
{ ; v 0.1 / (w) 28.06.2008 by derRaphael / zLib-Style release http://www.autohotkey.com/board/topic/6199-url-encoding/?p=193258
Loop,Parse,str,`%
txt := (A_Index=1) ? A_LoopField : txt chr("0x" substr(A_LoopField,1,2)) SubStr(A_LoopField,3)
return txt
}

convertInputToText(string, inputEncoding)
{
  if(inputEncoding = "BASE64")
    decodeUTF8(decoded, string, "Base64")
  else if(inputEncoding = "Binary")
    decodeUTF8(decoded, string, "Binary")
  else if(inputEncoding = "Decimal")
    return decimal2Ascii(string)
  else if(inputEncoding = "Hex")
    decodeUTF8(decoded, string, "Hex")
  else if(inputEncoding = "ROT13")
    return Rot13(string)
  else if(inputEncoding = "URI Encoded")
    return uriDecode(string)
  
  return decoded
}

convertInputToOutputEncoding(inputEncoding, string, outputEncoding)
{
  if(outputEncoding = "BASE64")
    encodeUTF8(encoded, string, "Base64")
  else if(outputEncoding = "Binary")
    encodeUTF8(encoded, string, "Binary")
  else if(outputEncoding = "Decimal")
    return ascii2Decimal(string)
  else if(outputEncoding = "Hex")
    encodeUTF8(encoded, string, "Hex")
  else if(outputEncoding = "ROT13")
    return Rot13(string)
  else if(outputEncoding = "URI Encoded")
    return uriEncode(string)
  else if(outputEncoding = "")
    return convertInputToText(string, inputEncoding)
  else if(outputEncoding = "Text")
    return convertInputToText(string, inputEncoding)
  
  return encoded
}

Gui, Add, Text, x22 y20 w40 h20 , Input:
Gui, Add, DropDownList, x92 y20 w280 h20 vinputEncoding Choose1 R9, Text|BASE64|Binary|Decimal|Hex|ROT13|URI Encoded
;~ Gui, Add, Button, x452 y10 w110 h40 vguessEncoding gguessEncoding , Guess encoding
Gui, Add, Edit, x32 y60 w530 h270 vconverterInput ,

Gui, Add, Text, x22 y340 w40 h20 , Output:
Gui, Add, DropDownList, x92 y340 w280 h20 voutputEncoding goutputEncoding Choose1 R9, Text|BASE64|Binary|Decimal|Hex|ROT13|URI Encoded
Gui, Add, CheckBox, x462 y340 w100 h20 hwndsmartConvertHwnd vsmartConvert , Smart Convert?
Gui, Add, Edit, x32 y370 w530 h270 vconverterOutput ,
Gui, Add, Button, x32 y650 w140 h30 gcopyOutputToClipboard hwndcopyToClipboardHwnd , Copy Output to Clipboard
Gui, Add, Button, x242 y650 w150 h30 gcopyOutputToInput , Copy Output to Input
Gui, Add, Button, x462 y650 w100 h30 vencode gencode , Encode

; Generated using SmartGUI Creator 4.0
Gui, Show, xCenter yCenter h705 w585, Converter
OnMessage(0x200, "Help")
Return

GuiClose:
ExitApp

encode:
Gui, Submit, nohide
GuiControlGet, inputEncoding,, inputEncoding
GuiControlGet, converterInput,, converterInput
GuiControlGet, outputEncoding,, outputEncoding
GuiControlGet, smartConvertEnabled,, smartConvert

outputText := convertInputToOutputEncoding(inputEncoding, converterInput, outputEncoding)

if(smartConvertEnabled)
{
  result := DetermineEncoding(outputText)
  While result != false && result != outputEncoding
  {
    outputText := convertInputToText(outputText, result)
    result := DetermineEncoding(outputText)
  }
}

Guicontrol,, converterOutput, %outputText%
return

copyOutputToClipboard:
GuiControlGet, ClipBoard,, converterOutput
return

copyOutputToInput:
GuiControlGet, currentOutputEncoding,, outputEncoding
GuiControlGet, output,, converterOutput
Guicontrol,, converterInput, %output%
Guicontrol,, converterOutput
GuiControl, ChooseString, inputEncoding, %currentOutputEncoding%
GuiControl, Choose, outputEncoding, 1
GuiControl, Enable, smartConvert
return

outputEncoding:
GuiControlGet, outputEncoding,, outputEncoding
if(outputEncoding != "Text")
{
  GuiControl,, smartConvert, 0
  GuiControl, Disable, smartConvert
}
else if(outputEncoding = "Text")
{
  GuiControl, Enable, smartConvert
}
return


« Last Edit: July 24, 2015, 06:18:55 AM by lifeweaver, Reason: If only single screen gui may have displayed off screen »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,273
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Just wanted to say welcome to the site lifeweaver, and thank you for sharing your code  :up: :up: :up:

lifeweaver

  • Member
  • Joined in 2014
  • **
  • Posts: 22
    • View Profile
    • Donate to Member
Just wanted to say welcome to the site lifeweaver, and thank you for sharing your code  :up: :up: :up:

Thanks I've lurked fore a while and was glad to find something I could contribute to.

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
lifeweaver!

Have only just seen your responses. Thank you.

About to play with it now.

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
Thank you for taking the time on this, the way it works/ease of use is fantastic. Thanks for the copy to input and clipboard! The Smart Convert is great :)

Unfortunately some of the conversions aren't working correctly. Seems as though input is always (AFAICS) being read as ASCII.
For example:
- Anything to/from decimal [decimal 1 gives HEX 31 and binary 00110001] [decimal 255 gives decimal 50 53 53]
- HEX to binary is actually doing text to binary.
- Binary to HEX is actually doing text to hex.

Haven't checked B64, ROT13 and URLenc but didn't see anything unusual.

Also is it possible to have it encode to all types at once? i.e. Choose only the input type and it outputs to multiple text boxes, then each could be copied to clipboard, input box, etc.

Also I slightly altered just one thing in your script as any hex output would be missing a space after the 16th value.

Spoiler
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance ignore

Help(wParam, lParam, Msg)
{
  MouseGetPos,,,, OutputVarControl

  IfEqual, OutputVarControl, Button1
Help := "Will try to convert the input multiple times if detected as Hex/Binary."
  else IfEqual, OutputVarControl, Button2
Help := "Will replace your current clipboard"

  ToolTip % Help
}

determineEncoding(string)
{
  BINARY_REGEX := "^(\d{8}\s?)*$"
  ;~ BASE64_REGEX := "^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)$"
  HEX_REGEX := "^([0-9A-Fa-f]{2}\s?)+$"
  ;~ DECIMAL_REGEX := "^([0-9]{2,3}\s?)+$"
 
  if(RegExMatch(string, BINARY_REGEX))
    return "Binary"
  else if(RegExMatch(string, HEX_REGEX))
    return "Hex"
  else
    return false
}

StrPutVar(string, ByRef var, encoding)
{
    ; Ensure capacity.
    VarSetCapacity( var, StrPut(string, encoding)
        ; StrPut returns char count, but VarSetCapacity needs bytes.
        * ((encoding="utf-16"||encoding="cp1200") ? 2 : 1) )
    ; Copy or convert the string.
    return StrPut(string, &var, encoding)
}

determineEncodingFlag(encoding, crypt)
{
  if(crypt = "encrypt")
  {
    if(encoding = "Binary")
      return 0x00000002
    else if(encoding = "BASE64")
      return 0x40000001
    else if(encoding = "Hex")
      return 0x00000004
  }
  else if (crypt = "decrypt")
  {
    if(encoding = "Binary")
      return 0x00000002
    else if(encoding = "BASE64")
      return 0x00000006
    else if(encoding = "Hex")
      return 0x00000008
  }
}

encodeUTF8( ByRef OutData, InData, inputEncoding)
{
  if(inputEncoding = "Binary")
  {
    OutData := TxtToBin(InData)
    return StrLen(OutData)
  }
 
  encoding := determineEncodingFlag(inputEncoding, "encrypt")
  InDataLen := StrPutVar(InData, InData, "UTF-8") - 1
  DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,encoding, UInt,0, UIntP,TChars, "CDECL Int" )
  VarSetCapacity( OutData, Req := TChars * ( A_IsUnicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,encoding, Str,OutData, UIntP,Req, "CDECL Int" )
 
  if(inputEncoding = "Hex")
  {
    StringReplace, OutData, OutData, %A_Space%%A_Space%, %A_Space%, All
   
    ; Not sure if I really want to remove the newlines for hex
    StringReplace, OutData, OutData, `r`n, %A_Space%, All
  }
 
  Return TChars
}

decodeUTF8( ByRef OutData, InData, inputEncoding)
{
  if(inputEncoding = "Binary")
  {
    OutData := BinToTxt(InData)
    return StrLen(OutData)
  }
 
  encoding := determineEncodingFlag(inputEncoding, "decrypt")
  DllCall( "Crypt32.dll\CryptStringToBinaryW", UInt,&InData, UInt,StrLen(InData), UInt,encoding, UInt,0, UIntP,Bytes, Int,0, Int,0, "CDECL Int" )
  VarSetCapacity( OutData, Req := Bytes * ( A_IsUnicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptStringToBinaryW", UInt,&InData, UInt,StrLen(InData), UInt,encoding, Str,OutData, UIntP,Req, Int,0, Int,0, "CDECL Int" )
  OutData := StrGet(&OutData, "cp0")
  Return Bytes
}

TxtToBin(txt)
{ ; http://www.autohotkey.com/board/topic/7835-ascii-binary-converter/?p=48740
  Loop Parse, txt
  {
    Loop 8
    {
      bin := bin (Asc(A_LoopField) >> (8 - A_Index) & 1)
    }
    bin .= " "
  }
   Return bin
}

BinToTxt(bin)
{ ; http://www.autohotkey.com/board/topic/7835-ascii-binary-converter/?p=48740
   StringReplace, bin, bin, %A_Space%,,All
   Loop Parse, bin
   {
      x += x + (A_LoopField = "1")

      If !Mod(A_Index,8)
      {
         txt := txt Chr(x)
         x = 0
      }
   }
   Return txt
}

Rot13(string)
{ ; from LinearSpoon's Translation of The Worlds Shortest C Implementation of Rot13 http://rosettacode.org/wiki/Rot-13#AutoHotkey
  Output := ""
  Loop, Parse, string
  {
    a := ~Asc(A_LoopField)
    Output .= Chr(~a-1//(~(a|32)//13*2-11)*13)
  }
  return Output
}

ascii2Decimal(string)
{
  encoding := ""
  Loop, Parse, string
  {
    encoding .= Asc(A_LoopField) . " "
  }
  return SubStr(encoding, 1, StrLen(encoding) - 1)
}

decimal2Ascii(string)
{
  decoding := ""
  Loop, Parse, string, %A_Space%
  {
    decoding .= Chr(A_LoopField)
  }
  return decoding
}

uriEncode(str)
{ ; v 0.3 / (w) 24.06.2008 by derRaphael / zLib-Style release http://www.autohotkey.com/board/topic/6199-url-encoding/?p=193258
b_Format := A_FormatInteger
data := ""
SetFormat,Integer,H
Loop,Parse,str
if ((Asc(A_LoopField)>0x7f) || (Asc(A_LoopField)<0x30) || (asc(A_LoopField)=0x3d))
data .= "%" . ((StrLen(c:=SubStr(ASC(A_LoopField),3))<2) ? "0" . c : c)
Else
data .= A_LoopField
SetFormat,Integer,%b_format%
return data
}

uriDecode(str)
{ ; v 0.1 / (w) 28.06.2008 by derRaphael / zLib-Style release http://www.autohotkey.com/board/topic/6199-url-encoding/?p=193258
Loop,Parse,str,`%
txt := (A_Index=1) ? A_LoopField : txt chr("0x" substr(A_LoopField,1,2)) SubStr(A_LoopField,3)
return txt
}

convertInputToText(string, inputEncoding)
{
  if(inputEncoding = "BASE64")
    decodeUTF8(decoded, string, "Base64")
  else if(inputEncoding = "Binary")
    decodeUTF8(decoded, string, "Binary")
  else if(inputEncoding = "Decimal")
    return decimal2Ascii(string)
  else if(inputEncoding = "Hex")
    decodeUTF8(decoded, string, "Hex")
  else if(inputEncoding = "ROT13")
    return Rot13(string)
  else if(inputEncoding = "URI Encoded")
    return uriDecode(string)
 
  return decoded
}

convertInputToOutputEncoding(inputEncoding, string, outputEncoding)
{
  if(outputEncoding = "BASE64")
    encodeUTF8(encoded, string, "Base64")
  else if(outputEncoding = "Binary")
    encodeUTF8(encoded, string, "Binary")
  else if(outputEncoding = "Decimal")
    return ascii2Decimal(string)
  else if(outputEncoding = "Hex")
    encodeUTF8(encoded, string, "Hex")
  else if(outputEncoding = "ROT13")
    return Rot13(string)
  else if(outputEncoding = "URI Encoded")
    return uriEncode(string)
  else if(outputEncoding = "")
    return convertInputToText(string, inputEncoding)
  else if(outputEncoding = "Text")
    return convertInputToText(string, inputEncoding)
 
  return encoded
}

Gui, Add, Text, x22 y20 w40 h20 , Input:
Gui, Add, DropDownList, x92 y20 w280 h20 vinputEncoding Choose1 R9, Text|BASE64|Binary|Decimal|Hex|ROT13|URI Encoded
;~ Gui, Add, Button, x452 y10 w110 h40 vguessEncoding gguessEncoding , Guess encoding
Gui, Add, Edit, x32 y60 w530 h270 vconverterInput ,

Gui, Add, Text, x22 y340 w40 h20 , Output:
Gui, Add, DropDownList, x92 y340 w280 h20 voutputEncoding goutputEncoding Choose1 R9, Text|BASE64|Binary|Decimal|Hex|ROT13|URI Encoded
Gui, Add, CheckBox, x462 y340 w100 h20 hwndsmartConvertHwnd vsmartConvert , Smart Convert?
Gui, Add, Edit, x32 y370 w530 h270 vconverterOutput ,
Gui, Add, Button, x32 y650 w140 h30 gcopyOutputToClipboard hwndcopyToClipboardHwnd , Copy Output to Clipboard
Gui, Add, Button, x242 y650 w150 h30 gcopyOutputToInput , Copy Output to Input
Gui, Add, Button, x462 y650 w100 h30 vencode gencode , Encode

; Generated using SmartGUI Creator 4.0
Gui, Show, xCenter yCenter h705 w585, Converter
OnMessage(0x200, "Help")
Return

GuiClose:
ExitApp

encode:
Gui, Submit, nohide
GuiControlGet, inputEncoding,, inputEncoding
GuiControlGet, converterInput,, converterInput
GuiControlGet, outputEncoding,, outputEncoding
GuiControlGet, smartConvertEnabled,, smartConvert

outputText := convertInputToOutputEncoding(inputEncoding, converterInput, outputEncoding)

if(smartConvertEnabled)
{
  result := DetermineEncoding(outputText)
  While result != false && result != outputEncoding
  {
    outputText := convertInputToText(outputText, result)
    result := DetermineEncoding(outputText)
  }
}

Guicontrol,, converterOutput, %outputText%
return

copyOutputToClipboard:
GuiControlGet, ClipBoard,, converterOutput
return

copyOutputToInput:
GuiControlGet, currentOutputEncoding,, outputEncoding
GuiControlGet, output,, converterOutput
Guicontrol,, converterInput, %output%
Guicontrol,, converterOutput
GuiControl, ChooseString, inputEncoding, %currentOutputEncoding%
GuiControl, Choose, outputEncoding, 1
GuiControl, Enable, smartConvert
return

outputEncoding:
GuiControlGet, outputEncoding,, outputEncoding
if(outputEncoding != "Text")
{
  GuiControl,, smartConvert, 0
  GuiControl, Disable, smartConvert
}
else if(outputEncoding = "Text")
{
  GuiControl, Enable, smartConvert
}
return

« Last Edit: September 09, 2015, 06:46:20 AM by mouser, Reason: put code in code block »

lifeweaver

  • Member
  • Joined in 2014
  • **
  • Posts: 22
    • View Profile
    • Donate to Member
Thanks for the feed back ashecorven!  Sorry about the conversion issues, it was one of my main worries, I realize that if you're using a conversion tool you need it to be rock solid.

To clarify:

Quote
- HEX to binary is actually doing text to binary.
- Binary to HEX is actually doing text to hex.
So for these use cases what you want is to decode the HEX to text then encode that text to Binary verses encoding the HEX to Binary? And you want to do the same for the Binary to HEX?

Quote
Also is it possible to have it encode to all types at once? i.e. Choose only the input type and it outputs to multiple text boxes, then each could be copied to clipboard, input box, etc.
I'll work on this while I wait for your reply on the first question.

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
Hello again,

Thanks for the quick response!

HEX to binary and vice-versa shouldn't have anything to do with ASCII. At the moment, both binary and HEX are being read in as ASCII (converting HEX to * == converting text to *) and (converting binary to * == converting text to *).

Examples.
11111111 00000000 00000000 11111111 should convert to FF00 00FF, but output is 31 31 31 31 31 31 31 31 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30 30 30 30 20 31 31 31 31 31 31 31 31.

E.g. FF00 00FF should convert to 11111111 00000000 00000000 11111111, but output is 01000110 01000110 00110000 00110000 00100000 00110000 00110000 01000110 01000110.

I think a quick way to check if things are being read in correctly would be HEX->HEX, Binary->binary, text->text, etc. At the moment they're not working.

lifeweaver

  • Member
  • Joined in 2014
  • **
  • Posts: 22
    • View Profile
    • Donate to Member
Thanks for the input it took me a bit but I think I have everything work, turned out that the base64 was really messed up for numbers it was treating everything as a string.

Spoiler
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance ignore

Help(wParam, lParam, Msg)
{
  MouseGetPos,,,, OutputVarControl

  ;~ IfEqual, OutputVarControl, Button1
   ;~ Help := "Will try to convert the input multiple times if detected as Hex/Binary."

  ToolTip % Help
}

encodeUTF8( ByRef OutData, InData, inputEncoding, outputEncoding)
{
  encoding := determineEncodingFlag(inputEncoding, "encrypt")
  InDataLen := StrPutVar(InData, InData, "UTF-8") - 1
  DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,encoding, UInt,0, UIntP,TChars, "CDECL Int" )
  VarSetCapacity( OutData, Req := TChars * ( A_IsUnicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,encoding, Str,OutData, UIntP,Req, "CDECL Int" )
 
  Return TChars
}

determineEncodingFlag(encoding, crypt)
{
  if(crypt = "encrypt")
  {
    if(encoding = "Binary")
      return 0x00000002
    else if(encoding = "BASE64")
      return 0x40000001
    else if(encoding = "Hexadecimal")
      return 0x00000004
  }
  else if (crypt = "decrypt")
  {
    if(encoding = "Binary")
      return 0x00000002
    else if(encoding = "BASE64")
      return 0x00000001
    else if(encoding = "Hexadecimal")
      return 0x00000008
  }
}

StrPutVar(string, ByRef var, encoding)
{
    ; Ensure capacity.
    VarSetCapacity( var, StrPut(string, encoding)
        ; StrPut returns char count, but VarSetCapacity needs bytes.
        * ((encoding="utf-16"||encoding="cp1200") ? 2 : 1) )
    ; Copy or convert the string.
    return StrPut(string, &var, encoding)
}

decodeUTF8( ByRef OutData, InData, inputEncoding)
{
  encoding := determineEncodingFlag(inputEncoding, "decrypt")
  DllCall( "Crypt32.dll\CryptStringToBinaryW", UInt,&InData, UInt,StrLen(InData), UInt,encoding, UInt,0, UIntP,Bytes, Int,0, Int,0, "CDECL Int" )
  VarSetCapacity( OutData, Req := Bytes * ( A_IsUnicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptStringToBinaryW", UInt,&InData, UInt,StrLen(InData), UInt,encoding, Str,OutData, UIntP,Req, Int,0, Int,0, "CDECL Int" )
  OutData := StrGet(&OutData, "cp0")
  Return Bytes
}

TxtToBin(txt)
{ ; http://www.autohotkey.com/board/topic/7835-ascii-binary-converter/?p=48740
  Loop Parse, txt
  {
    Loop 8
    {
      bin := bin (Asc(A_LoopField) >> (8 - A_Index) & 1)
    }
    bin .= " "
  }
  return SubStr(bin, 1, (StrLen(bin) - 1))
}

BinToTxt(bin)
{ ; http://www.autohotkey.com/board/topic/7835-ascii-binary-converter/?p=48740
   StringReplace, bin, bin, %A_Space%,,All
   Loop Parse, bin
   {
      x += x + (A_LoopField = "1")

      If !Mod(A_Index,8)
      {
         txt := txt Chr(x)
         x = 0
      }
   }
   Return txt
}

Rot13(string)
{ ; from LinearSpoon's Translation of The Worlds Shortest C Implementation of Rot13 http://rosettacode.org/wiki/Rot-13#AutoHotkey
  Output := ""
  Loop, Parse, string
  {
    a := ~Asc(A_LoopField)
    Output .= Chr(~a-1//(~(a|32)//13*2-11)*13)
  }
  return Output
}

ascii2Decimal(string)
{
  encoding := ""
  Loop, Parse, string
  {
    encoding .= Asc(A_LoopField) . " "
  }
  return SubStr(encoding, 1, StrLen(encoding) - 1)
}

decimal2Ascii(string)
{
  decoding := ""
  Loop, Parse, string, %A_Space%
  {
    decoding .= Chr(A_LoopField)
  }
  return decoding
}

uriEncode(str)
{ ; v 0.3 / (w) 24.06.2008 by derRaphael / zLib-Style release http://www.autohotkey.com/board/topic/6199-url-encoding/?p=193258

   b_Format := A_FormatInteger
   data := ""
   SetFormat,Integer,H
   Loop,Parse,str
      if ((Asc(A_LoopField)>0x7f) || (Asc(A_LoopField)<0x30) || (asc(A_LoopField)=0x3d))
         data .= "%" . ((StrLen(c:=SubStr(ASC(A_LoopField),3))<2) ? "0" . c : c)
      Else
         data .= A_LoopField
   SetFormat,Integer,%b_format%
   return data
}

uriDecode(str)
{ ; v 0.1 / (w) 28.06.2008 by derRaphael / zLib-Style release http://www.autohotkey.com/board/topic/6199-url-encoding/?p=193258
   Loop,Parse,str,`%
      txt := (A_Index=1) ? A_LoopField : txt chr("0x" substr(A_LoopField,1,2)) SubStr(A_LoopField,3)
   return txt
}

ConvertBase(InputBase, OutputBase, nptr)
{ ; came from somewhere very similar to http://www.autohotkey.com/board/topic/15951-base-10-to-base-36-conversion/?p=172786
    static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
    static v := A_IsUnicode ? "_i64tow"    : "_i64toa"
    VarSetCapacity(s, 66, 0)
    value := DllCall("msvcrt.dll\" u, "Str", nptr, "UInt", 0, "UInt", InputBase, "CDECL Int64")
    DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", OutputBase, "CDECL")
    return s
}

; Uses the ConvertBase function to convert formated numbers similar to the http://www.asciitohex.com/ site
Convert(inputBase, outputBase, inputNumber)
{
  ; Binary not created correctly if hex isn't inputed in 2 digit format.
  if(inputBase = 16 && outputBase = 2)
    inputNumber := standardizeHexInputFormatForBinaryOutput(inputNumber)
 
  output := ""
  Loop, Parse, inputNumber, %A_Space%
  {
    ; convertBase ouput 0 for 16 base 0 when I need 00
    temp := ConvertBase(inputBase, outputBase, A_LoopField)
    if(temp = 0)
    {
      if(outputBase = 2)
        temp := "00000000"
      else if(outputBase = 16)
        temp := "00"
    }
   
    output .= temp . " "
  }
  StringUpper, output, output
  return SubStr(output, 1, (StrLen(output) - 1))
}

standardizeHexInputFormatForBinaryOutput(inputHex)
{
  hex := ""
  StringReplace, inputHex, inputHex, %A_Space%,,All
  Loop, Parse, inputHex
  {
    if(A_Index != 1 && Mod(A_Index - 1, 2) = 0)
    {
      hex .= " " . A_LoopField
    }
    else
    {
      hex .= A_LoopField
    }
  }
  return hex
}

convertInputToOutputEncoding(inputEncoding, string, outputEncoding)
{
  if(inputEncoding = outputEncoding)
    return string
 
  ; if the conversion is a base to base, i.e. 10(dec) to 16(hex) use the convert function
  inputEncodingBase := determineEncodingBase(inputEncoding)
  outputEncodingBase := determineEncodingBase(outputEncoding)

  if(inputEncodingBase != 0 && outputEncodingBase != 0)
  {
    return Convert(inputEncodingBase, outputEncodingBase, string)
  }
 
  ; convert any base64 to text
  if(inputEncoding = "BASE64")
    string := convertInputToText(string, inputEncoding)
 
  ; Non-base to base conversions
  if(outputEncoding = "BASE64" && inputEncoding = "Text")
    encodeUTF8(encoded, string, "BASE64", outputEncoding)
  else if(outputEncoding = "BASE64")
    encoded := ToBase64(string)
  else if(outputEncoding = "Binary" && inputEncoding = "Text")
    encoded := TxtToBin(string)
  else if(outputEncoding = "Binary")
    encodeUTF8(encoded, string, "Binary", outputEncoding)
  else if(outputEncoding = "Decimal")
    return ascii2Decimal(string)
  else if(outputEncoding = "Hexadecimal")
    encodeUTF8(encoded, string, "Hexadecimal", outputEncoding)
  else if(outputEncoding = "ROT13")
    return Rot13(string)
  else if(outputEncoding = "URI")
    return uriEncode(string)
  else if(outputEncoding = "Text" && inputEncoding != "BASE64")
    return convertInputToText(string, inputEncoding)
 
  return encoded
}

determineEncodingBase(encoding)
{
  if(encoding = "Binary")
    return 2
  else if(encoding = "Decimal")
    return 10
  else if(encoding = "Hexadecimal")
    return 16
  else
    return 0
}

ToBase64(string)
{
  static CharSet := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 
  ; convert input to binary
  binary := ""
  Loop, Parse, string
  {
    ; if the entire string is a number treat don't get the Ascii value of the numbers skip that step
    ; Pad output with zeros to each section has 8.
    if string is not integer
      binary .= SubStr("0000000" .  ConvertBase(10, 2, Asc(A_LoopField)), -7, 8)
    else
      binary .= SubStr("0000000" .  ConvertBase(10, 2, A_LoopField), -7, 8)
  }
 
  ; If number of bytes(group of eight) isn't divisible by three bytes(24) add extra zeros until you have it.
  temp := Mod(StrLen(binary), 24)
  shortSection := (24 - temp)

  binary := binary . SubStr("000000000000000000000000", 1, shortSection)

  ; if the last 24,18,12,6 are zero remove them
  if(SubStr(binary, -23) = "000000000000000000000000")
    binary := SubStr(binary, 1, StrLen(binary) - 24)
  else if(SubStr(binary, -17) = "000000000000000000")
    binary := SubStr(binary, 1, StrLen(binary) - 18)
  else if(SubStr(binary, -11) = "000000000000")
    binary := SubStr(binary, 1, StrLen(binary) - 12)
  else if(SubStr(binary, -5) = "000000")
    binary := SubStr(binary, 1, StrLen(binary) - 6)
 
  ; split binary into segements of six
  binary := Trim(RegExReplace(binary, "(.{6})", "$1 "))

  ; convert segments to decimal
  base64 := ""
  Loop, Parse, binary, %A_Space%
  {
    base64 .= SubStr(CharSet, ConvertBase(2, 10, A_LoopField) + 1, 1)
  }
 
  ; If not divisible by 4 add one or two equal signs
  if(Mod(StrLen(base64), 4) > 0)
    base64 .= SubStr("==", 1, (4 - Mod(StrLen(base64), 4)))
 
  return base64
}

convertInputToText(string, inputEncoding)
{
  if(inputEncoding = "BASE64")
    decodeUTF8(decoded, string, "Base64")
  else if(inputEncoding = "Binary")
    decoded := BinToTxt(string)
  else if(inputEncoding = "Decimal")
    return decimal2Ascii(string)
  else if(inputEncoding = "Hexadecimal")
    decodeUTF8(decoded, string, "Hexadecimal")
  else if(inputEncoding = "ROT13")
    return Rot13(string)
  else if(inputEncoding = "URI")
    return uriDecode(string)
  else
    return string
 
  return decoded
}



Gui, Add, Text, x12 y10 w30 h20 , Input:
Gui, Add, DropDownList, x52 y10 w310 h20 vinputEncoding Choose1 R9, Text|BASE64|Binary|Decimal|Hexadecimal|ROT13|URI Encoded
Gui, Add, Edit, x12 y40 w360 h60 vconverterInput,
;~ Gui, Add, CheckBox, x402 y10 w90 h30 hwndsmartConvertHwnd vsmartConvert, Smart Encode?
Gui, Add, Button, x392 y50 w150 h40 vencode gencode, Encode

Gui, Add, Text, x12 y120 w110 h20 , Text (ASCII / ANSI)
Gui, Add, Edit, x12 y150 w170 h140 vtextOutput ,
Gui, Add, Button, x12 y300 w80 h30 vcopyToClipboardText gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x102 y300 w80 h30 vcopyToInputText gcopyToInput, Copy to input

Gui, Add, Text, x202 y120 w110 h20 , Binary
Gui, Add, Edit, x192 y150 w170 h140 vbinaryOutput ,
Gui, Add, Button, x192 y300 w80 h30 vcopyToClipboardBinary gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x282 y300 w80 h30 vcopyToInputBinary gcopyToInput, Copy to input

Gui, Add, Text, x382 y120 w110 h20 , Decimal
Gui, Add, Edit, x372 y150 w170 h140 vdecimalOutput,
Gui, Add, Button, x372 y300 w80 h30 vcopyToClipboardDecimal gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x462 y300 w80 h30 vcopyToInputDecimal gcopyToInput, Copy to input

Gui, Add, Text, x12 y350 w110 h20 , BASE64
Gui, Add, Edit, x12 y380 w170 h140 vbase64Output,
Gui, Add, Button, x12 y530 w80 h30 vcopyToClipboardBase64 gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x102 y530 w80 h30 vcopyToInputBase64 gcopyToInput, Copy to input

Gui, Add, Text, x202 y350 w110 h20 , ROT13
Gui, Add, Edit, x192 y380 w170 h140 vrot13Output,
Gui, Add, Button, x192 y530 w80 h30 vcopyToClipboardRot13 gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x282 y530 w80 h30 vcopyToInputRot13 gcopyToInput, Copy to input

Gui, Add, Text, x372 y350 w110 h20 , URI Encoded
Gui, Add, Edit, x372 y380 w170 h140 vuriOutput,
Gui, Add, Button, x372 y530 w80 h30 vcopyToClipboardUri gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x462 y530 w80 h30 vcopyToInputUri gcopyToInput, Copy to input

Gui, Add, Text, x562 y120 w110 h20 , Hexadecimal
Gui, Add, Edit, x562 y150 w170 h140 vhexadecimalOutput,
Gui, Add, Button, x562 y300 w80 h30 vcopyToClipboardHexadecimal gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x652 y300 w80 h30 vcopyToInputHexadecimal gcopyToInput, Copy to input

Gui, Add, Text, x562 y530 w170 h20 , Made for request on donationcoder
; Generated using SmartGUI Creator 4.0
Gui, Show, xCenter yCenter h566 w746, Encoder
OnMessage(0x200, "Help")
Return

GuiClose:
ExitApp

encode:
Gui, Submit, nohide
GuiControlGet, inputEncoding,, inputEncoding
GuiControlGet, converterInput,, converterInput
;~ GuiControlGet, smartConvertEnabled,, smartConvert

inputList := "Text|BASE64|Binary|Decimal|Hexadecimal|ROT13|URI"
Loop, Parse, inputList, |
{
  ; clear the output edit field first
  Guicontrol,, % A_LoopField . "Output"
 
  Guicontrol,, % A_LoopField . "Output", % convertInputToOutputEncoding(inputEncoding, converterInput, A_LoopField)
}

return

copyToClipboard:
; Use the name of variable name of the button to get the text from the correct edit box
; Each button is named 'copyToClipboardXXXXXX' and each edit is named 'XXXOutput'
GuiControlGet, ClipBoard,, % SubStr(A_GuiControl, 16) . "Output"
return

copyToInput:
; use A_GuiControl to figure out the new input encoding
newInputEncoding := SubStr(A_GuiControl, 12)

; Populate the input encoding
GuiControl, ChooseString, inputEncoding, %newInputEncoding%

; populate input with selected output text found via A_GuiControl
GuiControlGet, output,, % newInputEncoding . "Output"
Guicontrol,, converterInput, %output%

return



lifeweaver

  • Member
  • Joined in 2014
  • **
  • Posts: 22
    • View Profile
    • Donate to Member
Look like everything wasn't quite right yet.

Spoiler
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance ignore

Help(wParam, lParam, Msg)
{
  MouseGetPos,,,, OutputVarControl

  ;~ IfEqual, OutputVarControl, Button1
   ;~ Help := "Will try to convert the input multiple times if detected as Hex/Binary."

  ToolTip % Help
}

encodeUTF8( ByRef OutData, InData, inputEncoding, outputEncoding)
{
  encoding := determineEncodingFlag(inputEncoding, "encrypt")
  InDataLen := StrPutVar(InData, InData, "UTF-8") - 1
  DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,encoding, UInt,0, UIntP,TChars, "CDECL Int" )
  VarSetCapacity( OutData, Req := TChars * ( A_IsUnicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptBinaryToStringW", UInt,&InData, UInt,InDataLen, UInt,encoding, Str,OutData, UIntP,Req, "CDECL Int" )
 
  Return TChars
}

determineEncodingFlag(encoding, crypt)
{
  if(crypt = "encrypt")
  {
    if(encoding = "Binary")
      return 0x00000002
    else if(encoding = "BASE64")
      return 0x40000001
    else if(encoding = "Hexadecimal")
      return 0x00000004
  }
  else if (crypt = "decrypt")
  {
    if(encoding = "Binary")
      return 0x00000002
    else if(encoding = "BASE64")
      return 0x00000001
    else if(encoding = "Hexadecimal")
      return 0x00000008
  }
}

StrPutVar(string, ByRef var, encoding)
{
    ; Ensure capacity.
    VarSetCapacity( var, StrPut(string, encoding)
        ; StrPut returns char count, but VarSetCapacity needs bytes.
        * ((encoding="utf-16"||encoding="cp1200") ? 2 : 1) )
    ; Copy or convert the string.
    return StrPut(string, &var, encoding)
}

decodeUTF8( ByRef OutData, InData, inputEncoding)
{
  encoding := determineEncodingFlag(inputEncoding, "decrypt")
  DllCall( "Crypt32.dll\CryptStringToBinaryW", UInt,&InData, UInt,StrLen(InData), UInt,encoding, UInt,0, UIntP,Bytes, Int,0, Int,0, "CDECL Int" )
  VarSetCapacity( OutData, Req := Bytes * ( A_IsUnicode ? 2 : 1 ), 0 )
  DllCall( "Crypt32.dll\CryptStringToBinaryW", UInt,&InData, UInt,StrLen(InData), UInt,encoding, Str,OutData, UIntP,Req, Int,0, Int,0, "CDECL Int" )
  OutData := StrGet(&OutData, "cp0")
  Return Bytes
}

TxtToBin(txt)
{ ; http://www.autohotkey.com/board/topic/7835-ascii-binary-converter/?p=48740
  Loop Parse, txt
  {
    Loop 8
    {
      bin := bin (Asc(A_LoopField) >> (8 - A_Index) & 1)
    }
    bin .= " "
  }
  return SubStr(bin, 1, (StrLen(bin) - 1))
}

BinToTxt(bin)
{ ; http://www.autohotkey.com/board/topic/7835-ascii-binary-converter/?p=48740
   StringReplace, bin, bin, %A_Space%,,All
   Loop Parse, bin
   {
      x += x + (A_LoopField = "1")

      If !Mod(A_Index,8)
      {
         txt := txt Chr(x)
         x = 0
      }
   }
   Return txt
}

Rot13(string)
{ ; from LinearSpoon's Translation of The Worlds Shortest C Implementation of Rot13 http://rosettacode.org/wiki/Rot-13#AutoHotkey
  Output := ""
  Loop, Parse, string
  {
    a := ~Asc(A_LoopField)
    Output .= Chr(~a-1//(~(a|32)//13*2-11)*13)
  }
  return Output
}

ascii2Decimal(string)
{
  encoding := ""
  Loop, Parse, string
  {
    encoding .= Asc(A_LoopField) . " "
  }
  return SubStr(encoding, 1, StrLen(encoding) - 1)
}

decimal2Ascii(string)
{
  decoding := ""
  Loop, Parse, string, %A_Space%
  {
    decoding .= Chr(A_LoopField)
  }
  return decoding
}

uriEncode(str)
{ ; v 0.3 / (w) 24.06.2008 by derRaphael / zLib-Style release http://www.autohotkey.com/board/topic/6199-url-encoding/?p=193258

   b_Format := A_FormatInteger
   data := ""
   SetFormat,Integer,H
   Loop,Parse,str
      if ((Asc(A_LoopField)>0x7f) || (Asc(A_LoopField)<0x30) || (asc(A_LoopField)=0x3d))
         data .= "%" . ((StrLen(c:=SubStr(ASC(A_LoopField),3))<2) ? "0" . c : c)
      Else
         data .= A_LoopField
   SetFormat,Integer,%b_format%
   return data
}

uriDecode(str)
{ ; v 0.1 / (w) 28.06.2008 by derRaphael / zLib-Style release http://www.autohotkey.com/board/topic/6199-url-encoding/?p=193258
   Loop,Parse,str,`%
      txt := (A_Index=1) ? A_LoopField : txt chr("0x" substr(A_LoopField,1,2)) SubStr(A_LoopField,3)
   return txt
}

ConvertBase(InputBase, OutputBase, nptr)
{ ; came from somewhere very similar to http://www.autohotkey.com/board/topic/15951-base-10-to-base-36-conversion/?p=172786
    static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
    static v := A_IsUnicode ? "_i64tow"    : "_i64toa"
    VarSetCapacity(s, 66, 0)
    value := DllCall("msvcrt.dll\" u, "Str", nptr, "UInt", 0, "UInt", InputBase, "CDECL Int64")
    DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", OutputBase, "CDECL")
    return s
}

; Uses the ConvertBase function to convert formated numbers similar to the http://www.asciitohex.com/ site
Convert(inputBase, outputBase, inputNumber)
{
  ; Binary not created correctly if hex isn't inputed in 2 digit format.
  if(inputBase = 16 && outputBase = 2)
    inputNumber := standardizeHexInputFormatForBinaryOutput(inputNumber)
 
  output := ""
  Loop, Parse, inputNumber, %A_Space%
  {
    ; convertBase ouput 0 for 16 base 0 when I need 00
    temp := ConvertBase(inputBase, outputBase, A_LoopField)
    if(temp = 0)
    {
      if(outputBase = 2)
        temp := "00000000"
      else if(outputBase = 16)
        temp := "00"
    }
   
    output .= temp . " "
  }
  StringUpper, output, output
  return SubStr(output, 1, (StrLen(output) - 1))
}

standardizeHexInputFormatForBinaryOutput(inputHex)
{
  hex := ""
  StringReplace, inputHex, inputHex, %A_Space%,,All
  Loop, Parse, inputHex
  {
    if(A_Index != 1 && Mod(A_Index - 1, 2) = 0)
    {
      hex .= " " . A_LoopField
    }
    else
    {
      hex .= A_LoopField
    }
  }
  return hex
}

convertInputToOutputEncoding(inputEncoding, string, outputEncoding)
{
  if(inputEncoding = outputEncoding)
    return string
 
  ; if the conversion is a base to base, i.e. 10(dec) to 16(hex) use the convert function
  inputEncodingBase := determineEncodingBase(inputEncoding)
  outputEncodingBase := determineEncodingBase(outputEncoding)

  if(inputEncodingBase != 0 && outputEncodingBase != 0)
  {
    return Convert(inputEncodingBase, outputEncodingBase, string)
  }
 
  ; convert any base64 to text
  if(inputEncoding = "BASE64")
    string := convertInputToText(string, inputEncoding)
 
  ; Non-base to base conversions
  if(outputEncoding = "BASE64" && inputEncoding = "Text")
    encodeUTF8(encoded, string, "BASE64", outputEncoding)
  else if(outputEncoding = "BASE64")
    encoded := ToBase64(string)
  else if(outputEncoding = "Binary" && (inputEncoding = "Text" || inputEncoding = "BASE64"))
    encoded := TxtToBin(string)
  else if(outputEncoding = "Binary")
    encodeUTF8(encoded, string, "Binary", outputEncoding)
  else if(outputEncoding = "Decimal")
    return ascii2Decimal(string)
  else if(outputEncoding = "Hexadecimal")
    encodeUTF8(encoded, string, "Hexadecimal", outputEncoding)
  else if(outputEncoding = "ROT13")
    return Rot13(string)
  else if(outputEncoding = "URI")
    return uriEncode(string)
  else if(outputEncoding = "Text" && inputEncoding != "BASE64")
    return convertInputToText(string, inputEncoding)
  else if(outputEncoding = "Text" && inputEncoding = "BASE64")
    return string
 
  return encoded
}

determineEncodingBase(encoding)
{
  if(encoding = "Binary")
    return 2
  else if(encoding = "Decimal")
    return 10
  else if(encoding = "Hexadecimal")
    return 16
  else
    return 0
}

ToBase64(string)
{
  static CharSet := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 
  ; convert input to binary unless input is already binary
  if((RegExMatch(string, "^(\d{8}\s?)*$")))
  {
    StringReplace, string, string, %A_Space%,,All
    binary := string
  }
  else
  {
    binary := ""
    Loop, Parse, string
    {
      ; if the entire string is a number treat don't get the Ascii value of the numbers skip that step
      ; Pad output with zeros to each section has 8.
      if string is not integer
        binary .= SubStr("0000000" .  ConvertBase(10, 2, Asc(A_LoopField)), -7, 8)
      else
        binary .= SubStr("0000000" .  ConvertBase(10, 2, A_LoopField), -7, 8)
    }
  }
 
  ; If number of bytes(group of eight) isn't divisible by three bytes(24) add extra zeros until you have it.
  temp := Mod(StrLen(binary), 24)
  shortSection := (24 - temp)

  binary := binary . SubStr("000000000000000000000000", 1, shortSection)

  ; if the last 24,18,12,6 are zero remove them
  if(SubStr(binary, -23) = "000000000000000000000000")
    binary := SubStr(binary, 1, StrLen(binary) - 24)
  else if(SubStr(binary, -17) = "000000000000000000")
    binary := SubStr(binary, 1, StrLen(binary) - 18)
  else if(SubStr(binary, -11) = "000000000000")
    binary := SubStr(binary, 1, StrLen(binary) - 12)
  else if(SubStr(binary, -5) = "000000")
    binary := SubStr(binary, 1, StrLen(binary) - 6)
 
  ; split binary into segements of six
  binary := Trim(RegExReplace(binary, "(.{6})", "$1 "))

  ; convert segments to decimal
  base64 := ""
  Loop, Parse, binary, %A_Space%
  {
    base64 .= SubStr(CharSet, ConvertBase(2, 10, A_LoopField) + 1, 1)
  }
 
  ; If not divisible by 4 add one or two equal signs
  if(Mod(StrLen(base64), 4) > 0)
  {
    ; case where input was binary
    if(Mod(StrLen(base64), 4) < 2)
      base64 .= "A"
   
    base64 .= SubStr("==", 1, (4 - Mod(StrLen(base64), 4)))
  }
 
  return base64
}

Base64Decode(Code)
{ ;http://www.autohotkey.com/board/topic/5545-base64-coderdecoder/?p=690930
    static CharSet := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    Length := StrLen(Code)

    ;remove padding if present
    If SubStr(Code,0) = "="
    {
        If SubStr(Code,-1,1) = "="
            Length -= 2
        Else
            Length --
    }

    BufferSize := Ceil((Length / 4) * 3), VarSetCapacity(Data,BufferSize) ;calculate the correct buffer size
    Index := 1, BinPos := 0
    Loop, % Length >> 2 ;process 4 characters per iteration
    {
        ;decode the characters and store them in the output buffer
        Value := ((InStr(CharSet,SubStr(Code,Index,1),1) - 1) << 18)
            | ((InStr(CharSet,SubStr(Code,Index + 1,1),1) - 1) << 12)
            | ((InStr(CharSet,SubStr(Code,Index + 2,1),1) - 1) << 6)
            | (InStr(CharSet,SubStr(Code,Index + 3,1),1) - 1)
        Index += 4
        Data .= Chr(Value >> 16) . Chr((Value >> 8) & 255) . Chr(Value & 255)
    }
    Length &= 3 ;determine the number of characters that remain
    If Length > 0 ;characters remain
    {
        ;decode the first of the remaining characters and store it in the output buffer
        Value := ((InStr(CharSet,SubStr(Code,Index,1),1) - 1) << 18)
            | ((InStr(CharSet,SubStr(Code,Index + 1,1),1) - 1) << 12)
        Data .= Chr(Value >> 16)

        ;another character remains
        If Length = 3
        {
            ;decode the character and store it in the output buffer
            Value |= (InStr(CharSet,SubStr(Code,Index + 2,1),1) - 1) << 6
            Data .= Chr((Value >> 8) & 255)
        }
    }
    Return, Data
}

convertInputToText(string, inputEncoding)
{
  if(inputEncoding = "BASE64")
    decoded := Base64Decode(string)
  else if(inputEncoding = "Binary")
    decoded := BinToTxt(string)
  else if(inputEncoding = "Decimal")
    return decimal2Ascii(string)
  else if(inputEncoding = "Hexadecimal")
    decodeUTF8(decoded, string, "Hexadecimal")
  else if(inputEncoding = "ROT13")
    return Rot13(string)
  else if(inputEncoding = "URI")
    return uriDecode(string)
  else
    return string
 
  return decoded
}



Gui, Add, Text, x12 y10 w30 h20 , Input:
Gui, Add, DropDownList, x52 y10 w310 h20 vinputEncoding Choose1 R9, Text|BASE64|Binary|Decimal|Hexadecimal|ROT13|URI Encoded
Gui, Add, Edit, x12 y40 w360 h60 vconverterInput,
;~ Gui, Add, CheckBox, x402 y10 w90 h30 hwndsmartConvertHwnd vsmartConvert, Smart Encode?
Gui, Add, Button, x392 y50 w150 h40 vencode gencode, Encode

Gui, Add, Text, x12 y120 w110 h20 , Text (ASCII / ANSI)
Gui, Add, Edit, x12 y150 w170 h140 vtextOutput ,
Gui, Add, Button, x12 y300 w80 h30 vcopyToClipboardText gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x102 y300 w80 h30 vcopyToInputText gcopyToInput, Copy to input

Gui, Add, Text, x202 y120 w110 h20 , Binary
Gui, Add, Edit, x192 y150 w170 h140 vbinaryOutput ,
Gui, Add, Button, x192 y300 w80 h30 vcopyToClipboardBinary gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x282 y300 w80 h30 vcopyToInputBinary gcopyToInput, Copy to input

Gui, Add, Text, x382 y120 w110 h20 , Decimal
Gui, Add, Edit, x372 y150 w170 h140 vdecimalOutput,
Gui, Add, Button, x372 y300 w80 h30 vcopyToClipboardDecimal gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x462 y300 w80 h30 vcopyToInputDecimal gcopyToInput, Copy to input

Gui, Add, Text, x12 y350 w110 h20 , BASE64
Gui, Add, Edit, x12 y380 w170 h140 vbase64Output,
Gui, Add, Button, x12 y530 w80 h30 vcopyToClipboardBase64 gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x102 y530 w80 h30 vcopyToInputBase64 gcopyToInput, Copy to input

Gui, Add, Text, x202 y350 w110 h20 , ROT13
Gui, Add, Edit, x192 y380 w170 h140 vrot13Output,
Gui, Add, Button, x192 y530 w80 h30 vcopyToClipboardRot13 gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x282 y530 w80 h30 vcopyToInputRot13 gcopyToInput, Copy to input

Gui, Add, Text, x372 y350 w110 h20 , URI Encoded
Gui, Add, Edit, x372 y380 w170 h140 vuriOutput,
Gui, Add, Button, x372 y530 w80 h30 vcopyToClipboardUri gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x462 y530 w80 h30 vcopyToInputUri gcopyToInput, Copy to input

Gui, Add, Text, x562 y120 w110 h20 , Hexadecimal
Gui, Add, Edit, x562 y150 w170 h140 vhexadecimalOutput,
Gui, Add, Button, x562 y300 w80 h30 vcopyToClipboardHexadecimal gcopyToClipboard, Copy to Clipboard
Gui, Add, Button, x652 y300 w80 h30 vcopyToInputHexadecimal gcopyToInput, Copy to input

Gui, Add, Text, x562 y530 w170 h20 , Made for request on donationcoder
; Generated using SmartGUI Creator 4.0
Gui, Show, xCenter yCenter h566 w746, Encoder
OnMessage(0x200, "Help")
Return

GuiClose:
ExitApp

encode:
Gui, Submit, nohide
GuiControlGet, inputEncoding,, inputEncoding
GuiControlGet, converterInput,, converterInput
;~ GuiControlGet, smartConvertEnabled,, smartConvert

inputList := "Text|BASE64|Binary|Decimal|Hexadecimal|ROT13|URI"
Loop, Parse, inputList, |
{
  ; clear the output edit field first
  Guicontrol,, % A_LoopField . "Output"
 
  Guicontrol,, % A_LoopField . "Output", % convertInputToOutputEncoding(inputEncoding, converterInput, A_LoopField)
}

return

copyToClipboard:
; Use the name of variable name of the button to get the text from the correct edit box
; Each button is named 'copyToClipboardXXXXXX' and each edit is named 'XXXOutput'
GuiControlGet, ClipBoard,, % SubStr(A_GuiControl, 16) . "Output"
return

copyToInput:
; use A_GuiControl to figure out the new input encoding
newInputEncoding := SubStr(A_GuiControl, 12)

; Populate the input encoding
GuiControl, ChooseString, inputEncoding, %newInputEncoding%

; populate input with selected output text found via A_GuiControl
GuiControlGet, output,, % newInputEncoding . "Output"
Guicontrol,, converterInput, %output%

return


« Last Edit: September 11, 2015, 09:36:34 AM by lifeweaver »

ashecorven

  • Supporting Member
  • Joined in 2015
  • **
  • Posts: 19
    • View Profile
    • Donate to Member
Lifeweaver, thank you, it's fantastic. Everything seems to be working fantastically.

Really appreciated.

I'm not sure how this is done, but I think this should be added to the list of software available on the site!