Change Endian

Everything else that doesn't fall into one of the other PB categories.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8453
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Change Endian

Post by netmaestro »

Working with hashing algorithms is one place you can run into the need for changing the byte order in a variable. I didn't really see any examples and I can't remember any so I came up with this, which does do the job:

Code: Select all

Procedure.q ChangeEndian(value.q)
  
  Structure bquad
    byte.a[8]
  EndStructure
  
  Protected result.q = 0
  *thismem.bquad = @result
  *thatmem.bquad = @value
  For i=0 To 7
    *thismem\byte[i] = *thatmem\byte[7-i]
  Next
  
  ProcedureReturn result
  
EndProcedure
Question: Is there a better way? Or a "standard" way that everybody but me knows about?
BERESHEIT
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Change Endian

Post by nco2k »

If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
kenmo
Addict
Addict
Posts: 2083
Joined: Tue Dec 23, 2003 3:54 am

Re: Change Endian

Post by kenmo »

netmaestro you seem to be doing a lot of cryptography coding lately!

I ran into all sorts of endianness headaches writing code for AES and BLAKE for a crypto class in the spring. (Mostly in C though.) I almost wrote a PB port of BLAKE (one of the final candidates to become the official SHA-3 hash) but I figured it wasn't worth it unless BLAKE is chosen as the new standard next year.
Post Reply