我正在尝试为 JRuby 实现一个 Java 扩展来执行字符串异或。我只是不确定如何将字节数组类型转换为RubyString
:
public static RubyString xor(ThreadContext context, IRubyObject self, RubyString x, RubyString y) {
byte[] xBytes = x.getBytes();
byte[] yBytes = y.getBytes();
int length = yBytes.length < xBytes.length ? yBytes.length : xBytes.length;
for(int i = 0; i < length; i++) {
xBytes[i] = (byte) (xBytes[i] ^ yBytes[i]);
}
// How to return a RubyString with xBytes as its content?
}
此外,如何就地执行相同的操作(即x
更新 s 值)?