# How to retrieve Char in C

**URL:** <https://discourse.julialang.org/t/how-to-retrieve-char-in-c/36057>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 16, 2020, 8:17pm UTC](https://discourse.julialang.org/t/how-to-retrieve-char-in-c/36057 "2020-03-16T20:17:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nnn](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nnn](https://discourse.julialang.org/u/nnn)\
**Post date:** [March 16, 2020, 8:17pm UTC](https://discourse.julialang.org/t/how-to-retrieve-char-in-c/36057/1 "2020-03-16T20:17:10Z")

</div>

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

As I understand Char is implemented [here](https://github.com/JuliaLang/julia/blob/master/base/char.jl) 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:

```julia
#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?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 16, 2020, 8:29pm UTC](https://discourse.julialang.org/t/how-to-retrieve-char-in-c/36057/2 "2020-03-16T20:29:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nnn](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nnn](https://discourse.julialang.org/u/nnn)\
**Post date:** [March 16, 2020, 9:30pm UTC](https://discourse.julialang.org/t/how-to-retrieve-char-in-c/36057/3 "2020-03-16T21:30:55Z")

</div>

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`:

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

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 16, 2020, 9:46pm UTC](https://discourse.julialang.org/t/how-to-retrieve-char-in-c/36057/4 "2020-03-16T21:46:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nnn](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nnn](https://discourse.julialang.org/u/nnn)\
**Post date:** [March 17, 2020, 7:28pm UTC](https://discourse.julialang.org/t/how-to-retrieve-char-in-c/36057/5 "2020-03-17T19:28:03Z")

</div>

Thank you for the info.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 17, 2020, 10:44pm UTC](https://discourse.julialang.org/t/how-to-retrieve-char-in-c/36057/6 "2020-03-17T22:44:39Z")

</div>

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.)
