From: 7stud -- Date: 2009-03-31T11:33:38+09:00 Subject: Re: Euro currency symbol David Wright wrote: > Hmm, I seem to be having a bit of a time representing the Euro sign in > Ruby. > > ruby -v > ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] > (same results on a linux box, 1.8.5 with Ruby) > > symbol: ��� > Decimal: 8364 > hex: 20Ac > html number: € > html name: € > description: euro sign > > > dwright@[1061]:dwright% perl -C2 -le 'print chr(oct("20254"))' > ��� > > dwright@[1062]:dwright%ruby -le 'puts "20254".oct.chr' > -e:1:in `chr': 8364 out of char range (RangeError) > from -e:1 > > dwright@[1063]:dwright%irb >>> puts "20254".oct > 8364 > > >>> puts "20254".oct.chr > RangeError: 8364 out of char range > from (irb):5:in `chr' > from (irb):5 > > ruby -e 'puts "20Ac".hex.chr' > -e:1:in `chr': 8364 out of char range (RangeError) > from -e:1 > > > ruby -KU -e 'puts "20Ac".hex.chr' > -e:1:in `chr': 8364 out of char range (RangeError) > from -e:1 > > ruby -KU -e '$KCODE="u";puts "20Ac".hex.chr' > -e:1:in `chr': 8364 out of char range (RangeError) > from -e:1 > > ruby -e "puts '20Ac'.hex.chr" > -e:1:in `chr': 8364 out of char range (RangeError) > from -e:1 > > Here's the pound: > > ruby -e 'puts "The pound:\243"' > The pound:�� > > ruby -e 'puts "243".oct.chr' > �� dec_num = "20254".oct puts dec_num --output:-- 8364 $ri chr ------------------------------------------------------------ Integer#chr int.chr => string ------------------------------------------------------------------------ Returns a string containing the ASCII character represented by the receiver's value. 65.chr #=> "A" ?a.chr #=> "a" 230.chr #=> "\346" There is no ascii character with an ascii code equal to 8364. The pound has been around a long time, and it made it into extended ascii (or latin-1, which uses 8 bits). The euro is a recent invention, and it's numerical code is way out in unicode land. arr = [] arr << dec_num str = arr.pack("U") #U=UTF-8 => encode unicode 8364 into a UTF-8 character. puts str --output:-- ��� -- Posted via http://www.ruby-forum.com/.