title=pack ƥץ졼ʸ



ʲˤΤϡ[[m:Array#pack]][[m:String#unpack]]
Υƥץ졼ʸΰǤƥץ졼ʸϸˡĹפɽ
³뤳ȤǤޤĹפ`*'Ȥ뤳ȤǡֻĤơ
ɽȤǤޤ

Ĺΰ̣ϥƥץ졼ʸˤۤʤޤ
  "iiii"
Τ褦Ϣ³ƥץ졼ʸ
  "i4"
Ƚ񤭴뤳ȤǤޤ

ƥץ졼ʸζ̵뤵ޤ
ޤ`#' Ԥ뤤ϥƥץ졼ʸκǸޤǤϥȤȤߤ
̵뤵ޤ

==== Υƥץ졼ʸΥƥ¸

ƥƥץ졼ʸǡ
short  long ϥƥˤ餺줾 2, 4Хȥ
ο(32ӥåȥޥǰŪshort, longΥ)̣Ƥ
s, S, l, L ФƤľ _ ޤ !  "s_" 뤤 "s!" Τ褦
³뤳Ȥǥƥ¸ short, long Υˤ뤳ȤǤޤ

i, I (int)ΥϾ˥ƥ¸Ǥꡢn, N, v, V
ΥϾ˥ƥ¸ǤϤʤ(!Ĥʤ)ȤդƤ

Ĥޤꡢ[[m:IO#ioctl]] ʤɤ C ι¤ΤϤȤΤ褦ˡ
ƥ¸Υȥǥ˹碌ɬפȤˤ
s!, S!, i!, I!, l!, L!, q!, Q! Ѥޤ
ޤͥåȥץȥեեޥåȤΤ褦ˡ
ƥ˰¸ʤǡ򰷤Ȥˤ
n, N, v, V Ѥޤ

ޤȤȰʲΤ褦ˤʤޤ

: ǥ¸¸ (ͥåȥץȥʤɤŬ)
//emlist{
  n: big endian unsigned 16bit
  N: big endian unsigned 32bit
  v: little endian unsigned 16bit
  V: little endian unsigned 32bit
//}

: ǥ¸¸ (C ι¤ΤʤɤŬ)
//emlist{     
  s!: signed short
  S!: unsigned short
  i!: signed int
  I!: unsigned int
  l!: signed long
  L!: unsigned long
  q!: signed long long
  Q!: unsigned long long
//}

: ǥ¸¸ (C99  stdint.h ˤ븷̩Ŭ)
//emlist{
  s: int16_t
  S: uint16_t
  l: int32_t
  L: uint32_t
//}

==== ƥƥץ졼ʸ

桢[[m:Array#pack]]  [[m:String#unpack]] ǰ㤤ΤΤ `/' Ƕڤä
Array#pack  / String#unpack פȤƤޤ

: a

  ASCIIʸ(nullʸͤ/³nullʸ䥹ڡĤ)
//emlist{
    ["abc"].pack("a") => "a"
    ["abc"].pack("a*") => "abc"
    ["abc"].pack("a4") => "abc\0"

    "abc\0".unpack("a4") => ["abc\0"]
    "abc ".unpack("a4") => ["abc "]
//}

: A

  ASCIIʸ(ڡͤ/³nullʸ䥹ڡ)
//emlist{
    ["abc"].pack("A") => "a"
    ["abc"].pack("A*") => "abc"
    ["abc"].pack("A4") => "abc "

    "abc ".unpack("A4") => ["abc"]
    "abc\0".unpack("A4") => ["abc"]
//}

: Z

  nullüʸ(aƱ / ³nullʸ)
//emlist{
    ["abc"].pack("Z") => "a"
    ["abc"].pack("Z*") => "abc"
    ["abc"].pack("Z4") => "abc\0"

    "abc\0".unpack("Z4") => ["abc"]
    "abc ".unpack("Z4") => ["abc "]
//}

: b

  ӥåȥȥ(ƥХȤȤ˲̥ӥåȤ̥ӥå)
//emlist{
    "\377\000".unpack("b*") => ["1111111100000000"]
    "\001\002".unpack("b*") => ["1000000001000000"]
    "\001\002".unpack("b3") => ["100"]


    ["1000000001000000"].pack("b*") => "\001\002"
//}

: B

  ӥåȥȥ(ƥХȤȤ˾̥ӥåȤ鲼̥ӥå)
//emlist{
    "\377\000".unpack("B*")  => ["1111111100000000"]
    "\001\002".unpack("B*")  => ["0000000100000010"]
    "\001\002".unpack("B9")  => ["000000010"]
    "\001\002".unpack("B15") => ["000000010000001"]

    ["0000000100000010"].pack("B*") => "\001\002"
    ["0000000100000010"].pack("B0") => ""
    ["0000000100000010"].pack("B1") => "\000"
    ["0000000100000010"].pack("B7") => "\000"
    ["0000000100000010"].pack("B8") => "\001"
    ["0000000100000010"].pack("B9") => "\001\000"
    ["0000000100000010"].pack("B14") => "\001\000"
    ["0000000100000010"].pack("B15") => "\001\002"
    ["0000000100000010"].pack("B16") => "\001\002"
//}

: h

  16ʸ(̥˥֥뤬)
//emlist{
    "\x01\xfe".unpack("h*") => ["10ef"]
    "\x01\xfe".unpack("h3") => ["10e"]

    ["10ef"].pack("h*") => "\001\376"
//}

: H

  16ʸ(̥˥֥뤬)
//emlist{
    "\x01\xfe".unpack("H*") => ["01fe"]
    "\x01\xfe".unpack("H3") => ["01f"]
    "~".unpack("H2") => ["7e"]

    ["01fe"].pack("H*") => "\001\376"
    ["7e"].pack("H2") => "~"
//}

: c

  char (8bit Ĥ)
//emlist{
    "\001\376".unpack("c*") => [1, -2]

    [1, -2].pack("c*") => "\001\376"
    [1, 254].pack("c*") => "\001\376"
//}

: C

  unsigned char (8bit ʤ)
//emlist{
    "\001\376".unpack("C*") => [1, 254]

    [1, -2].pack("C*") => "\001\376"
    [1, 254].pack("C*") => "\001\376"
//}

: s

  short (16bit Ĥ, ǥ˰¸)
  (s!  16bit Ǥʤshort Υ˰¸)

  ȥ륨ǥ:
//emlist{
    "\001\002\376\375".unpack("s*") => [513, -514]

    [513, 65022].pack("s*") => "\001\002\376\375"
    [513, -514].pack("s*") => "\001\002\376\375"
//}

  ӥåǥ:
//emlist{
    "\001\002\376\375".unpack("s*") => [258, -259]

    [258, 65277].pack("s*") => "\001\002\376\375"
    [258, -259].pack("s*") => "\001\002\376\375"
//}

: S

  unsigned short (16bit ʤ, ǥ˰¸)
  (S!  16bit Ǥʤshort Υ˰¸)

  ȥ륨ǥ:
//emlist{
    "\001\002\376\375".unpack("S*") => [513, 65022]

    [513, 65022].pack("s*") => "\001\002\376\375"
    [513, -514].pack("s*") => "\001\002\376\375"
//}

  ӥåǥ:

//emlist{
    "\001\002\376\375".unpack("S*") => [258, 65277]

    [258, 65277].pack("S*") => "\001\002\376\375"
    [258, -259].pack("S*") => "\001\002\376\375"
//}

: i

  int (Ĥ, ǥ int Υ˰¸)

  ȥ륨ǥ, 32bit int:
//emlist{
    "\001\002\003\004\377\376\375\374".unpack("i*") => [67305985, -50462977]

    [67305985, 4244504319].pack("i*") => RangeError
    [67305985, -50462977].pack("i*") => "\001\002\003\004\377\376\375\374"
//}

  ӥåǥ, 32bit int:
//emlist{
    "\001\002\003\004\377\376\375\374".unpack("i*") => [16909060, -66052]

    [16909060, 4294901244].pack("i*") => RangeError
    [16909060, -66052].pack("i*") => "\001\002\003\004\377\376\375\374"
//}
: I

  unsigned int (ʤ, ǥ int Υ˰¸)

  ȥ륨ǥ, 32bit int:
//emlist{
    "\001\002\003\004\377\376\375\374".unpack("I*") => [67305985, 4244504319]

    [67305985, 4244504319].pack("I*") => "\001\002\003\004\377\376\375\374"
    [67305985, -50462977].pack("I*") => "\001\002\003\004\377\376\375\374"
//}
  ӥåǥ, 32bit int:
//emlist{
    "\001\002\003\004\377\376\375\374".unpack("I*") => [16909060, 4294901244]

    [16909060, 4294901244].pack("I*") => "\001\002\003\004\377\376\375\374"
    [16909060, -66052].pack("I*") => "\001\002\003\004\377\376\375\374"
//}

: l

  long (32bit Ĥ, ǥ˰¸)
  (l!  32bit Ǥʤlong Υ˰¸)

  ȥ륨ǥ, 32bit long:
//emlist{
    "\001\002\003\004\377\376\375\374".unpack("l*") => [67305985, -50462977]

    [67305985, 4244504319].pack("l*") => RangeError
    [67305985, -50462977].pack("l*") => "\001\002\003\004\377\376\375\374"
//}
: L

  unsigned long (32bit ʤ, ǥ˰¸)
  (L!  32bit Ǥʤlong Υ˰¸)

  ȥ륨ǥ, 32bit long:
//emlist{
    "\001\002\003\004\377\376\375\374".unpack("L*") => [67305985, 4244504319]

    [67305985, 4244504319].pack("L*") => "\001\002\003\004\377\376\375\374"
    [67305985, -50462977].pack("L*") => "\001\002\003\004\377\376\375\374"
//}

: q

  long long (դ, ǥ long long Υ˰¸)
  (C  long long ʤˤ 64bit)

  ȥ륨ǥ, 64bit long long:
//emlist{
    "\001\002\003\004\005\006\007\010\377\376\375\374\373\372\371\370".unpack("q*")
    => [578437695752307201, -506097522914230529]

    [578437695752307201, -506097522914230529].pack("q*")
    => "\001\002\003\004\005\006\a\010\377\376\375\374\373\372\371\370"
    [578437695752307201, 17940646550795321087].pack("q*")
    => "\001\002\003\004\005\006\a\010\377\376\375\374\373\372\371\370"
//}
: Q

  unsigned long long (ʤ, ǥ long long Υ˰¸)
  (C  long long ʤˤ 64bit)

  ȥ륨ǥ, 64bit long long:
//emlist{
    "\001\002\003\004\005\006\007\010\377\376\375\374\373\372\371\370".unpack("Q*")
    => [578437695752307201, 17940646550795321087]

    [578437695752307201, 17940646550795321087].pack("Q*")
    => "\001\002\003\004\005\006\a\010\377\376\375\374\373\372\371\370"
    [578437695752307201, -506097522914230529].pack("Q*")
    => "\001\002\003\004\005\006\a\010\377\376\375\374\373\372\371\370"
//}

: m

  base64줿ʸ60 ƥåȤ(ȺǸ)˲ԥɤղäޤ

  Base64ϡ3ƥå(8bits * 3 = 24bits)ΥХʥꥳɤASCIIʸ
  65ʸ ([A-Za-z0-9+/]64ʸpaddingΤ'=')Ѥ
  4ƥå(6bits * 4 = 24bits)ΰǽʸѴ륨󥳡ǥ
  ˡǤRFC2045Ƥޤ
//emlist{
    [""].pack("m") => ""
    ["\0"].pack("m") => "AA==\n"
    ["\0\0"].pack("m") => "AAA=\n"
    ["\0\0\0"].pack("m") => "AAAA\n"
    ["\377"].pack("m") => "/w==\n"
    ["\377\377"].pack("m") => "//8=\n"
    ["\377\377\377"].pack("m") => "////\n"

    ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m")
    => "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n"
    ["abcdefghijklmnopqrstuvwxyz"].pack("m3")
    => "YWJj\nZGVm\nZ2hp\namts\nbW5v\ncHFy\nc3R1\ndnd4\neXo=\n"

    "".unpack("m") => [""]
    "AA==\n".unpack("m") => ["\000"]
    "AA==".unpack("m") => ["\000"]

    "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m")
    => ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
    "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m")
    => ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
//}

: M

  quoted-printable encoding 줿ʸ
//emlist{
    ["a b c\td \ne"].pack("M") => "a b c\td =\n\ne=\n"

    "a b c\td =\n\ne=\n".unpack("M") => ["a b c\td \ne"]
//}

: n

  ͥåȥХȥ(ӥåǥ)unsigned short (16bit ʤ)
//emlist{
    [0,1,-1,32767,-32768,65535].pack("n*")
    => "\000\000\000\001\377\377\177\377\200\000\377\377"

    "\000\000\000\001\377\377\177\377\200\000\377\377".unpack("n*")
    => [0, 1, 65535, 32767, 32768, 65535]
//}

: N

  ͥåȥХȥ(ӥåǥ)unsigned long (32bit ʤ)
//emlist{
    [0,1,-1].pack("N*") => "\000\000\000\000\000\000\000\001\377\377\377\377"

    "\000\000\000\000\000\000\000\001\377\377\377\377".unpack("N*") => [0, 1, 4294967295]
//}

: v

  "VAX"Хȥ(ȥ륨ǥ)unsigned short (16bit ʤ)
//emlist{
    [0,1,-1,32767,-32768,65535].pack("v*")
    => "\000\000\001\000\377\377\377\177\000\200\377\377"

    "\000\000\001\000\377\377\377\177\000\200\377\377".unpack("v*")
    => [0, 1, 65535, 32767, 32768, 65535]
//}

: V

  "VAX"Хȥ(ȥ륨ǥ)unsigned long (32bit ʤ)
//emlist{
    [0,1,-1].pack("V*") => "\000\000\000\000\001\000\000\000\377\377\377\377"

    "\000\000\000\000\001\000\000\000\377\377\377\377".unpack("V*") => [0, 1, 4294967295]
//}

: f

  ñư(¸)

  IA-32 (x86) (IEEE754 ñ ȥ륨ǥ):
//emlist{
    [1.0].pack("f") => "\000\000\200?"
    [0.0/0.0].pack("f") => "\000\000\300\377"       # NaN
    [1.0/0.0].pack("f") => "\000\000\200\177"       # +Inf
    [-1.0/0.0].pack("f") => "\000\000\200\377"      # -Inf
//}

  SPARC (IEEE754 ñ ӥåǥ):
//emlist{
    [1.0].pack("f") => "?\200\000\000"
    [0.0/0.0].pack("f") => "\177\377\377\377"       # NaN
    [1.0/0.0].pack("f") => "\177\200\000\000"       # +Inf
    [-1.0/0.0].pack("f") => "\377\200\000\000"      # -Inf
//}

  VAX (NetBSD 3.0) (IEEE754):
//emlist{
    [1.0].pack("f") => "\200@\000\000"
//}

: d

  ư(¸)

  IA-32 (IEEE754  ȥ륨ǥ):
//emlist{
    [1.0].pack("d") => "\000\000\000\000\000\000\360?"
    [0.0/0.0].pack("d") => "\000\000\000\000\000\000\370\377"       # NaN
    [1.0/0.0].pack("d") => "\000\000\000\000\000\000\360\177"       # +Inf
    [-1.0/0.0].pack("d") => "\000\000\000\000\000\000\360\377"      # -Inf
//}

  SPARC (IEEE754  ӥåǥ):
//emlist{
    [1.0].pack("d") => "?\360\000\000\000\000\000\000"
    [0.0/0.0].pack("d") => "\177\377\377\377\377\377\377\377"       # NaN
    [1.0/0.0].pack("d") => "\177\360\000\000\000\000\000\000"       # +Inf
    [-1.0/0.0].pack("d") => "\377\360\000\000\000\000\000\000"      # -Inf
//}

  VAX (NetBSD 3.0) (IEEE754):
//emlist{
    [1.0].pack("d") => "\200@\000\000\000\000\000\000"
//}

: e

  ȥ륨ǥñư(¸)

  IA-32 (IEEE754):
//emlist{
    [1.0].pack("e") => "\000\000\200?"
//}

  SPARC (IEEE754):
//emlist{
    [1.0].pack("e") => "\000\000\200?"
//}

: E

  ȥ륨ǥư(¸)

  IA-32 (IEEE754):
//emlist{
    [1.0].pack("E") => "\000\000\000\000\000\000\360?"
//}

  SPARC (IEEE754):
//emlist{
    [1.0].pack("E") => "\000\000\000\000\000\000\360?"
//}

: g

  ӥåǥñư(¸)

  IA-32 (IEEE754):
//emlist{
    [1.0].pack("g") => "?\200\000\000"
//}

  SPARC (IEEE754):
//emlist{
    [1.0].pack("g") => "?\200\000\000"
//}

  IEEE754ʴĶξ硢ʲΤ褦ˤ桢ؿФޤ
//emlist{
    s = [v].pack("g").unpack("B*")[0][0,1]      # 
    e = [v].pack("g").unpack("B*")[0][1,8]      # ؿ
    f = [v].pack("g").unpack("B*")[0][9,23]     # 
//}

  ơs, e, f ΰ̣ϰʲ̤Ǥ
//emlist{
    sgn = s == "0" ? +1.0 : -1.0
    exp = Integer("0b" + e)
    fra = Integer("0b" + f)
    if exp == 0
      if fra == 0
        sgn * 0                     # 0 (positive/negative zero)
      else
        sgn * fra * 2**(-126-23)    #  (denormalized number)
      end
    elsif exp == 255
      if fra == 0
        sgn * Inf                   # ޡ (positive/negative infinity)
      else
        NaN                         #  (not a number)
      end
    else
      fra += 1 << 23                # 
      sgn * fra * 2**(exp-127-23)   #  (normalized number)
    end
//}

: G

  ӥåǥư(¸)

  IA-32:
//emlist{
    [1.0].pack("G") => "?\360\000\000\000\000\000\000"
//}

  SPARC:
//emlist{
    [1.0].pack("G") => "?\360\000\000\000\000\000\000"
//}

  IEEE754ʴĶξ硢ʲΤ褦ˤ桢ؿФޤ
//emlist{
    s = [v].pack("G").unpack("B*")[0][0,1]    # 
    e = [v].pack("G").unpack("B*")[0][1,11]   # ؿ
    f = [v].pack("G").unpack("B*")[0][12,52]  # 
//}

  ơs, e, f ΰ̣ϰʲ̤Ǥ
//emlist{
    sgn = s == "0" ? +1.0 : -1.0
    exp = Integer("0b" + e)
    fra = Integer("0b" + f)
    if exp == 0
      if fra == 0
        sgn * 0                     # 0 (positive/negative zero)
      else
        sgn * fra * 2**(-1022-52)   #  (denormalized number)
      end
    elsif exp == 2047
      if fra == 0
        sgn * Inf                   # ޡ (positive/negative infinity)
      else
        NaN                         #  (not a number)
      end
    else
      fra += 1 << 52                # 
      sgn * fra * 2**(exp-1023-52)  #  (normalized number)
    end
//}

: p

  ʥ뽪üʸؤΥݥ
//emlist{
    [""].pack("p") => "\310\037\034\010"
    ["a", "b", "c"].pack("p3") => " =\030\010\340^\030\010\360^\030\010"
    [nil].pack("p") => "\000\000\000\000"
//}

: P

  ¤(Ĺʸ)ؤΥݥ
//emlist{
    [nil].pack("P") => "\000\000\000\000"
    ["abc"].pack("P3") => "x*\024\010"

    ["abc"].pack("P4") => ArgumentError: too short buffer for P(3 for 4)
    [""].pack("P") => ArgumentError: too short buffer for P(0 for 1)
//}

: u

  uuencode줿ʸ
//emlist{
    [""].pack("u") => ""
    ["a"].pack("u") => "!80``\n"
    ["abc"].pack("u") => "#86)C\n"
    ["abcd"].pack("u") => "$86)C9```\n"
    ["a"*45].pack("u") => "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n"
    ["a"*46].pack("u") => "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n"
    ["abcdefghi"].pack("u6") => "&86)C9&5F\n#9VAI\n"
//}

: U

  UTF-8
//emlist{
    [0].pack("U") => "\000"
    [1].pack("U") => "\001"
    [0x7f].pack("U") => "\177"
    [0x80].pack("U") => "\302\200"
    [0x7fffffff].pack("U") => "\375\277\277\277\277\277"
    [0x80000000].pack("U") => RangeError
    [0,256,65536].pack("U3") => "\000\304\200\360\220\200\200"

    "\000\304\200\360\220\200\200".unpack("U3") => [0, 256, 65536]
    "\000\304\200\360\220\200\200".unpack("U") => [0]
    "\000\304\200\360\220\200\200".unpack("U*") => [0, 256, 65536]
//}

: w

  BER

  1ХȤ7ӥåȤѤɬ׺Ǿ¤ΥХȿǤե
  0ʾɽɽƥХȤκǾ̥ӥåȤϥǡκǸ
  ɬ1ΩäƤ(ĤޤǾ̥ӥåȤϤɤޤǥǡ
  뤫򼨤Ƥ)

  ISO/IEC 8825-1:1995 : Information technologyASN.1 encoding rules : Specification of Basic Encoding Rules(BER) 沽ˡ

//emlist{
    [0].pack("w") => "\000"
    [1].pack("w") => "\001"
    [127].pack("w") => "\177"
    [128].pack("w") => "\201\000"
    [0x3fff].pack("w") => "\377\177"
    [0x4000].pack("w") => "\201\200\000"
    [0x3fffffff].pack("w") => "\203\377\377\377\177"
    [0x40000000].pack("w") => "\204\200\200\200\000"
    [0xffffffff].pack("w") => "\217\377\377\377\177"
    [0x100000000].pack("w") => "\220\200\200\200\000"

    "\0".unpack("w") => [0]
    "\0\201\0\1".unpack("w*") => [0, 128, 1]
//}

  ʤBERǥ󥳡ɤ̤羮ط¸ʤ
  ȤС[0x3fff].pack("w") > [0x4000].pack("w") Ǥ롣

: x

  ʥХ/1ХɤФ
//emlist{
    [?a, ?b].pack("CxC") => "a\000b"
    [?a, ?b].pack("Cx3C") => "a\000\000\000b"

    "a\000b".unpack("CxC") => [97, 98]
    "a\377b".unpack("CxC") => [97, 98]
    "a\377b".unpack("Cx3C") => ArgumentError: x outside of string
//}

: X

  1Хȸ
//emlist{
    [?a, ?b, ?c].pack("CCXC") => "ac"

    "abcdef".unpack("x*XC") => [102]
//}

: @

  а֤ؤΰư
//emlist{
    [?a, ?b].pack("C @3 C") => "a\000\000b"

    "a\000\000b".unpack("C @3 C") => [97, 98]
//}

==== 

ʲpack/unpack λΰǤ

pack ѤʤƤƱȤǤϤܤƤޤ
pack ϰŹˤʤ䤹̤뤳Ȥθpack Ȥʤ
̲򼨤Ǥ

: (ʸ)ʸѴ
//emlist{
    p [82, 117, 98, 121].pack("cccc")
    => "Ruby"

    p [82, 117, 98, 121].pack("c4")
    => "Ruby"

    p [82, 117, 98, 121].pack("c*")
    => "Ruby"

    s = ""
    [82, 117, 98, 121].each {|c| s << c}
    p s
    => "Ruby"

    p [82, 117, 98, 121].collect {|c| sprintf "%c", c}.join
    => "Ruby"

    p [82, 117, 98, 121].inject("") {|s, c| s << c}
    => "Ruby"
//}

: ʸ(ʸ)Ѵ
//emlist{
    p "Ruby".unpack('C*')
    => [82, 117, 98, 121]

    a = []
    "Ruby".each_byte {|c| a << c}
    p a
    => [82, 117, 98, 121]
//}

: "x" ǥʥХȤ뤳ȤǤ
//emlist{
    p [82, 117, 98, 121].pack("ccxxcc")
    => "Ru\000\000by"
//}

: "x" ʸɤФ
//emlist{
    p "Ru\0\0by".unpack('ccxxcc')
    => [82, 117, 98, 121]
//}

: HexפͤѴ
//emlist{
    p "61 62 63 64 65 66".delete(' ').to_a.pack('H*').unpack('C*')
    => [97, 98, 99, 100, 101, 102]

    p "61 62 63 64 65 66".split.collect {|c| c.hex}
    => [97, 98, 99, 100, 101, 102]
//}

: Хʥ16ʿpackǤĹХȿǤϤʤӥåȤ˥֥θĿɽ
//emlist{
    p [0b01010010, 0b01110101, 0b01100010, 0b01111001].pack("C4")
    => "Ruby"
    p ["01010010011101010110001001111001"].pack("B32") # 8 bits * 4
    => "Ruby"

    p [0x52, 0x75, 0x62, 0x79].pack("C4")
    => "Ruby"
    p ["52756279"].pack("H8")  # 2 nybbles * 4
    => "Ruby"
//}

: ƥץ졼ʸ'a'Ĺ1ĤʸŬѤ
//emlist{
    p  ["RUBY", "u", "b", "y"].pack("a4")
    => "RUBY"

    p ["RUBY", "u", "b", "y"].pack("aaaa")
    => "Ruby"

    p ["RUBY", "u", "b", "y"].pack("a*aaa")
    => "RUBYuby"
//}

: ƥץ졼ʸ"a"ϡĹ­ʤʬ̥ʸ䤦
//emlist{
    p ["Ruby"].pack("a8")
    => "Ruby\000\000\000\000"
//}

: ȥ륨ǥȥӥåǥ
//emlist{
    p [1,2].pack("s2")
    => "\000\001\000\002" # ӥåǥΥƥǤν
    => "\001\000\002\000" # ȥ륨ǥΥƥǤν

    p [1,2].pack("n2")
    => "\000\001\000\002" # ƥˤ餺ӥåǥ

    p [1,2].pack("v2")
    => "\001\000\002\000" # ƥˤ餺ȥ륨ǥ
//}

: ͥåȥХȥ signed long
//emlist{
      s = "\xff\xff\xff\xfe"
      n = s.unpack("N")[0]
      if n[31] == 1
        n = -((n ^ 0xffff_ffff) + 1)
      end
      p n
      => -2
//}

: ͥåȥХȥ signed long(2)
//emlist{
      s = "\xff\xff\xff\xfe"
      p n = s.unpack("N").pack("l").unpack("l")[0]
      => -2
//}

: IPɥ쥹
//emlist{
      require 'socket'
      p Socket.gethostbyname("localhost")[3].unpack("C4").join(".")
      => "127.0.0.1"

      p "127.0.0.1".split(".").collect {|c| c.to_i}.pack("C4")
      => "\177\000\000\001"
//}

: sockaddr_in ¤
//emlist{
      require 'socket'
      p [Socket::AF_INET,
         Socket.getservbyname('echo'),
         127, 0, 0, 1].pack("s n C4 x8")
      => "\002\000\000\a\177\000\000\001\000\000\000\000\000\000\000\000"
//}

  pack/unpack Ȥ
  [[m:Socket.pack_sockaddr_in]],
  [[m:Socket.unpack_sockaddr_in]] ᥽åɤޤ

: '\0'üʸΥɥ쥹

  ƥץ졼ʸ "p"  "P" ϡC ٥Υ󥿥եΤ
  ˤޤ(㤨 [[m:IO#ioctl]])
//emlist{
    p ["foo"].pack("p")
    => "8\266\021\010"
//}

  ̤ʸϥߤ˸ޤºݤʸ"foo\0"ؤɥ
  (ΥХʥɽ)ǤʲΤ褦ˤи줿ɽǸ뤳
  ޤ
//emlist{
    printf "%#010x\n", "8\266\021\010".unpack("L")[0]
    => 0x0811b638
//}

  ɥ쥹ؤΥ֥( "foo\0") ϡpack η
  ̤ GC ޤǤGCʤȤݾڤƤޤ

  unpack("p"), unpack("P") ϡpack η̤餷 unpack Ǥޤ
//emlist{
    p ["foo"].pack("p").unpack("p")
    => ["foo"]
    p "8\266\021\010".unpack("p")
    => -:1:in `unpack': no associated pointer (ArgumentError)
            from -:1
//}
  "p"  "P" ϡnil ̤˰ NULL
  ݥ󥿤ȤƲᤷޤ(ʲϡ32bitޥǰŪʷ)
//emlist{
      p [nil].pack("p")        #=> "\000\000\000\000"
      p "\0\0\0\0".unpack("p") #=> [nil]
//}

: ¤ΤΥɥ쥹

  㤨С
//emlist{
      struct {
        int   a;
        short b;
        long  c;
      } v = {1,2,3};
//}
  ɽʸ
//emlist{
      v = [1,2,3].pack("i!s!l!")
//}
  Ǥ(byte alignment ꤫ºݤŬ padding ɬפ
  ʤ뤫⤷ޤ)

  ι¤Τؤɥ쥹
//emlist{
      p [v].pack("P")
      => "\300\265\021\010"
//}
  ޤ

: UTF-8UCS-2ؤѴ (ȤƤʤΤ UTF-16 ȤϤʤ)

  Little endian:
//emlist{
    p(("Comments").unpack("U*").pack("v*"))
    "C\000o\000m\000m\000e\000n\000t\000s\000"
//}
  Big endian:
//emlist{
    p(("Comments").unpack("U*").pack("n*"))
    "\000C\000o\000m\000m\000e\000n\000t\000s"
//}
