0

I'm curious what's the best way to convert hexadecimal string to binary string in Ruby.

I.e.

def get_binary_string( hex_string )
  # what's the best way?
end

puts get_binary_string("2F")
output> "00101111"

1 Answer 1

5

You could convert the hex 2F to an integer (47), and then convert it back to a binary string (101111):

"2F".to_i(16).to_s(2)
# => "101111"
Sign up to request clarification or add additional context in comments.

1 Comment

What if the hexadecimal string is very long and the integer conversion overflows?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.