Page 1 of 1

Detecting the byte order (endian) of a system.

Posted: Wed Mar 07, 2018 8:32 pm
by Dreamland Fantasy
Hi there,

I've seen some code in the forums that utilises 'CompilerIf #PB_Compiler_OS = ...' statements to let a program know what endian format a system is using. I decided however that I wanted to write this little function that tests what endian format is being used. Maybe someone will find it useful.

Code: Select all

Enumeration
  #LITTLE_ENDIAN
  #BIG_ENDIAN
EndEnumeration

Procedure ByteOrder()
  Protected test.w = 1
  If PeekB(@test)
    ProcedureReturn #LITTLE_ENDIAN
  Else
    ProcedureReturn #BIG_ENDIAN
  EndIf
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  Select ByteOrder()
    Case #LITTLE_ENDIAN
      MessageRequester("ByteOrder()", "Little-endian (Intel) byte order detected.")
    Case #BIG_ENDIAN
      MessageRequester("ByteOrder()", "Big-endian (Motorola) byte order detected.")
  EndSelect
CompilerEndIf
Kind regards,

Francis

Re: Detecting the byte order (endian) of a system.

Posted: Wed Mar 07, 2018 9:26 pm
by wilbert
As far as I know all operating systems PureBasic currently supports are little endian.

Re: Detecting the byte order (endian) of a system.

Posted: Thu Mar 08, 2018 2:17 am
by Dreamland Fantasy
wilbert wrote:As far as I know all operating systems PureBasic currently supports are little endian.
True for the latest versions of PureBasic, but I do on occasion get asked to program stuff for PowerPC-based Macs. ;)

Kind regards,

Francis