How to get the binary or hex code of any file?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

How to get the binary or hex code of any file?

21 Sep 2017, 22:26

Is it possible to store the binary or hex code of any file into a variable?

Binary (0 and 1)

Hex (0 to 9 and A to F)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to get the binary or hex code of any file?

22 Sep 2017, 06:15

Here is an example of reading/writing binary using the AutoHotkey exe.

Code: Select all

q:: ;read/write binary files
;note: to copy any file you can just use FileCopy
;based on code from here:
;conversion logic, v1 = -> v1 := -> v2, two-way compatibility - Page 5 - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=27069&p=169780#p169780

vPath := A_AhkPath
oFile := FileOpen(vPath, "r")
oFile.Pos := 0
vSize := oFile.Length
oFile.RawRead(vData, oFile.Length)
oFile.Close()

vPath = %A_Desktop%\z ahk exe copy %A_Now%.exe
if FileExist(vPath)
	return
oFile := FileOpen(vPath, "w")
oFile.RawWrite(vData, vSize)
oFile.Close()
return
See my JEE_BinDataToHex/JEE_HexToBinData functions here:
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596

For hex to bin numbers / bin to hex numbers:

Code: Select all

q:: ;hex to bin numbers
vHex := "0123456789ABCDEF"
vList := "0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,1011,1100,1101,1110,1111"
vBin := vHex
Loop, Parse, vList, % ","
	vBin := StrReplace(vBin, Format("{:X}", A_Index-1), A_LoopField)
MsgBox, % vBin
MsgBox, % LTrim(vBin, "0")
return

w:: ;bin to hex numbers
vBin := "0000000100100011010001010110011110001001101010111100110111101111"
oArray := {"0000":0,"0001":1,"0010":2,"0011":3,"0100":4,"0101":5,"0110":6,"0111":7,"1000":8,"1001":9,"1010":"A","1011":"B","1100":"C","1101":"D","1110":"E","1111":"F"}
;prepend zeros to ensure the length is a multiple of 4
vPfx := SubStr("000", Mod(StrLen(vBin)+3, 4)+1)
vBin := RTrim(RegExReplace(vPfx vBin, "....", "$0,"), ",")
vHex := ""
VarSetCapacity(vHex, StrLen(vBin)/2)
Loop, Parse, vBin, % ","
	vHex .= oArray["" A_LoopField]
MsgBox, % vHex
MsgBox, % LTrim(vHex, "0")
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to get the binary or hex code of any file?

22 Sep 2017, 08:47

so, is it not possible to read any type of file and save its binary (0 and 1) or Hex (0 to 9 and A to F) code directly into a variable?

by using RawRead(), long Chinese characters string are stored into the variable (整瑳汬汬汬 ...)

then, by using RawWrite(), the original unicode or ansi characters are stored into the file instead the file's binary or hex code!
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: How to get the binary or hex code of any file?

25 Sep 2017, 12:32

Can someone here tell me why the script returns Chinese characters instead the bin or the hex code of "test.txt" file?

Note that, the "test.txt" file only contains Ansi characters, does not contain any Chinese characters!
Read Bin.png
Read Bin.png (21.9 KiB) Viewed 2467 times
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to get the binary or hex code of any file?

05 Dec 2017, 04:48

- In that 'gui, add, edit' line, if run in Unicode versions of AHK, it will regard that data as a UTF-16 LE string, and display text accordingly.
- To read text from a text file, use FileRead.
- To read text from data, use StrGet.
- To display binary data as a hex string, you could use my function above.
- To display binary data as 0s and 1s, convert it to a hex string, and then to 0s and 1s, by using StrReplace/RegExReplace.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], jeves, mikeyww, Rohwedder, septrinus and 290 guests