Skip to content

Instantly share code, notes, and snippets.

@rurounijones
Created February 7, 2012 09:19
Show Gist options
  • Save rurounijones/1758578 to your computer and use it in GitHub Desktop.
Save rurounijones/1758578 to your computer and use it in GitHub Desktop.
Ruby FFI shennanigans
$ rvm current
ruby-1.9.3-p0
$ ruby test.rb
Input: 'Test テスト 文字調査', UTF-8
Output: 'Test テスト 文字調査', ASCII-8BIT
$ rvm current
jruby-1.6.6
$ ruby --1.9 test.rb
Input: 'Test テスト 文字調査', UTF-8
Output: '���R', ASCII-8BIT
#include "test.h"
char * testFunction(const char* data, int pageNum)
{
return((char*)data);
}
#ifdef __cplusplus
extern "C" {
#endif
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
char * testFunction(const char* data, int pageNume);
#endif // PDF_TO_IMAGE_H_INCLUDED
#ifdef __cplusplus
}
#endif
# encoding: utf-8
require 'rubygems'
require 'ffi'
module TestModule
extend FFI::Library
ffi_lib File.expand_path("libtest.so", File.dirname(__FILE__))
attach_function :testFunction, [:string, :int], :string
end
input = "Test テスト 文字調査"
output = TestModule.testFunction(input, 1)
puts "Input: '#{input}', #{input.encoding}"
puts "Output: '#{output}', #{output.encoding}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment