It is currently Fri Aug 14, 2020 8:15 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Convert To Hex
PostPosted: Tue Nov 05, 2019 9:07 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 21, 2009 6:56 pm
Posts: 579
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

_________________
Simon White
dCipher Computing


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Tue Nov 05, 2019 9:31 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1839
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:
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)

_________________
On GitHub: PB Includes - IDE Tools - Color Themes - IDE Branches - TabBarGadget Mods


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Tue Nov 05, 2019 11:51 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 11, 2006 12:07 am
Posts: 560
Location: Near Hamburg
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. ;-)

_________________
PB 5.72 x64, OS: Windows 7 Pro x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age: 52y
"Happiness is a pet." | "Never run a changing system!"


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Wed Nov 06, 2019 2:00 am 
Offline
Enthusiast
Enthusiast

Joined: Thu May 21, 2009 6:56 pm
Posts: 579
This method can do 100,000 iteration in 550ms on my computer whereas the code above takes 777ms.

Code:
*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


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Wed Nov 06, 2019 8:02 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3647
Location: Netherlands
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

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Wed Nov 06, 2019 9:38 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 719
Im sure wilberts solution is the fastest :)

Anyway here is a lib that i wrote some time ago:
https://www.purebasic.fr/german/viewtopic.php?f=8&t=31304


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Wed Nov 06, 2019 11:29 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Oct 16, 2009 10:12 am
Posts: 668
Location: BE
Thanks for that (updating isn't it ?) hexdata coding Wilbert.
Great stuff again.

_________________
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Wed Nov 06, 2019 12:00 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3647
Location: Netherlands
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.

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Wed Nov 06, 2019 5:52 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4627
Location: Spa, relaxing and thinking, and learning...
I have these here stored in my collection:
Code:
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Wed Nov 06, 2019 9:11 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 21, 2009 6:56 pm
Posts: 579
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

_________________
Simon White
dCipher Computing


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Thu Nov 07, 2019 8:39 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3647
Location: Netherlands
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

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Convert To Hex
PostPosted: Thu Nov 07, 2019 7:17 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2612
Location: Germany
PeekH and PokeH from features request...

Thanks Wilbert for nice Idee
Code:
;-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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 51 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye