Converting an integer to a hexadecimal string in Ruby
You can give to_s
a base other than 10:
10.to_s(16) #=> "a"
Note that in ruby 2.4 FixNum
and BigNum
were unified in the Integer
class.
If you are using an older ruby check the documentation of FixNum#to_s
and BigNum#to_s
Ruby: How to convert a list of integers into a hexadecimal string?
With pack
and unpack
: (or unpack1
in Ruby 2.4+)
[0, 128, 255].pack('C*').unpack('H*')[0]
#=> "0080ff"
[0, 128, 255].pack('C*').unpack1('H*')
#=> "0080ff"
The actual binary hexadecimal string is already returned by pack('C*')
:
[0, 128, 255].pack('C*')
#=> "\x00\x80\xFF"
unpack('H*')
then converts it back to a human readable representation.
A light-weight alternative is sprintf
-style formatting via String@%
which takes an array:
'%02x%02x%02x' % [0, 128, 255]
#=> "0080ff"
x
means hexadecimal number and 02
means 2 digits with leading zero.
Convert a hex string to a hex int
You'd need supply integer base argument to String#to_i
method:
irb> color = "0xFF00FF"
irb> color.to_i(16)
=> 16711935
irb> color.to_i(16).to_s(16)
=> "ff00ff"
irb> '%#X' % color.to_i(16)
=> "0XFF00FF"
Convert Integer in Hex String Notation into a signed 8-Bit Integer
The c
directive comes very close but it expects a different input: a single character representing the signed 8-bit integer. This requires the input "0xff"
to be "\xff"
. So this conversion must take place first. At least one method known by you can be used here; the other one is Integer#chr
:
"0xff".hex.chr # => "\xFF"
And the complete solution:
"0xff".hex.chr.unpack1(?c) # => -1
Decimal to hexadecimal conversion
Fixnum#to_s
255.to_s(16) # => "ff"
Convert string to hexadecimal in Ruby
Change this line:
line = a.map { |b| ", #{b}" }.join
to this:
line = a.map { |b| sprintf(", 0x%02X",b) }.join
(Change to %02x
if necessary, it's unclear from the example whether the hex digits should be capitalized.)
Converting a hexadecimal string to a decimal number
Just use String#to_i
with a base of 16:
"1a2f".to_i(16) # => 6703
convert decimal to hexidecimal 2 digits at a time Ruby
You can use String#gsub with a regular expression and Kernel#sprintf:
"01 67 15 06 01 76 61 73".gsub(/\d{2} */) { |s| sprintf("%02x", s.to_i) }
#=> "01430f06014c3d49"
The regular expression /\d{2} */)
matches two digits followed by zero or more spaces (note 73
is not followed by space).
The result of the block calculation replaces the two or three characters that were matched by the regular expression.
sprintf
's formatting directive forms a sting containing 2
characters, padded to the left with '0'
's, if necessary, and converting the string representation of an integer in base 10 to the string representation of an integer in base 16 ('x'
).
Alternatively, one could use String#% (with sprintf
's formatting directives):
"01 67 15 06 01 76 61 73".gsub(/\d{2} */) { |s| "%02x" % s.to_i }
#=> "01430f06014c3d49"
Convert byte array to hex string
This works:
[1, 1, 65, -50].map { |n| '%02X' % (n & 0xFF) }.join
The %02X
format specifier makes a 2-character-wide hex number, padded with 0
digits. The & 0xFF
is necessary to convert your negative numbers into the standard 0 through 255 range that people usually use when talking about byte values.
Convert binary string to hexadecimal in Ruby
You can convert it to an integer first, hinting that the string is binary (to_i(2)
), then to hexadecimal (to_s(16)
"1010".to_i(2).to_s(16) # => 'a'
If you need it in uppercase, you can call upcase
on the resulting string.
Related Topics
Rails Paperclip How to Delete Attachment
Haml: Append Class If Condition Is True in Haml
Rspec: Expect VS Expect With Block - What's the Difference
What Are the Magic $-Prefixed Variables in Ruby
Rails Engines Extending Functionality
Include Jekyll/Liquid Template Data in a Yaml Variable
Nomethoderror When Trying to Invoke Helper Method from Rails Controller
Class ≪≪ Self VS Self.Method With Ruby: What's Better
When Do I Need to Restart Server in Rails
Why Not Use Shared Activerecord Connections For Rspec + Selenium
Xpath Axis, Get All Following Nodes Until
Stop Devise from Clearing Session
Why Does Ruby'S 'Gets' Includes the Closing Newline
How to Edit or Write on Existing Pdf With Ruby