Here's a question to start off the thread.
Earlier today I was trying to get write some basic AHK functions which allow me to convert characters which I've copied to the clipboard to a different representation. For instance, my initial use case was something like this: I want to be able to copy a series of bytes from my hex editor (as hex bytes) and use a hotkey to trigger a function (or series of functions) which will bit-invert those bytes, then save them as hex bytes back to the clipboard. Once that's done, I can paste them back into my hex editor and see the ASCII representation of the inverted bytes.
The strategy I used to go about this was seemingly simple, but possibly too complex. I'm not sure. I assume up front that the bytes I'm copying are already valid hex bytes, 0-9 and A-F, regardless of case (uppercase everything internally), and ignore whitespace (SPC, TAB, and RET). Convert from hex to a binary value, invert the value, then convert from binary to hex and save to the clipboard.
I'm not terribly familiar with writing functions in AHK; mostly I use it for hotstrings and hotkeys only. I tried my hand at a couple different approaches, none of which worked fully. I also looked at the following code samples on the AHK Community Forums, which seemed really interesting, but also overkill for what I want.
http://www.autohotkey.com/forum/topic21172.htmlhttp://www.autohotkey.com/forum/topic21172-105.htmlI did find some code in AutoIt (v3) that I modified only slightly that works well (at least the one bit I've tested), but the problem is that I don't run AutoIt like I run AutoHotkey, i.e. running in my system tray, capturing key macros, replacing text, etc. I probably can run AutoIt that way (I'd have to look into it), but generally I run AutoIt scripts for specific tasks, and don't really use it the same way I use AutoHotkey. While I could just write hotkeys in AutoHotkey to call the AutoIt scripts, that gets rid of the idea of having this be self-contained in my standard AHK script, and complicates deployment.
Any ideas? I'm open to all approaches, as long as I can make sense of the code.
Thanks in advance!
Here's the edited AutoIt code (as it stands right now):
; baseconv.au3 -- Base conversion functions for representative strings (binary
; strings, decimal values, hex values).
; ==============================================================================
; FUNCTIONS
; ==============================================================================
;
; Original code for functions by "ptrex" on AutoIt Forums
; http://www.autoitscript.com/forum/index.php?showtopic=70507
; --------------------------------------------------------------------------
; BinStr2Dec
; --------------------------------------------------------------------------
$lngResult = 0
; do nothing
$lngResult = $lngResult + (2 ^ (StringLen($strBin)-$intIndex)) ; invalid binary digit, so the whole thing is invalid
$lngResult = 0
$intIndex = 0 ; stop the loop
$Return = $lngResult
MsgBox(0,"Error","Wrong input, try again ...")
; --------------------------------------------------------------------------
; Dec2BinStr
; --------------------------------------------------------------------------
$i = 1
$x = 16^$i
$i +=1
; Determine the Octects
$sBinChar &= "1"
$sBinChar &= "0"
MsgBox(0,"Error","Wrong input, try again ...")
; --------------------------------------------------------------------------
; Hex2Dec
; --------------------------------------------------------------------------
Local $Temp, $i, $Pos, $Ret, $Output
$Pos = $Temp[0] - $i
$Ret = Dec (Hex ("0x" & $temp[$i] )) * 16 ^ $Pos $Output = $Output + $Ret
MsgBox(0,"Error","Wrong input, try again ...")
; --------------------------------------------------------------------------
; Dec2Hex
; --------------------------------------------------------------------------
$Input = $Input/16
MsgBox(0,"Error","Wrong input, try again ...")
; --------------------------------------------------------------------------
; BinStr2Hex
; --------------------------------------------------------------------------
Local $test, $Result = '',$numbytes,$nb
Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111"
SetError(-1);non binary character detected
#region make
binary string an integral multiple of
4 characters
$strBin = '0' & $strBin
#endregion make
binary string an integral multiple of
4 characters
Dim $bytes[$numbytes],$Deci[$numbytes] For $j = 0 to $numbytes - 1;for each byte ;extract the next byte
;find what the dec value of the byte is
for $k = 0 to 15;for all the 16 possible hex values if $bytes[$j] = $bits[$k+1] Then $Deci[$j] = $k
;now we have the decimal value for each byte, so stitch the string together again
$Result = ''
for $l = 0 to $numbytes - 1 $Result &= Hex($Deci[$l],1) MsgBox(0,"Error","Wrong input, try again ...")
; --------------------------------------------------------------------------
; Hex2BinStr
; --------------------------------------------------------------------------
Local $Allowed = '0123456789ABCDEF'
Local $bits = "0000|0001|0010|0011|0100|0101|0110|0111|1000|1001|1010|1011|1100|1101|1110|1111" $Result &= $bits[Dec($strHex[$n])+1]
; ==============================================================================
; TEST CODE
; ==============================================================================
; Quick and drity, not good test code, but works for now.
; Currently only tests Hex2BinStr.
Dim $returnVal1 = Hex2BinStr
($testVal1) MsgBox(0,"Results",$returnVal1) ; Expected value: 1101111010101101
Dim $returnVal2 = Hex2BinStr
($testVal2) MsgBox(0,"Results",$returnVal2) ; Expected value: 0010000101010010
Dim $returnVal3 = Hex2BinStr
($testVal3) MsgBox(0,"Results",$returnVal3) ; Expected value: 1011010101111011