Jump to content

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

Binary to Hex string with dividers for display/debug


  • Please log in to reply
No replies to this topic
Michael@Oz
  • Members
  • 234 posts
  • Last active: Dec 30 2011 11:24 PM
  • Joined: 08 Nov 2009
I imaging there are a few of there around, but sharing is good.

Handy with tooltips for debugging.

VarSetCapacity(btn, 32, 0)
DllCall("ReadProcessMemory", "int64", hProc, "int64", pRB, "int64", &btn, "int64", 32, "int64", &bread, "int64") ; example only, don't try this
dwData := Numget(btn,16,"int64")
b2:=Bin2Hex(btn,32,32)
ToolTip, b2: %b2%

Enjoy


Bin2Hex(ByRef Bin,Len,Chunks=32, Div="|") {
	; Bin2Hex - Convert Binary data in a variable to hex, good for displaying data from a DLL call
	;	Bin   = The data,
	;	Len	  = The size in bytes
	;	Chunks= How many bits to insert a divider, set to zero for no divider
	;	Div	  = Character/String to divide chunks
	; Returns - String with Hex characters and chunks dividers
	;	e.g.  - "EC010100|00000000|0404000|60EB6D04"
	bytes:=Chunks/8
	strint:=A_FormatInteger
	SetFormat, IntegerFast, H
	line:=""
	Loop, %Len% {
		chunk:=NumGet(Bin,A_Index-1, "UChar") 
		chunk:=SubStr("00" . SubStr(chunk,3), -1) 
		if (!Mod(A_Index-1,bytes) && Chunks!=0)
			line.=Div
		line.=chunk
	}
	SetFormat, IntegerFast, %strint%
	Return line
}