Ruby c extensions: character values ​​greater than 127

I'm trying to make a C extension for Ruby that includes a method that returns a string that will sometimes have character values ​​that should be in an unsigned char . At http://github.com/shyouhei/ruby/blob/trunk/README.EXT, all functions listed to include C strings in Ruby strings accept signed characters. Therefore, I could not do this:

 unsigned char bytes[] = {0xf0, 0xf1, 0xf2}; return rb_str_new(bytes, 3); 

How can I create a method that returns these types of strings? In other words, how do I make a C extension using a method that returns "\xff" ?

+4
source share
1 answer

I realized that ruby ​​would treat negative characters as their unsigned equivalent when using rb_str_new . So you can just pass the byte array to char * .

 unsigned char bytes[] = {0xf0, 0xf1, 0xf2}; return rb_str_new((char *)bytes, 3); 
+1
source

Source: https://habr.com/ru/post/1307882/


All Articles