Created
June 18, 2019 21:18
-
-
Save moreati/6497d5214802aa96c2ff01165b5860da to your computer and use it in GitHub Desktop.
Cython byte swap demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecs | |
import _float_swap | |
print(codecs.encode(_float_swap.noswap(), 'hex')) | |
print(codecs.encode(_float_swap.swap(), 'hex')) |
Author
moreati
commented
Jun 18, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment