0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyでHexを数値ではなく文字列として扱う

Last updated at Posted at 2016-07-02

Stringクラスにあるhex

"34D6411230".hex
=> 226932888112

これはただ数値に変換されてしまう

自分がやりたいのはこっち

"34D6411230" => "4\xD6A\x120"
OpenSSL::Cipherで復号化したいときに、バイナリ(hex)を直接扱いづらい。
文字コードになっているものは文字を表示させて、表示できないものはエスケープしといてほしい。

やりかた

"34D6411230".scan(/../).map(&:hex).map(&:chr).join
=> "4\xD6A\x120"
0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?