From: Shanti Braford Date: 2007-10-31T10:05:29+09:00 Subject: (BitTorrent-related) binary "string" to hexadecimal? Hello, Let's say one were "hypothetically", and that's a big hypothetical there, writing a BitTorrent tracker in ruby... Quick Synopsis: I've got the SHA1 hexdigest of a .torrent metainfo file saved on the server. Clients connect to the tracker and send the 'info_hash' param; this is the raw SHA1 digest (not stored in hex). I'm trying to lookup the hexdigest (of known torrents) with the incoming 'info_hash' param, which is a binary "String" (ruby class wise). So.. For our example below ('foo' is not a valid bencoded string but it makes it easy to demonstrate in this example): digest_str = Digest::SHA1.hexdigest('foo') # => "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" Next up we're trying to write the /announce portion of the BitTorrent tracker. The 'info_hash' param comes through as an escaped binary representation of the info_hash (not hex). To replicate what's going on from the Client end, I've done the following: digest = Digest::SHA1.digest('foo') # => "\v?ǵ????]\r?<[?uڊ3" The torrent client then escapes the digest for transmission over http wire: digest_escaped = CGI::escape(digest) # => "%0B%EE%C7%B5%EA%3F%0F%DB%C9%5D%0D%D4%7F%3C%5B%C2u%DA%8A3" Back on my end, of course I can first CGI unescape this: digest = CGI::unescape(params[:info_hash) # => "\v?ǵ????]\r?<[?uڊ3" So my $1,000,000 question :) How in ruby do we get from "\v?ǵ????]\r?<[?uڊ3" to "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" ? I am aware of a workaround: store the initial SHA1 digest of the torrent in binary from in MySQL. I'm more comfortable working with strings, and haven't dealt with storing binary data in MySQL... but I'll take a look at that option if there's no easy way to go from ruby binary representation to hex. Anyone have any ideas or pointers? - Shanti ps. this is written in rails but the question takes place more at the ruby level, so I figured this forum would be more fitting to ask in! -- Posted via http://www.ruby-forum.com/.