# Swig: how to convert char\* to Array?

**URL:** <https://www.ruby-forum.com/t/swig-how-to-convert-char-to-array/126735>\
**Category:** Ruby\
**Created:** [January 21, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/swig-how-to-convert-char-to-array/126735 "2008-01-21T19:59:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [January 21, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/swig-how-to-convert-char-to-array/126735/1 "2008-01-21T19:59:36Z")

</div>

I have a C++ library that I’m trying to access in Ruby. One of the C++  
functions returns a char\* and I would like this to be converted to an  
Array  
(since the char\* may have 0x00 in it, which terminate a Ruby string  
prematurely). I’m trying to write a typemap for the conversion, but I’m  
getting stuck on determining the length of the array – I can’t pass  
that as  
a parameter, since this is an _out_ typemap. So my question is, does  
anyone  
have a way to convert char\* to Array in Swig, hopefully with an example?  
Thanks!

Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [January 22, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/swig-how-to-convert-char-to-array/126735/2 "2008-01-22T17:24:53Z")

</div>

On Monday 21 January 2008 19:58:44 Adam B. wrote:

> I have a C++ library that I’m trying to access in Ruby. One of the C++  
> functions returns a char\* and I would like this to be converted to an Array  
> (since the char\* may have 0x00 in it, which terminate a Ruby string  
> prematurely). I’m trying to write a typemap for the conversion, but I’m  
> getting stuck on determining the length of the array – I can’t pass that  
> as a parameter, since this is an _out_ typemap. So my question is, does  
> anyone have a way to convert char\* to Array in Swig, hopefully with an  
> example? Thanks!

Ruby strings are not null-terminated, so they may contain 0x00 as well  
as any  
other char. Of course when converting from C char\* you have to specify  
the  
length:

VALUE rubystring;  
char str[] = {‘a’,‘b’,0x00,‘c’};

rubystring = rb\_str\_new(str,4);

Jan
