Strange issue with IF and Peek

Just starting out? Need help? Post your questions and find answers here.
dibor
Enthusiast
Enthusiast
Posts: 160
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Strange issue with IF and Peek

Post by dibor »

Hello gung.

I broke my head with this problem and I can't understand where I did wrong.

Code: Select all

*Buffer = AllocateMemory(2048)

If CreateNetworkServer(0, 2237, #PB_Network_UDP)
    Debug "Server started on port 2237"
  Repeat
    Event = NetworkServerEvent()
    If Event
      ClientID = EventClient()
      If Event = #PB_NetworkEvent_Data
        Size = ReceiveNetworkData(ClientID, *Buffer, 1024)
        If Size > 4
            
            Debug "Got Long: " + Hex(PeekL(*Buffer), #PB_Long)
            If PeekL(*Buffer) = $DABCCBAD  ; Little-endian порядок
                Debug "Got right packet (Little-endian)"
            ElseIf PeekL(*Buffer) = $ADBCCBDA  ; Big-endian порядок
                Debug "Got right packet (Big-endian)"
            EndIf
            
            Debug "Got: " + Hex(PeekL(*Buffer), #PB_Long)
            Debug "Got: " + Hex(PeekU(*Buffer), #PB_Byte)
            Debug "Got: " + Hex(PeekU(*Buffer+1), #PB_Byte)
            Debug "Got: " + Hex(PeekU(*Buffer+2), #PB_Byte)
            Debug "Got: " + Hex(PeekU(*Buffer+3), #PB_Byte)
            If (PeekU(*Buffer) = $AD And PeekU(*Buffer + 1) = $BC And PeekU(*Buffer + 2) = $CB And PeekU(*Buffer + 3) = $DA)
                Debug "Got right packet"
            EndIf

        EndIf
      EndIf
    EndIf
  Until Event = #PB_NetworkEvent_Disconnect
  CloseNetworkServer(0)
Else
  MessageRequester("Error", "Can not create server!")
EndIf

FreeMemory(*Buffer)
I got output in the debugger window:
Got Long: DACBBCAD
Got: DACBBCAD
Got: AD
Got: BC
Got: CB
Got: DA

So, expression (PeekU(*Buffer) = $AD And PeekU(*Buffer + 1) = $BC And PeekU(*Buffer + 2) = $CB And PeekU(*Buffer + 3) = $DA) and PeekL(*Buffer) = $ADBCCBDA is every time is false :(
Mac Studio M1Max, PB 6.12 Arm64 and x64.
Macbook Air M2, PB 6.12 Arm64 and x64.
Windows 10, PB 6.12 x64 and x86.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Strange issue with IF and Peek

Post by mk-soft »

Comparisons are always integer (signed)

PeekL(x) delivers Signed Long becomes the Signed Integer.

Code: Select all

Structure ArrayOfByte
  StructureUnion
    b.b[0]
    a.a[0]
  EndStructureUnion
EndStructure

*buffer.ArrayOfByte = AllocateMemory(16)
*buffer\a[0] = $DA
*buffer\a[1] = $CB
*buffer\a[2] = $BC
*buffer\a[3] = $AD

Macro ULONG(X)
  ($FFFFFFFF & X)
EndMacro

Debug "Got Long: " + Hex(PeekL(*buffer), #PB_Long)
Debug "Signed PeekL = " + Str(PeekL(*buffer)) 
Debug "Unsigned PeekL = " + Str(ULONG(PeekL(*buffer)))
Debug "Const = " + $DABCCBAD ; Integer
Debug "Const = " + $ADBCCBDA ; Integer

Macro PeekUL(X)
  (PeekL(x) & $FFFFFFFF)
EndMacro

If PeekUL(*Buffer) = $DABCCBAD  ; Little-endian порядок
  Debug "Got right packet (Little-endian)"
ElseIf PeekUL(*Buffer) = $ADBCCBDA  ; Big-endian порядок
  Debug "Got right packet (Big-endian)"
EndIf
Unfortunately, there is no Unsigned Long under PB

P.S.

It is better to work with structures. The peek and poke functions are basic like and useful, but with structures easier to access data.

Code: Select all

Structure ArrayOfByte
  StructureUnion
    b.b[0]
    a.a[0]
  EndStructureUnion
EndStructure

*buffer.ArrayOfByte = AllocateMemory(16)
*buffer\a[0] = $DA
*buffer\a[1] = $CB
*buffer\a[2] = $BC
*buffer\a[3] = $AD

; ----

Structure strReceiveData
  Id.l
  Dat.a[0]
EndStructure

*recv.strReceiveData = *buffer

If *recv\Id = $DABCCBAD  ; Little-endian порядок
  Debug "Got right packet (Little-endian)"
ElseIf *recv\Id = $ADBCCBDA  ; Big-endian порядок
  Debug "Got right packet (Big-endian)"
EndIf
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
dibor
Enthusiast
Enthusiast
Posts: 160
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Strange issue with IF and Peek

Post by dibor »

In other words I can NOT to use long and quad in the comparisons?
Mac Studio M1Max, PB 6.12 Arm64 and x64.
Macbook Air M2, PB 6.12 Arm64 and x64.
Windows 10, PB 6.12 x64 and x86.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Strange issue with IF and Peek

Post by mk-soft »

X64:
A signed long (32bit) is converted from a signed long (32bit) to a signed integer (64bit).

Through a binary filter $FFFFFFFF you can view the long part as unsigned
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
dibor
Enthusiast
Enthusiast
Posts: 160
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Strange issue with IF and Peek

Post by dibor »

I do not big programmer but doesn't saw this restriction in the comparisons in any other languages :(

Compiler do not gave any warnings or errors - simple doesn't work.
Mac Studio M1Max, PB 6.12 Arm64 and x64.
Macbook Air M2, PB 6.12 Arm64 and x64.
Windows 10, PB 6.12 x64 and x86.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Strange issue with IF and Peek

Post by mk-soft »

Edit

In other languages, the type must also be specified.

Hard to explain ...

With PureBasic, the type is automatically adjusted.

1. PeekL returns a signed value. So a type adjustment from signed long to signed integer, so that an assignment to an integer variable is correct.

2. A $[HEX VALUE] is always an integer, as no type is specified here. In your case a positive value.

3. A comparison is standard of type Integer.

4. However, if the first variable in a comparison is of type Long, the following comparison values are adjusted to Long.

Edit

Code: Select all

Structure ArrayOfByte
  StructureUnion
    b.b[0]
    a.a[0]
  EndStructureUnion
EndStructure

*buffer.ArrayOfByte = AllocateMemory(16)
*buffer\a[0] = $DA
*buffer\a[1] = $CB
*buffer\a[2] = $BC
*buffer\a[3] = $AD

longVal.l = PeekL(*buffer)
If longVal = $DACBBCAD  ; Little-endian порядок
  Debug "Got right packet (Little-endian)"
ElseIf longVal = $ADBCCBDA  ; Big-endian порядок
  Debug "Got right packet (Big-endian)"
EndIf

*buffer\a[0] = $AD
*buffer\a[1] = $BC
*buffer\a[2] = $CB
*buffer\a[3] = $DA

longVal.l = PeekL(*buffer)
If longVal = $DACBBCAD  ; Little-endian порядок
  Debug "Got right packet (Little-endian)"
ElseIf longVal = $ADBCCBDA  ; Big-endian порядок
  Debug "Got right packet (Big-endian)"
EndIf
Last edited by mk-soft on Sun Jun 29, 2025 10:15 am, edited 2 times in total.
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
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Strange issue with IF and Peek

Post by mk-soft »

Post edited above :wink:
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
User avatar
HeX0R
Addict
Addict
Posts: 1187
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Strange issue with IF and Peek

Post by HeX0R »

no one saw the main error in the initial code?

Code: Select all

If (PeekA(*Buffer) = $AD And PeekA(*Buffer + 1) = $BC And PeekA(*Buffer + 2) = $CB And PeekA(*Buffer + 3) = $DA)
  Debug "Got right packet"
EndIf
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Strange issue with IF and Peek

Post by NicTheQuick »

Why do you wait for more than 4 bytes if you only need 4?

Code: Select all

If Size > 4
Do you always send more than 4?
And why do yo ignore the data if less was received? You should buffer it until it reaches 4 bytes.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Strange issue with IF and Peek

Post by infratec »

And ...

Code: Select all

            If PeekL(*Buffer) = $DABCCBAD  ; Little-endian порядок
                Debug "Got right packet (Little-endian)"
            ElseIf PeekL(*Buffer) = $ADBCCBDA  ; Big-endian порядок
                Debug "Got right packet (Big-endian)"
            EndIf
Little endian: DABCCBAD
Big endian: ADCBBCDA and not ADBCCBDA

The comparrison with PeekL() is wrong.
dibor
Enthusiast
Enthusiast
Posts: 160
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Strange issue with IF and Peek

Post by dibor »

HeX0R wrote: Sat Jun 28, 2025 2:48 pm no one saw the main error in the initial code?

Code: Select all

If (PeekA(*Buffer) = $AD And PeekA(*Buffer + 1) = $BC And PeekA(*Buffer + 2) = $CB And PeekA(*Buffer + 3) = $DA)
  Debug "Got right packet"
EndIf
Are U RIGHT!!
PeekA work!
Mac Studio M1Max, PB 6.12 Arm64 and x64.
Macbook Air M2, PB 6.12 Arm64 and x64.
Windows 10, PB 6.12 x64 and x86.
dibor
Enthusiast
Enthusiast
Posts: 160
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Strange issue with IF and Peek

Post by dibor »

NicTheQuick wrote: Sat Jun 28, 2025 6:33 pm Why do you wait for more than 4 bytes if you only need 4?

Code: Select all

If Size > 4
Do you always send more than 4?
And why do yo ignore the data if less was received? You should buffer it until it reaches 4 bytes.
Because 4 first bytes are preamble of the right message.
Mac Studio M1Max, PB 6.12 Arm64 and x64.
Macbook Air M2, PB 6.12 Arm64 and x64.
Windows 10, PB 6.12 x64 and x86.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Strange issue with IF and Peek

Post by mk-soft »

infratec wrote: Sat Jun 28, 2025 7:29 pm And ...

Code: Select all

            If PeekL(*Buffer) = $DABCCBAD  ; Little-endian порядок
                Debug "Got right packet (Little-endian)"
            ElseIf PeekL(*Buffer) = $ADBCCBDA  ; Big-endian порядок
                Debug "Got right packet (Big-endian)"
            EndIf
Little endian: DABCCBAD
Big endian: ADCBBCDA and not ADBCCBDA

The comparrison with PeekL() is wrong.
The comparison is still wrong because PeekL returns a signed integer.
Thus, the peek value is negative and the hex value is positive
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
Post Reply