How to Convert PeekS Value

Just starting out? Need help? Post your questions and find answers here.
Maya

How to Convert PeekS Value

Post 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
Last edited by Maya on Tue Oct 31, 2023 8:41 am, edited 1 time in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to Convert PeekS Value

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Maya

Re: How to Convert PeekS Value

Post by Maya »

Is there another function in PureBasic that find in Memory, more than One Hex Byte?
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to Convert PeekS Value

Post by STARGÅTE »

What is a "One Hex Byte"?
What do you mean with "find in Memory"?
What do you want to do?
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: How to Convert PeekS Value

Post 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()
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: How to Convert PeekS Value

Post 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.
Maya

Re: How to Convert PeekS Value

Post by Maya »

Dears, Oso, juergenkulow & Stargåte,
Problem solved.
Thank you for your great assistance.
Post Reply