「Ruby」16進数の文字列を数値に変換するサンプル

2021年2月4日

書式
16進数文字列.hex
数値.to_s
使用例

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
 
p  "value1:" + "0x0a".hex.to_s
 
p  "value2:" + "0x0A".hex.to_s

p  "value3:" + "0x0b".hex.to_s
 
p  "value4:" + "0x0B".hex.to_s
 
p  "value5:" + "a".hex.to_s

p  "value6:" + "0xA1".hex.to_s

p "value7:" + "0xB1".hex.to_s
 
p "value8:" + "0x08".hex.to_s
 
p "value9:" +  "0x09".hex.to_s

実行結果
>ruby sample.rb
“value1:10"
“value2:10"
“value3:11"
“value4:11"
“value5:10"
“value6:161"
“value7:177"
“value8:8"
“value9:9"

Ruby

Posted by arkgame