How to retrieve Char in C

I need to get value from Julia Char datatype into C.

As I understand Char is implemented here and comment says:

Char is a 32-bit AbstractChar type that is the default representation of characters in Julia. Char is the type used for character literals like 'x' and it is also the element type of String.

I have not found something like jl_unbox_char, so I thought to use jl_string_ptr. However it always prints c\.

Here’s my code:

#include <julia.h>

int main(int argc, char *argv[])
{
    char* str = malloc(sizeof(char) * 1024);

    jl_init();

    //jl_value_t *var = jl_eval_string("\"abc\""); //works
    jl_value_t *var = jl_eval_string("'a'"); // does not work

    if (jl_is_string(var)) {
    	str = jl_string_ptr(var);
    } else if (jl_isa(var, jl_char_type)) {
    	str = jl_string_ptr(var);
    }
    jl_atexit_hook(0);


    printf("%s", str);

    return 0;
}
}

I’m interested how can I retrieve a Char value?

There’s no need for something like jl_unbox_char, you can just cast the pointer. uint32_t c = *(uint32_t*)var;. You can do your conversion from there. (unicode -> ascii, for example). Char is not a NULL terminated string so if you want that you have to do some conversion yourself.

Thank you!

Now I’m getting a numerical value (1627389952 for a, 1644167168 for b).

How do I interpret it as, well, a unicode character?

Code inside the else if:

    	uint32_t ch = *(uint32_t*)var;
    	sprintf(str,"%lu", ch);

Actually you’ll need the equivalent of codepoint to get the corresponding unicode. It seems that for ascii you can just do >>24 though.

Thank you for the info.

What’s going on is that a Char value in Julia is a 32-bit quantity that basically stores the UTF-8 encoding of the code point, rather than the code-point as an ordinary 32-bit integer. (This is to simplify processing of UTF-8 encoded strings.)

1 Like