From: Ernest Ellingson Date: 2001-12-03T07:50:27+09:00 Subject: [ruby-talk:27298] Re: ANN: REXML 1.1a7 In my haste to put something out about converting files to UTF8, I screwed up the ASCII conversion. In addition the regular expression that detects UTF-8 was changed slightly (100 to 200 {1} removed. The code below works correctly. def utf8_enc(str) enc="" if str=~/^\376\377/ enc='UTF16' #unicode big endian elsif str=~/^\377\376/ enc='UNILE' #unicode litle endian elsif str=~/([\300-\377][\200-\277])/ #not a guaranteed test won't detect random pairs enc='UTF8' else enc='ASC' end if enc== 'UTF8' strUTF8=str else arrayEnc=[] str.each_byte{|b| arrayEnc << b} arrayUTF8=[] case enc when 'ASC' arrayEnc.each{|b| arrayUTF8 << [b].pack("U")} strUTF8=arrayUTF8.join("") when 'UTF16' || 'UNILE' if enc=='UTF16' 2.step(arrayEnc.size-1, 2){|i| arrayUTF8 << [arrayEnc.at(i+1) + arrayEnc.at(i) * 0x100].pack("U")} elsif enc=='UNILE' 2.step(arrayEnc.size-1, 2){|i| arrayUTF8 << [arrayEnc.at(i) + arrayEnc.at(i+1) * 0x100].pack("U")} else str.each_byte end strUTF8=arrayUTF8.join("") end end end