Convert To Hex
Convert To Hex
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
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
Simon White
dCipher Computing
dCipher Computing
Re: Convert To Hex
Nope, sorryswhite wrote:Is there a function PureBasic that converts a memory buffer to hex
This should be faster than many calls to Hex(), but the Assembly experts here can surely beat it: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.
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
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.
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.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
"Happiness is a pet." | "Never run a changing system!"
Re: Convert To Hex
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
Simon White
dCipher Computing
dCipher Computing
Re: Convert To Hex
Here's an asm optimized module I did a few years ago. It should be fast.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.
viewtopic.php?p=486830#p486830
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Convert To Hex
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
Anyway here is a lib that i wrote some time ago:
https://www.purebasic.fr/german/viewtop ... =8&t=31304
Re: Convert To Hex
Thanks for that (updating isn't it ?) hexdata coding Wilbert.
Great stuff again.
Great stuff again.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Re: Convert To Hex
Thanks.Joris wrote:Thanks for that (updating isn't it ?) hexdata coding Wilbert.
Great stuff again.
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.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
- Psychophanta
- Addict
- Posts: 4997
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Lípetsk, Russian Federation
- Contact:
Re: Convert To Hex
I have these here stored in my collection:
And in i386 assembler this one:
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
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$
http://www.zeitgeistmovie.com
While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB
While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB
Re: Convert To Hex
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
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
Simon White
dCipher Computing
dCipher Computing
Re: Convert To Hex
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
viewtopic.php?p=486830#p486830
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Convert To Hex
PeekH and PokeH from features request...
Thanks Wilbert for nice Idee
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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive