@kyanny's blog

My thoughts, my life. Views/opinions are my own.

マルチバイト文字

#!/usr/bin/env ruby
# coding: utf-8

# Shift_JIS (CP932)
"".encode('cp932').ord # => 33440
"".encode('cp932').ord.to_s(16) # => "82a0"
"".encode('cp932').codepoints.first.to_s(16) # => "82a0"
a = "".encode('cp932').ord.to_s(2) # => "1000001010100000"

"".encode('cp932').bytes # => [130, 160]
"".encode('cp932').bytes.map{ |b| b.to_s(16) } # => ["82", "a0"]
"".encode('cp932').bytes.map{ |b| b.to_s(2) } # => ["10000010", "10100000"]
b = "".encode('cp932').bytes.map{ |b| b.to_s(2) }.join # => "1000001010100000"

a # => "1000001010100000"
b # => "1000001010100000"
a == b # => true

# UTF-8
# String#ord は Unicode コードポイントを返す
"".ord # => 12354
"".ord.to_s(16) # => "3042"
"".codepoints.first.to_s(16) # => "3042"
a = "".ord.to_s(2) # => "11000001000010"

"".bytes # => [227, 129, 130]
"".bytes.map{ |b| b.to_s(16) } # => ["e3", "81", "82"]
"".bytes.map{ |b| b.to_s(2) } # => ["11100011", "10000001", "10000010"]
b = "".bytes.map{ |b| b.to_s(2) }.join # => "111000111000000110000010"

a # => "11000001000010"
b # => "111000111000000110000010"
a == b # => false
  • ShiftJIS の「あ」の1バイト目は 82 2バイト目は a0
  • String#ord が返す「あ」のコードポイントは16進数で 82a0
  • 82 a0 という2バイトが並んでいるデータ == 82a0 で2バイトなデータ
  • UTF-8 の「あ」の1バイト目は e3 2バイト目は 81 3バイト目は 82
  • String#ord が返す「あ」のコードポイントは16進数で 3042 これはユニコード番号(Unicode コードポイント)であり各バイトの値とは無関係
  • 結論: String#bytes または xxd コマンドで各バイトの値をちゃんとみる

入門ビジネス英語 Lesson 9

Good morning, everyone.
We’re all quite busy today, so I’d like to keep this meeting to 30 minutes.
But Paul isn’t here yet.
He sent sends his apologies. He has an urgent matter to do deal with.
Something serious?
No, it’s just one of those things, you know.
So, let’s get started.
We need to reach a consensus on two items.:
T the location for the third store in London, and whether to increase the number of staff in Paris.

RubyKaigi 2019

RubyKaigi に参加した。会社でスポンサーして企業ブースを出展したのでブース担当を交代でやるために6名派遣したが、自分は個人的なポリシーに従い有給をとって観光がてら自費で参加した。二日目の午後に太宰府天満宮を、三日目の夕方以降に福岡タワーを観光した。パーティーの類が毎度居心地悪くて苦手なので今回は全てのパーティーを欠席して四泊五日ずっと単独行動していた。気まずさは少なくて気楽ではあったけど、これはこれで少々寂しいものがあった。自分が夜遊びの類をしない人間であることを考慮し忘れていた。福岡の街はたしかにコンパクトにまとまっていて便利そうだったが、ふつうの都市という感じでこれといって魅力を感じなかった。