From: nobu@... Date: 2006-06-23T10:21:26+09:00 Subject: Re: ascii representation of unicode string? Hi, At Fri, 23 Jun 2006 06:12:21 +0900, Chris Hulan wrote in [ruby-talk:198599]: > darren kirby wrote: > > Hello all. > > > > I am unpacking some unicode strings from a binary file. I have a string > > like: > > > > "W\000M\000/\000T\000r\000a\000c\000k\000N\000u\000m\000b\000e\000r\000\000\000\003" > > > > and I need to turn it into: > > > > "WM/TrackNumber" > > > > could try: > > myAscii = s.unpack('U'*s.length).select{|x| x >46}.collect{|x| > x.chr}.to_s Shorter: s.unpack("v*").pack("U*") #=> "WM/TrackNumber\000" Or to keep trailing odd byte, (s+"\0").unpack("v*").pack("U*") #=> "WM/TrackNumber\000\003" Note this isn't aware of surrogate pairs. -- Nobu Nakada