1

I want to convert a String to a Ruby byte array. I have a String and saving it in Hbase as binary byte array. Need to scan with some filter on key which is a binary byte array. Have a String like "U92w8GSBJ2BiHzGg" and need its representation like "\xFF\xA4\xDD\x13\x003\xE4\x85\xC7\x9D\xD5ZY\xF0\x1E" so that I can make query on Hbase shell like below

hbase(main):005:0> scan 't1', {FILTER => "(PrefixFilter ('\xFF\xA4\xDD\x13\x003\xE4')"}

Thanks in advance

minhas23
  • 9,291
  • 3
  • 58
  • 40
  • Possible duplicate of [How to convert string to bytes in Ruby?](https://stackoverflow.com/questions/4103895/how-to-convert-string-to-bytes-in-ruby) – anothermh Apr 17 '18 at 22:08

2 Answers2

1

I want to convert a String to a Ruby byte array.

will assume you mean: "want to convert a Ruby String to a Java byte array"

simply use 'a_ruby_binary_string'.to_java_bytes (returns byte[] under JRuby)

kares
  • 7,076
  • 1
  • 28
  • 38
0

The correct way to do it is to use double quotes for JRuby byte strings. For example:

"\xFF\xA4\xDD\x13\x003\xE4"

(I know this reply is late, but I had the same problem and stumbled upon this solution)

balster neb
  • 127
  • 1
  • 10
  • 1. This works in other places in hbase shell (`STARTROW`, for example), but but in filter parameter it results in `TypeError: cannot convert instance of class org.jruby.RubyString to class [B` 2. This does not really answer the question, as for the string "U92w8GSBJ2BiHzGg" you first need to figure out yourself what the corresponding bytes are – Oleg Muravskiy Feb 04 '20 at 09:09