9

I have a situation where I need to convert a binary value to hex in Ruby. My situation is as follows:

When bin = "0000111", my output should be: hex = "07".

When bin = "010001111", my output should be: hex = "08f".

Could someone help me out on how to do this? Thanks in advance.

5 Answers 5

14

How about:

>> "0x%02x" % "0000111".to_i(2) #=> "0x07"
>> "0x%02x" % "010001111".to_i(2) #=> "0x8f"

Edit: if you don't want the output to be 0x.. but just 0.. leave out the first x in the format string.

Sign up to request clarification or add additional context in comments.

3 Comments

when i pass in '1' a single binary bit, this converts to 001. Shouldn't it convert to '1' in hex as well?
``` ("0%02x" % binary.to_i(2)) #=> "001"
"0x%02x"%"1".to_i(2) #=> "0x01".
12
def bin_to_hex(s)
    s.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
end

Which I found here (with zero padding modifications):

http://anthonylewis.com/2011/02/09/to-hex-and-back-with-ruby/

3 Comments

I don't know how people claim it works! For me, using ruby 4.1, it gives: '1111'.each_byte.map { |b| b.to_s(16) }.join => "31313131" (And I want "f" or something similiar.)
@Djunzu You're talking about something different. The string '1111' has four bytes, and each has a value of 49 (ie, the ASCII value). 49 in decimal is 31 in hex. You don't have a "binary" value in the sense that the question is talking about. You have a string containing ASCII characters '1' and '0'.
@Djunzu Actually, nevermind. I just realized that actually, you're right, and the answer is using "binary" in the wrong sense.
5

Both String#to_i and Integer#to_s take an optional integer argument specifying the base. So you can convert your binary string to an integer using base 2 and then convert that integer back to a string using base 16.

2 Comments

hey thanks for ur response.. As u say if i use "11111".to_i(2).to_s(16) it gives me "1f" but if i give "01111".to_i(2).to_s(16) it gives me only "f" but i want "0f"
@sundar: You can using String#% (a.k.a. sprintf) instead of to_s to format the number appropriately - using %02x to format the number as a hexadecimal number padded to 2 digits using 0s. However this does not work if you want a base other than 16 (or 10 if you use d instead of x), as % does not allow you to pick an arbitrary base. Alternatively you can use to_s followed by String#rjust, which works with arbitrary bases and padding characters.
4

You can use the unpack method on string pointing the target to be hex

def bin_to_hex(binary_string)
  binary_string.unpack('H*').first
end

Refer to: https://apidock.com/ruby/String/unpack

I found it to be much more clean than the solutions listed above.

Comments

0

as state above by @Michael Kohl easiest way, define method with the action

  # if you don't want the output to be 0x.. but just 0.. leave out the first x in the format string.
  def self.bin_to_hex(string)
    "0x%02x" % "#{string}".to_i(2)
  end

and use the method like this

string = "00011110"
puts bin_to_hex(string)
=> 0x1e

remember these are still ASCII representations, in case you want to encode it and send bytes as HEX (send via UDP or low level protocols) just do:

value = self.bin_to_hex(value)
bytes = [value]
bytes.pack('H*' * bytes.size)

Comments

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.