From: Reid Thompson Date: 2012-04-28T02:51:26+09:00 Subject: Re: why does this return 255 bits instead of 256 (and general bit byte hex string array crypt mayhem :P) On Sat, 2012-04-28 at 01:05 +0900, instance_variable ok wrote: > So I am doing something that requires me to manipulate things on the bit > level, and am having a bit of a headache with learning to use pack and > unpack to accomplish my goal. I thought I came one step closer with > this, but was disappointed to see it thinks the result is 255 bits since > I think it should be 256. What gives? > > hash = OpenSSL::Digest::SHA256.new > test_hash_value = hash << "anything" > puts test_hash_value.to_s.to_i(16).to_s(2).length > > => 255 > > shouldn't it be 256? What is a better way to get the binary > representation of my hash string? I also note that > [12:58:04] rthompso@raker2>~ $ irb irb(main):001:0> require 'openssl' => true irb(main):002:0> require 'digest/sha2' => true irb(main):003:0> hash = OpenSSL::Digest::SHA256.new => # irb(main):004:0> test_hash_value = hash << "anything" => # irb(main):005:0> p test_hash_value # => nil irb(main):006:0> puts test_hash_value.to_s.to_i(16).to_s(2).length 256 => nil irb(main):007:0>