Page 1 of 1
Convert To Hex
Posted: Tue Nov 05, 2019 9:07 pm
by swhite
Hi
Is there a function PureBasic that converts a memory buffer to hex. For example let's say that a memory buffer contains 100 bytes. Is there a function that would return the 200 byte hex representation of the 100 binary bytes? Something like PeekH(*Buffer,100)
I know I can loop through the 100 bytes and use the Hex() function but I am looking for a fast method to convert memory buffers into hex.
Thanks
Simon
Re: Convert To Hex
Posted: Tue Nov 05, 2019 9:31 pm
by kenmo
swhite wrote:Is there a function PureBasic that converts a memory buffer to hex
Nope, sorry
swhite wrote:I know I can loop through the 100 bytes and use the Hex() function but I am looking for a fast method to convert memory buffers into hex.
This should be faster than many calls to Hex(), but the Assembly experts here can surely beat it:
Code: Select all
Procedure.s MemoryToHexString(*Memory, Bytes.i, Lowercase.i = #False)
Protected Result.s
If (*Memory And (Bytes > 0))
If (Lowercase)
Lowercase = 'a' - 'A'
EndIf
Result = Space(Bytes * 2)
Protected *Read.BYTE = *Memory
Protected *Write.CHARACTER = @Result
Protected Value.i
While (*Write\c)
Value = (*Read\b >> 4) & $0F
Select (Value)
Case 0 To 9
*Write\c = '0' + Value
Case 10 To 15
*Write\c = 'A' + Value - 10 + Lowercase
EndSelect
*Write + SizeOf(CHARACTER)
Value.i = *Read\b & $0F
Select (Value)
Case 0 To 9
*Write\c = '0' + Value
Case 10 To 15
*Write\c = 'A' + Value - 10 + Lowercase
EndSelect
*Write + SizeOf(CHARACTER)
*Read + 1
Wend
EndIf
ProcedureReturn (Result)
EndProcedure
test.l = $aa22cc44
Debug MemoryToHexString(@test, 4)
Re: Convert To Hex
Posted: Tue Nov 05, 2019 11:51 pm
by Kurzer
Hello, Simon,
in the german forum there was a similar thread with some example codes:
https://www.purebasic.fr/german/viewtop ... =8&t=29368
Remark: Don't use the first code in the thread.

Re: Convert To Hex
Posted: Wed Nov 06, 2019 2:00 am
by swhite
This method can do 100,000 iteration in 550ms on my computer whereas the code above takes 777ms.
Code: Select all
*Buffer = AllocateMemory(22)
PokeQ(*Buffer,$e17d9f6604a70040)
PokeQ(*Buffer+8,$0082022000950500)
PokeQ(*Buffer+16,$000000005f000000)
Procedure.s NeoToHex(*tnBuf,tnLen.i)
Define ln.i,lcHex.s
For ln = 0 To tnLen - 1
lcHex = lcHex +RSet(Hex(PeekA(*tnBuf + ln),#PB_Ascii),2,"0")
Next
ProcedureReturn lcHex
EndProcedure
For ln =1 To 100000
NeoToHex(*Buffer,21)
Next
Re: Convert To Hex
Posted: Wed Nov 06, 2019 8:02 am
by wilbert
swhite wrote:I know I can loop through the 100 bytes and use the Hex() function but I am looking for a fast method to convert memory buffers into hex.
Here's an asm optimized module I did a few years ago. It should be fast.
viewtopic.php?p=486830#p486830
Re: Convert To Hex
Posted: Wed Nov 06, 2019 9:38 am
by Mijikai
Im sure wilberts solution is the fastest
Anyway here is a lib that i wrote some time ago:
https://www.purebasic.fr/german/viewtop ... =8&t=31304
Re: Convert To Hex
Posted: Wed Nov 06, 2019 11:29 am
by Joris
Thanks for that (updating isn't it ?) hexdata coding Wilbert.
Great stuff again.
Re: Convert To Hex
Posted: Wed Nov 06, 2019 12:00 pm
by wilbert
Joris wrote:Thanks for that (updating isn't it ?) hexdata coding Wilbert.
Great stuff again.
Thanks.
I only updated the example today and changed the labels to local labels. The rest of the code is still the same as a few years ago.
Re: Convert To Hex
Posted: Wed Nov 06, 2019 5:52 pm
by Psychophanta
I have these here stored in my collection:
Code: Select all
Procedure DecByteSequenceToHexByteString(*seq.ascii,length.l,*HexString.ascii)
;This function converts a byte sequence stored in a given base address of sequential memory to a Hex string (.s):
For t.l=1 To length.l
*HexString\a=Asc(Hex((*seq\a>>4)&$0F)); <-HighNibble
*HexString+1
*HexString\a=Asc(Hex(*seq\a&$0F)); <-LowNibble
*HexString+1
*seq+1
Next
*HexString\a=0
EndProcedure
Procedure.b HexByteStringToDecByteSequence(HexString.s,*seq.ascii)
;This function converts a given Hex string (.s) to a byte sequence stored in a given base address of sequential memory:
Protected c.a,Nibbles.l=Len(HexString.s),t.l
If Nibbles.l&1:HexString.s="0"+HexString.s:Nibbles.l+1:EndIf
For t.l=1 To Nibbles.l
c.a=Asc(LCase(Mid(HexString.s,t.l,1)))
If c<'0' Or c>'f' Or (c>'9' And c<'a')
ProcedureReturn 0
EndIf
If c>'9':c=(c&$5F)-$07:EndIf
c-'0'
If t.l&1
*seq\a=c<<4
Else
*seq\a|c
*seq+1
EndIf
Next
ProcedureReturn 1
EndProcedure
;Test it:
*d.byte=AllocateMemory(22)
HexByteStringToDecByteSequence("e153a9a6449b16d0bd950fe6b9e762b43cdabbe4ec0193505822854c109029e9",*d.byte)
For t=1 To 22
Debug Hex(PeekB(*d.byte+t-1)&$FF)
Next
st.s=Space(22*2)
DecByteSequenceToHexByteString(*d.byte,22,@st.s)
Debug st.s
And in i386 assembler this one:
Code: Select all
Procedure StringToHex(cPuntero.l,cCantidad.l,cBuffer.l)
;This function converts String to hex sequence:
!pushad
!mov esi,dword[p.v_cPuntero+32]
!mov edi,dword[p.v_cBuffer+32]
!.bucle:
!cmp dword[p.v_cCantidad+32],0
!je .salir
!xor edx,edx
!movzx eax,byte[esi]
!mov ebx,16
!div ebx
!mov bl, byte[numeros+eax]
!mov byte[edi],bl
!mov bl, byte[numeros+edx]
!mov byte[edi+1],bl
!add edi,2
!inc esi
!dec dword[p.v_cCantidad+32]
!jmp .bucle
!.salir:
!popad
ProcedureReturn
!numeros db '0123456789ABCDEF',0
EndProcedure
;Test it:
ff$="* PureBasic *"
vv$=Space(Len(ff$)*2)
StringToHex(@ff$,Len(ff$),@vv$)
Debug vv$
Re: Convert To Hex
Posted: Wed Nov 06, 2019 9:11 pm
by swhite
Hi
Using Wilbert's HexData function is much faster. It only took 21ms to do 100,000 iterations which is about 25 times faster than my method. So thanks again Wilbert for your help.
Simon
Re: Convert To Hex
Posted: Thu Nov 07, 2019 8:39 am
by wilbert
I added a unicode only version to my code which might perform better on recent PureBasic versions for Windows and Linux.
viewtopic.php?p=486830#p486830
Re: Convert To Hex
Posted: Thu Nov 07, 2019 7:17 pm
by mk-soft
PeekH and PokeH from features request...
Thanks Wilbert for nice Idee
Code: Select all
;-TOP
; ----
Global Dim PeekHexList.l(255)
Global Dim PokeByteList.a(255)
Procedure InitHexList()
Protected i.i
For i = 0 To 255
PokeS(@PeekHexList(i), RSet(Hex(i), 2, "0"), 2, #PB_String_NoZero)
Next
EndProcedure
InitHexList()
Procedure InitByteList()
Protected i.i, c.i
For i = '0' To '9'
PokeByteList(i) = c
c + 1
Next
c = 10
For i = 'A' To 'F'
PokeByteList(i) = c
c + 1
Next
c = 10
For i = 'a' To 'f'
PokeByteList(i) = c
c + 1
Next
EndProcedure
InitByteList()
; ----
Procedure.s PeekH_PB(*Buffer.Ascii, Len)
Protected r1.s, index
len - 1
For index = 0 To Len
r1 + RSet(Hex(*Buffer\a, #PB_Byte), 2, "0")
*Buffer + 1
Next
ProcedureReturn r1
EndProcedure
; ----
Procedure.s PeekH(*Buffer.Ascii, Len)
Protected Dim Result.l(Len)
Protected *Result.Long = @Result()
While Len
Len - 1
*Result\l = PeekHexList(*Buffer\a)
*Result + 4
*Buffer + 1
Wend
ProcedureReturn PeekS(@Result())
EndProcedure
; ----
Procedure PokeH_PB(*Buffer.Ascii, String.s)
Protected i, Len
len = Len(String)
For i = 1 To len Step 2
*Buffer\a = Val("$"+Mid(String, i, 2))
*Buffer + 1
Next
EndProcedure
; ----
Procedure PokeH(*Buffer.Ascii, String.s)
Protected *String.unicode = @String
While *String\u
If *String\u < 256
*Buffer\a = PokeByteList(*String\u) << 4
EndIf
*String + 2
If *String\u
If *String\u < 256
*Buffer\a | PokeByteList(*String\u)
EndIf
Else
Break
EndIf
*String + 2
*buffer + 1
Wend
EndProcedure
; ----
;-Test
Define value.q
value.q = $01020304A1B2C3D4
start = ElapsedMilliseconds()
For i = 1 To 1000000
t1.s = PeekH_PB(@value, 8)
Next
ende = ElapsedMilliseconds()
m1.s + "Time PeekH_PB = " + Str(ende - start) + "ms " + t1 + #LF$
start = ElapsedMilliseconds()
For i = 1 To 1000000
t1.s = PeekH(@value, 8)
Next
ende = ElapsedMilliseconds()
m1.s + "Time PeekH = " + Str(ende - start) + "ms " + t1 + #LF$
value = 0
start = ElapsedMilliseconds()
For i = 1 To 1000000
PokeH_PB(@value, "11223344A1B2C3D4")
Next
ende = ElapsedMilliseconds()
m1.s + "Time PokeH_PB = " + Str(ende - start) + "ms " + Hex(value) +#LF$
value = 0
start = ElapsedMilliseconds()
For i = 1 To 1000000
PokeH(@value, "11223344A1B2C3D4")
Next
ende = ElapsedMilliseconds()
m1.s + "Time PokeH = " + Str(ende - start) + "ms " + Hex(value) +#LF$
MessageRequester("Time", m1)