Method: String#hex_to_byte_string
- Defined in:
- lib/openc3/core_ext/string.rb
#hex_to_byte_string ⇒ String
Converts the String representing a hexadecimal number (i.e. “0xABCD”) to a binary String with the same data (i.e “xABxCD”)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/openc3/core_ext/string.rb', line 255 def hex_to_byte_string string = self.dup # Remove leading 0x or 0X if string[0..1] == '0x' or string[0..1] == '0X' string = string[2..-1] end length = string.length length += 1 unless (length % 2) == 0 array = [] (length / 2).times do # Grab last two characters if string.length >= 2 last_two_characters = string[-2..-1] string = string[0..-3] else last_two_characters = string[0..0] string = '' end int_value = Integer('0x' + last_two_characters) array.unshift(int_value) end array.pack("C*") end |