Created
February 7, 2012 09:19
-
-
Save rurounijones/1758578 to your computer and use it in GitHub Desktop.
Ruby FFI shennanigans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "test.h" | |
char * testFunction(const char* data, int pageNum) | |
{ | |
return((char*)data); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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