Swig: how to convert char* to Array?

A

Adam Bender

[Note: parts of this message were removed to make it a legal post.]

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
 
J

Jan Dvorak

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top