Skip to content

Instantly share code, notes, and snippets.

@moreati
Created June 18, 2019 21:18
Show Gist options
  • Save moreati/6497d5214802aa96c2ff01165b5860da to your computer and use it in GitHub Desktop.
Save moreati/6497d5214802aa96c2ff01165b5860da to your computer and use it in GitHub Desktop.
Cython byte swap demo
from libc.string cimport memcpy
cpdef bytes noswap():
cdef double pi = 3.14159
cdef unsigned char buf[8]
memcpy(&buf, &pi, 8)
return buf[:8]
cpdef bytes swap():
cdef double pi = 3.14159
cdef unsigned char buf[8]
memcpy(&buf, &pi, 8)
return buf[8::-1]
import codecs
import _float_swap
print(codecs.encode(_float_swap.noswap(), 'hex'))
print(codecs.encode(_float_swap.swap(), 'hex'))
@moreati
Copy link
Author

moreati commented Jun 18, 2019

$ cythonize -i3a _float_swap.pyx && python float_swap.py 

Compiling /home/alex/src/cickle/_float_swap.pyx because it changed.
[1/1] Cythonizing /home/alex/src/cickle/_float_swap.pyx
running build_ext
building '_float_swap' extension
...
b'6e861bf0f9210940'
b'400921f9f01b866e'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment