Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython can cast int 65 into char 'A'?

Tags:

python

cython

%%cython
import cython
cdef int k = 65
cdef unsigned char kk = cython.cast("char", k)
print kk

the result is 65. I already tried <char> to convert 65 to 'A'

Anyone have some ideas? I am currently working in Ipython notebook.

Thank you in advance!!

[Edited] I added the first motivation to this question.

In c,

int i = 65;
char c = i;
printf("%c", c); //prints 'A'

because char 'A' is already int, if I correctly understand

But in Cython,

%%cython
import cython
cdef int k = 65
cdef char kk = cython.cast("char", k) 
print <char>kk, <int>kk

same result.

like image 407
Sunghyun Kim Avatar asked Dec 20 '25 16:12

Sunghyun Kim


1 Answers

Python doesn't have true character types. Python has strings. The ord() function works by taking a 1-character string as a function argument, and just throws an error if the string's length is longer.

Under the hood, all ord() really does is just cast the char to int. In C, I could write a naive function like this:

#include <string.h>         // for strlen

int c_ord(const char* string)
{
    // declare our variables
    size_t length;
    int ret;

    // check the length
    // note that Python actually stores the length,
    // so this wouldn't be done in real code.
    // This is here for example
    length = strlen(string);
    if (length != 1) {
        // invalid length, set a dummy placeholder
        ret = -1; 
    } else {
        // just grab our value
        ret = string[0];
    }

    return ret;
}

Notice how all ord() is doing is getting the exact value, just getting the character, not the string representation. What Cython is doing is the true behavior: treating char like an integer and therefore printing out it's integer value. To treat a character like a string, we could create a array of characters, and let Python know it's a string. The builtin method chr does this for us all under the hood.

%%cython
import cython
cdef int k = 65
print chr(k)

To write a trivial method in Cython to create a null-terminated C-string, and optionally convert it to a Python string, we can do the following:

Python doesn't have true character types. Python has strings. The ord() function works by taking a 1-character string as a function argument, and just throws an error if the string's length is longer.

Under the hood, all ord() really does is just cast the char to int. In C, I could write a naive function like this:

#include <string.h>         // for strlen

int c_ord(const char* string)
{
    // declare our variables
    size_t length;
    int ret;

    // check the length
    // note that Python actually stores the length,
    // so this wouldn't be done in real code.
    // This is here for example
    length = strlen(string);
    if (length > 1) {
        // invalid length, set a dummy placeholder
        ret = -1; 
    } else {
        // just grab our value
        ret = string[0];
    }

    return ret;
}

Notice how all ord() is doing is getting the exact value, just getting the character, not the string representation. What Cython is doing is the true behavior: treating char like an integer and therefore printing out it's integer value. To treat a character like a string, we could create a array of characters, and let Python know it's a string. The builtin method chr does this for us all under the hood.

%%cython
import cython
cdef int k = 65
print chr(k)

To write a trivial extension to do chr() and create a null-terminated string (commonly referred to as a C-string), we can write the following.

%%cython
# imports
import cython
from libc.stdlib cimport malloc, free

# create null-terminated string, or a C-string
cdef char* c_string = <char*>malloc(2)      # only need 2
c_string[0] = 65                            # 'A'
c_string[1] = 0                             # '\0', null-termination
# ... do what we want with the C-string

# convert to Python object
cdef bytes str = c_string

# remember to free the allocate memory
free(c_string)

# use Python object
print(str)
like image 54
Alexander Huszagh Avatar answered Dec 22 '25 04:12

Alexander Huszagh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!