2014年2月12日水曜日

RubyでLSBfirst

とある機器との通信でLSBit firstで送信する必要があり、Rubyでコーディング。
美しくないけど、これでいいはず…。

  def b_reverse(s_char)
    i_work = 0
    i_ascii = s_char.ord
    8.times {|x|
      i_work += (i_ascii & 1) * 2**(7-x)
      i_ascii >>= 1
    }
    return i_work.chr
  end