Are PureBasic's integer types little or big endian?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Are PureBasic's integer types little or big endian?

Post 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.
Last edited by Quin on Thu Apr 27, 2023 1:32 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post 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
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post by Quin »

Ah, thanks Fred! That's what I figured. Guess my code is bugged, then. :(
ricardo_sdl
Enthusiast
Enthusiast
Posts: 141
Joined: Sat Sep 21, 2019 4:24 pm

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

Post 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()
You can check my games at:
https://ricardo-sdl.itch.io/
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

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

Post 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
Last edited by boddhi on Tue Jun 27, 2023 1:20 am, edited 1 time in total.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

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

Post by Quin »

Oh wow, this is awesome, thanks you two! :)
Post Reply