Page 1 of 1

How to Convert PeekS Value

Posted: Tue Oct 31, 2023 6:59 am
by Maya
Good morning,
I'm building a simple base calculator, and trying to read a Hex part from the current process memory.
How can I read or convert the debugged Binary Unicode Values into Hex?

Note that I can do this by the code [ RSet(Hex(PeekA(@buffer+i)), 2,"0") ] but it will be a long way in case of looping.
If PeekS is not the right function, then which one I need to use?
Thank you!

Code: Select all

ReadProcessMemory_(GetCurrentProcess_() , Address, @buffer, 8, @BytesRead)
Debug PeekS(@buffer, 8) 
Image

Re: How to Convert PeekS Value

Posted: Tue Oct 31, 2023 7:33 am
by STARGÅTE
In Pure Basic, there is no function to read binary data as a string, as strings are always truncated after null.
Looping over all bytes (or integers, 8 byte) by hand is the only way, but you can write a simple function by yourself.

Re: How to Convert PeekS Value

Posted: Tue Oct 31, 2023 9:20 am
by Maya
Is there another function in PureBasic that find in Memory, more than One Hex Byte?

Re: How to Convert PeekS Value

Posted: Tue Oct 31, 2023 11:13 am
by STARGÅTE
What is a "One Hex Byte"?
What do you mean with "find in Memory"?
What do you want to do?

Re: How to Convert PeekS Value

Posted: Tue Oct 31, 2023 11:49 am
by Oso
Maya wrote: Tue Oct 31, 2023 9:20 am Is there another function in PureBasic that find in Memory, more than One Hex Byte?
Welcome to the forum Maya. If speed is the concern, as you say you want to avoid looping, the below may be faster. It avoids using Peek and advances through the memory by a unicode character at a time. This example completes in 1ms and that's with the debugger on. It assumes the contents are stored as Unicode, since that is PureBasic's default for strings.

Code: Select all

OpenConsole()

Define str.s = "This serves as an example of memory contents"
Define *strpointer.Unicode

*strpointer = @str 
While *strpointer\u                                                     ; Strings being null terminated, will end here
  Print(RSet(Hex(*strpointer\u), 2, "0" ))                              ; Convert value to hex
  *strpointer + SizeOf(Unicode)                                         ; Advance the pointer
Wend   

Input()

ASCII version — If you are instead reading ASCII data, therefore single bytes, this would need to be modified, as per this second example.

Code: Select all

OpenConsole()

Define *str = Ascii("This serves as an example of memory contents")     ; Store string as ASCII, *str is pointer to contents
Define *strpointer.Ascii

*strpointer = *str
While *strpointer\a                                                     ; Strings being null terminated, will end here
  Print(RSet(Hex(*strpointer\a), 2, "0" ))                              ; Convert value to hex
  *strpointer + SizeOf(ascii)                                           ; Advance the pointer by 1 byte (ASCII)
Wend   

Input()

Re: How to Convert PeekS Value

Posted: Tue Oct 31, 2023 5:05 pm
by juergenkulow

Code: Select all

s.s="Is there another function in PureBasic that find in Memory, more than One Hex Byte?"

Procedure.s Hexdump(*ptr.Ascii,len.i)
  Protected s.s=Space(len*2)
  Protected *p.Unicode=@s
  Protected i=len
  Protected j
  While i>0
    j=*ptr\a/$10
    If j<10
      *p\u='0'+j
    Else 
      *p\u='A'+j-10
    EndIf 
    *p+2
    j=*ptr\a%$10
    If j<10
      *p\u='0'+j
    Else 
      *p\u='A'+j-10
    EndIf     
    *p+2
    *ptr+1
    i-1 
  Wend 
  ProcedureReturn s
EndProcedure

*Buffer=@s
length=Len(s)*2
Debug FindString(Hexdump(*Buffer,length),"200061006E")/2
ShowMemoryViewer(*Buffer,32)
; 16
; 000000000071B700  49 00 73 00 20 00 74 00 68 00 65 00 72 00 65 00  I.s. .t.h.e.r.e.
; 000000000071B710  20 00 61 00 6E 00 6F 00 74 00 68 00 65 00 72 00   .a.n.o.t.h.e.r.

Re: How to Convert PeekS Value

Posted: Wed Nov 01, 2023 5:36 am
by Maya
Dears, Oso, juergenkulow & Stargåte,
Problem solved.
Thank you for your great assistance.