Display_Memory() function, usefull for debugging

Share your advanced PureBasic knowledge/code with the community.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Display_Memory() function, usefull for debugging

Post by newbie »

Hi,

I have done a simple procedure to display the content of any memory you want, it can be a structure, a buffer allocated with AllocateMemory, a packet received with ReceiveNetworkData, etc...

Code: Select all

;Function hex2dec from 'PB' from purebasic forum
Procedure.l hex2dec(h$)
    h$=UCase(h$)
    For r=1 To Len(h$)
        d<<4 : a$=Mid(h$,r,1)
        If Asc(a$)>60
            d+Asc(a$)-55
        Else
            d+Asc(a$)-48
        EndIf
    Next
    ProcedureReturn d
    
EndProcedure 

Procedure Display_Memory(*Buffer, NBstart.l, NBend.l)
    
    sniffer.s = ""
    chr1.s = ""
    chr2.s = ""
    string.s = ""
    cnt.b = 0
    For i = NBstart To NBend - 1 Step 2
        ;/ Hex format
        chr1 = Hex(PeekB(*Buffer+i) & $FF) 
        If Len(chr1) = 1
            chr1 = "0" + chr1
        EndIf
        chr2 = Hex(PeekB(*Buffer+i+1) & $FF) 
        If Len(chr2) = 1
            chr2 = "0" + chr2
        EndIf
        sniffer = sniffer + chr1 + chr2 + " "
        
        ;/ String format
        If hex2dec(chr1) = 0
            string + "."
        Else
            string + Chr(hex2dec(chr1))
        EndIf
        If hex2dec(chr2) = 0
            string + "."
        Else
            string + Chr(hex2dec(chr2))
        EndIf
        
        cnt + 1
        
        If cnt = 8
            sniffer = sniffer + "       " + string + Chr(13) + Chr(10)
            string = ""
            cnt = 0
        EndIf
        
    Next
    For i = cnt To 8
        sniffer + "  "
    Next i
    sniffer = sniffer + "        " + string + Chr(13) + Chr(10)
    string = ""
    
; if you want to return the result in a string or text gadget
    SetGadgetText(#yourgadget, sniffer)
    
EndProcedure
to use it, simply call it like this (for instance) :

Code: Select all

.
.
.
#BUFFER_SIZE = 256
*Buffer = AllocateMemory(#BUFFER_SIZE)
PokeS(*Buffer, "65s26g54s2v65e4r26v54e26zr24v6ez54r2v6ez542r6vez4")

Display_Memory(*Buffer, 0, #BUFFER_SIZE)
.
.
.
it will return the content with the following format :
60F9 E400 E02F 8B00 8829 8B00 60FF A300 70FF A300 8829 8B00 0C18 0000 `ùä.à/‹.ˆ)‹.`ÿ£.pÿ£.ˆ)‹...
0000 0000 0100 0000 A4FF E400 60F9 E400 4D00 A300 0100 0000 2B5B 4000 .......¤ÿä.`ùä.M.£....+[@.
60F9 E400 8588 4200 F485 4200 602F 8B00 40FF A300 0000 0000 0000 0000 `ùä.…ˆB.ô…B.`/‹.@ÿ£.........
if you return the result in a gadget, I advise the following font :

Code: Select all

If LoadFont(0,"Lucida console",10) 
  SetGadgetFont(#yourGadget,FontID())  
EndIf 
If you choose another font, the characters won't take the same space (0000 will be shorter in place than CCD7).

It has been very usefull for me for debugging.
- Registered PB user -

Using PB 4.00