Page 1 of 1

Are PureBasic's integer types little or big endian?

Posted: Thu Apr 27, 2023 1:26 pm
by Quin
Hi all.
I'm currently writing a tool to read binary data from a file, and perform some actions on it. From some hex editing, I've determined that the integers are uint32_ts, and byteorder is little-endian. I don't want to lock this program to 32-bit, and because ReadInteger() reads a different number of bytes based on your compiler, I figured I'd use ReadLong(), because it claims to read 4 bytes. However, is the return of this procedure little or big endian? I (surprisingly) can't find any mentions of endianness mentioned in the PB docs (not even in the variables and datatypes section). I can write a converter if they're big endian, but I was curious if anyone knows?
Thanks in advance!

Edit to clarify that I'm talking about Windows, running on an AMD Ryzen. I assume it's little-endian, but I know some languages are weird about this, and I was also hitting a weird problem with reading longs, but I'm not sure if it's an issue of endianness, that's just my first suspect.

Re: Are PureBasic's integer types little or big endian?

Posted: Thu Apr 27, 2023 1:30 pm
by Fred
It all depends of your processor but all PB program will be little endian as it runs on x86/x64 and arm32/64

Re: Are PureBasic's integer types little or big endian?

Posted: Thu Apr 27, 2023 1:33 pm
by Quin
Ah, thanks Fred! That's what I figured. Guess my code is bugged, then. :(

Re: Are PureBasic's integer types little or big endian?

Posted: Thu Apr 27, 2023 4:00 pm
by ricardo_sdl
I think you can use code like this to test if the current architecture is big or little endian:

Code: Select all

EnableExplicit

Procedure.a IsBigEndian()
  Protected CanaryValue.c = $abba
  Protected TestValue.a = PeekA(@CanaryValue)
  
  If TestValue = $ab
    ProcedureReturn #True
  EndIf
    
  ProcedureReturn #False
EndProcedure

Procedure.a IsLittleEndian()
  ProcedureReturn Bool(Not IsBigEndian())
EndProcedure


Debug IsBigEndian()
Debug IsLittleEndian()

Re: Are PureBasic's integer types little or big endian?

Posted: Thu Apr 27, 2023 4:33 pm
by boddhi

Code: Select all

Procedure.a Fc_ProcessorTest() ; #True if Intel, #False if Motorola
  Protected.w Value ; => 2 bytes variable type needed

  Value=1
  ; 1 will be coded '00 01' under Motorola and '01 00' under Intel
  ; We test first byte
  If PeekB(@Value)=Value:ProcedureReturn #True:EndIf
  ; or 
  ; ProcedureReturn Bool(PeekB(@Value)=Value)
EndProcedure

If Fc_ProcessorTest()
  Debug "Intel/ARM processor"
Else
  Debug "Motorola processor"
Endif

Re: Are PureBasic's integer types little or big endian?

Posted: Thu Apr 27, 2023 5:04 pm
by Quin
Oh wow, this is awesome, thanks you two! :)