Skip to content

Instantly share code, notes, and snippets.

@hiroeorz
Created May 4, 2014 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroeorz/11517270 to your computer and use it in GitHub Desktop.
Save hiroeorz/11517270 to your computer and use it in GitHub Desktop.
ツナでもわかるmruby [4回目:バイナリデータフォーマットの実装] ref: http://qiita.com/hiroeorz@github/items/27e0866f493e0e7a65c5
def fixed_header(params)
message_type = params[:message_type] || 1
dup = params[:dup] || 0
qos = params[:qos] || 0
retain = params[:retain] || 0
remaining_size = params[:remaining_size] || 0
data_1 = (message_type << 4) | (dup << 3) | (qos << 1) | retain
data_2 = remaining_size
data_1.chr + data_2.chr
end
fixed_header(message_type:1, dup:0, qos:1, retain:1, remaining_size:20) #=> "\023\024"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment