Jump to content

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

Hex <---> Asc (Strings, not just nos.)


  • Please log in to reply
7 replies to this topic
Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
Convert strings from Hex to Asc or the other way round.

Supports the Unicode strings too (5400720061006E0073006C006100740069006F006E00 = T.r.a.n.s.l.a.t.i.o.n.)

If only ahk supported reading binary files (Chris please! ;)) we could write a hex editor in ahk or do a full search/replace in any file, make a byte patcher....

Hex(Inp,UC = 0)
{
	OldFmt = %A_FormatInteger%
	SetFormat, Integer, hex

	Loop, Parse, Inp
	{
		TransForm, Asc, Asc, %A_LoopField%
		Asc += 0
		StringTrimLeft, Hex, Asc, 2
		IfEqual, UC, 0
			Result = %Result%%Hex%
		Else
			Result = %Result%%Hex%00
	}
	SetFormat, Integer, %OldFmt%
	StringUpper, Result, Result

	Return Result
}

Asc(Inp,UC = 0)
{
	StringLen, Len, Inp
	Len /= 2

	OldFmt = %A_FormatInteger%
	SetFormat, Integer, D

	Loop, %Len%
	{
		StringLeft, Hex, Inp, 2
		IfEqual, UC, 0
			StringTrimLeft, Inp, Inp, 2
		Else
			StringTrimLeft, Inp, Inp, 4
		
		Hex = 0x%Hex%
		Hex += 0
		
		TransForm, Chr, Chr, %Hex%
		Result = %Result%%Chr%
	}
	SetFormat, Integer, %OldFmt%

	Return Result
}

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004

If only ahk supported reading binary files ... we could write a hex editor in ahk or do a full search/replace in any file, make a byte patcher....

If you need it soon, the following post demonstrates how to use DllCall to read and write binary files: http://www.autohotke... ... 2389#22389

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
thanx for that post Chris, but thats a very long procedure. by the way is there any other problem with binary files besides the one that ahk doesn't handle null (00) bytes? and can that be solved anytime soon?

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Null bytes are the main problem with binary files (perhaps the only problem). There is no quick or easy solution to that, so it is best to use DllCall for anything that needs binary zeroes in the near term.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
You can find another variants of these here. The last versions of the functions are called String2Hex and Hex2String. The whole package can be downloaded from here.

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
heh! so i redid the wheel!

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
...with Unicode option. Nice touch!

mprost
  • Guests
  • Last active:
  • Joined: --
Hi.

In Asc(), spaces (0x20) in the string are being ignored.

Don't know if this is intentional, but may be avoided changing

Result = %Result%%Chr%

by

Result := Result . Chr

Thanks for the functions, they are very useful!

Cheers,

mprost