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.
Are PureBasic's integer types little or big endian?
Are PureBasic's integer types little or big endian?
Last edited by Quin on Thu Apr 27, 2023 1:32 pm, edited 1 time in total.
Re: Are PureBasic's integer types little or big endian?
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?
Ah, thanks Fred! That's what I figured. Guess my code is bugged, then. 

-
- Enthusiast
- Posts: 141
- Joined: Sat Sep 21, 2019 4:24 pm
Re: Are PureBasic's integer types little or big endian?
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/
https://ricardo-sdl.itch.io/
Re: Are PureBasic's integer types little or big endian?
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...
Except on this sentence...
Re: Are PureBasic's integer types little or big endian?
Oh wow, this is awesome, thanks you two! 
