[Pyrex] [cython] bin2hex conversion

Martijn Meijers w3-pyrex at zw9.nl
Fri May 9 17:14:47 CEST 2008


Hi,

I have a question regarding a bin2hex conversion, which I can't seem to 
get working with Cython (0.9.6.12).

This code is working (buf contains the binary character string):

    cdef char* buf
    cdef unsigned int i = 0

    hexstr = 
["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
    result = []

    for i from 0 <= i < size:
        result.append(hexstr[buf[i] >> 4 & 0xf])
        result.append(hexstr[buf[i] & 0xf])

It gives me the correct hex string where I'm after, _but_ this version 
is still slow (as hexstr and result are still python objects).

I've tried to replace hexstr with:

    cdef char hexstr[] = "0123456789ABCDEF"

as hexstr[14] could give me E for example.

However, this code doesn't compile with cython. It complains about: 
"Syntax error in C variable declaration" pointing to the line above.

Could someone shed some light on this or provide a working solution?

Kind regards,


Martijn

-- 
Martijn Meijers         Delft University of Technology
b.m.meijers at tudelft.nl  OTB, Section GIS-technology




More information about the Pyrex mailing list