# How to safely cast a \_pointer to \_bytes?

**URL:** https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364
**Category:** Questions & Answers
**Tags:** question, ffi, cairo, racketdraw
**Created:** [December 6, 2021, 4:06pm UTC](https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364 "2021-12-06T16:06:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kurinoku](https://avatars.discourse-cdn.com/v4/letter/k/aca169/32.png) [@kurinoku](https://racket.discourse.group/u/kurinoku)
#### Post date: [December 6, 2021, 4:06pm UTC](https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364/1 "2021-12-06T16:06:22Z")

</div>

I'm using `racket/draw/unsafe/cairo` and a custom ffi for MagickWand C API.  
I have this definition, you can see the last parameter is `_bytes`.

```scheme
(define-mwand MagickExportImagePixels (_fun _MagickWand-pointer _size _size _size _size _string _StorageType _bytes -> _MagickBooleanType))

```

The cairo function I'm trying to use is

```scheme
(define-cairo cairo_image_surface_get_data* (_cfun (s : _cairo_surface_t)
                                                   -> _pointer)
  #:c-id cairo_image_surface_get_data)

```

Which returns a pointer which then I feed to `MagickExportImagePixels`, for now I just added another definition called `MagickExportImagePixels/pointer` which is the same as the namesake function but `_bytes` is replaced with `_pointer`.

Yet I still have the question, how to cast a `_pointer` to `_bytes` correctly, I just did `(cast data-pointer _pointer _bytes)` and it crashed or at least I remember doing this, I also realize that `_bytes` in the docs is syntax, so that might be also part of a problem.

---

<div class="post-metadata">

### Author: ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)
#### Post date: [December 6, 2021, 4:35pm UTC](https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364/2 "2021-12-06T16:35:21Z")

</div>

Maybe something like this?

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/00c41aaf164719799482fd417c07a045938aeca0.png)

From [cairo/main.rkt at 5281c9acf2217015b96b8a6a4788b9ec6eecd179 · soegaard/cairo · GitHub](https://github.com/soegaard/cairo/blob/5281c9acf2217015b96b8a6a4788b9ec6eecd179/cairo-lib/cairo/main.rkt#L301)

---

<div class="post-metadata">

### Author: ![kurinoku](https://avatars.discourse-cdn.com/v4/letter/k/aca169/32.png) [@kurinoku](https://racket.discourse.group/u/kurinoku)
#### Post date: [December 6, 2021, 4:54pm UTC](https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364/3 "2021-12-06T16:54:13Z")

</div>

Thank you for the sample code, I didn't realize there was a wrapper for cairo since I generally check the documentation index and not the package index and I also didn't realize there was two `cairo_image_surface_get_data` one with `_bytes` and one with `_pointer` since I want to avoid more allocations, the solution seems to be to have two functions pointing to the same c function.

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [December 6, 2021, 5:25pm UTC](https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364/4 "2021-12-06T17:25:12Z")

</div>

Would it be correct to mark @soegaard 's answer as the solution?

---

<div class="post-metadata">

### Author: ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)
#### Post date: [December 6, 2021, 5:32pm UTC](https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364/5 "2021-12-06T17:32:28Z")

</div>

The library is still new.

There is no documentation yet.

Also the library has only seen limited testing, so beware for bugs.

---

<div class="post-metadata">

### Author: ![kurinoku](https://avatars.discourse-cdn.com/v4/letter/k/aca169/32.png) [@kurinoku](https://racket.discourse.group/u/kurinoku)
#### Post date: [December 6, 2021, 7:00pm UTC](https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364/6 "2021-12-06T19:00:16Z")

</div>

well, I'm not using that exactly, but I think I will

---

<div class="post-metadata">

### Author: ![mflatt](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/mflatt/32/6_2.png) [@mflatt](https://racket.discourse.group/u/mflatt)
#### Post date: [December 6, 2021, 7:26pm UTC](https://racket.discourse.group/t/how-to-safely-cast-a-pointer-to-bytes/364/7 "2021-12-06T19:26:07Z")

</div>

Just for the record: converting from `_pointer` to `_bytes` requires copying, and when `_bytes` is used without a size, the copied data's length is inferred from a terminating a nul character. The `MagickExportImagePixels` binding should almost certainly use `_pointer` instead of `_bytes`, since it's not dealing with nul-terminated data. It should be just as convenient, since `_pointer` as an argument type accepts a byte-string.
