Page 1 of 1

Change Endian

Posted: Thu Aug 11, 2011 5:14 pm
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?

Re: Change Endian

Posted: Thu Aug 11, 2011 5:48 pm
by nco2k

Re: Change Endian

Posted: Fri Aug 12, 2011 5:15 pm
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.