Detecting the byte order (endian) of a system.

Share your advanced PureBasic knowledge/code with the community.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Detecting the byte order (endian) of a system.

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

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

Post by wilbert »

As far as I know all operating systems PureBasic currently supports are little endian.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

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

Post 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
Post Reply