From: Jeremy Hinegardner Date: 2007-05-11T02:57:19+09:00 Subject: Re: Trouble using string.tr() On Fri, May 11, 2007 at 01:54:36AM +0900, Todd Burch wrote: > Here's another twist. > > If I set > > instring = 0xF1.chr + 0xF2.chr + 0x4B.chr + 0xF3.chr + 0xF4.chr > > which is ebcdic for "12.34", I get "45.67" in ascii. It looks like > something ( tr() maybe? ) is swallowing characters. Have you tried just funnelling the data through some other tool? For instance on *nix there's dd(1) which will do the conversion for you. Or GNU's recode[1] which will take care of it, and the both take stdin and stdout parameters. I know you are on a windows machine, but if you have cygwin or the gnu utilities for Win32 you should be able to take care of it via a system() or %x{} call in ruby. Here is a sample OSX version using Open3. > cat ebcdic2ascii.rb #!/usr/bin/env ruby require 'open3' DD="/bin/dd conv=ascii" RECODE="/opt/local/bin/recode EBCDIC..ASCII" INPUT= 0xF1.chr + 0xF2.chr + 0x4B.chr + 0xF3.chr + 0xF4.chr dd_output = nil Open3.popen3(DD) do |stdin,stdout,stderr| stdin.write(INPUT) stdin.close dd_output = stdout.read end recode_output = nil Open3.popen3(RECODE) do |stdin,stdout,stderr| stdin.write(INPUT) stdin.close recode_output = stdout.read end puts "#{"%10s %10s %10s" % %w(EBCDIC dd recode)}" INPUT.size.times do |i| e = "0x%02x" % INPUT[i] puts "#{"%10s %10s %10s" % [e,dd_output[i].chr,recode_output[i].chr]}" end > ruby ebcdic2ascii.rb EBCDIC dd recode 0xf1 1 1 0xf2 2 2 0x4b . . 0xf3 3 3 0xf4 4 4 Some old ruby-talk[2] postings talk about using Iconv but that the code sample in the msg doesn't seem to work anymore enjoy, -jeremy 1 - http://www.gnu.org/software/recode/recode.html 2 - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/66280 -- ======================================================================== Jeremy Hinegardner jeremy@hinegardner.org