# Ruby 1.9.2 unicode with hex codepoint problem

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-2-unicode-with-hex-codepoint-problem/203029>\
**Category:** Ruby\
**Created:** [February 10, 2011, 9:34pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-unicode-with-hex-codepoint-problem/203029 "2011-02-10T21:34:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 10, 2011, 9:34pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-unicode-with-hex-codepoint-problem/203029/1 "2011-02-10T21:34:00Z")

</div>

Hi,

I have the following test case:

$ cat test.in  
Der gro\xdfe BilderSauger

$ cat test.rb  
File.open(‘test.in’, ‘r’).each\_line do |line|  
puts line  
test = “Der gro\xdfe BilderSauger”  
puts test

The result is  
Der gro\xdfe BilderSauger  
Der gro?e BilderSauger

I have tried to put an encoding in the File.open() or line.encode()  
without success. the ‘’ is recognized as a real ‘’, not as the  
beginning of an hex escape sequence.

How can I get \xdf to be recognized as ß when reading from a file?

Thanks

–Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [February 11, 2011, 1:54am UTC](https://www.ruby-forum.com/t/ruby-1-9-2-unicode-with-hex-codepoint-problem/203029/2 "2011-02-11T01:54:28Z")

</div>

On Feb 10, 2011, at 12:34 PM, Gilles Gilles wrote:

> The result is  
> Der gro\xdfe BilderSauger  
> Der gro?e BilderSauger
> 
> I have tried to put an encoding in the File.open() or line.encode()  
> without success. the '' is recognized as a real '', not as the  
> beginning of an hex escape sequence.
> 
> How can I get \xdf to be recognized as when reading from a file?

Unicode codepoint 00df needs to be written in a particular encoding.  
I’ll choose UTF-8.

$ echo ‘Der groe BilderSauger’ \> test.in  
$ hexdump -C test.in  
00000000 44 65 72 20 67 72 6f c3 9f 65 20 42 69 6c 64 65 |Der gro..e  
Bilde|  
00000010 72 53 61 75 67 65 72 0a |rSauger.|  
00000018

In test.rb you also need to set UTF-8 for this to work:

$ cat test.rb

# coding: UTF-8

File.open(‘test.in’, ‘r’).each\_line do |line|  
puts line  
test = “Der groe BilderSauger”  
puts test

end  
$ ruby19 test.rb  
Der groe BilderSauger  
Der groe BilderSauger

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [February 12, 2011, 11:50am UTC](https://www.ruby-forum.com/t/ruby-1-9-2-unicode-with-hex-codepoint-problem/203029/3 "2011-02-12T11:50:23Z")

</div>

Gilles Devaux wrote in post #980948:

> test = “Der gro\xdfe BilderSauger”

\xdf is the single byte DF, and your ‘test’ string will have encoding  
ASCII-8BIT.

You’d use \u00df instead if you are using UTF-8 encoding (where that  
character is encoded as two bytes).

Otherwise, if you are using ISO-8859-1 (say), where that codepoint _is_  
the single byte DF, then you’ll need to open your file with that  
encoding specified

Of course, test.in should not contain the character sequence "" “x” “d”  
“f”  
but rather the single byte (or two bytes, if you are using UTF-8  
encoding)

Probably the simplest solution is to open a text editor and type in ß.  
Then use hexdump -C on the file to see what it contains, byte by byte.

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [February 15, 2011, 9:31pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-unicode-with-hex-codepoint-problem/203029/4 "2011-02-15T21:31:09Z")

</div>

You’re doing two different things.

> $ cat test.rb  
> a = “Der gro\xdfe BilderSauger”

That’s a double-quoted string, and so Ruby is doing some translation of  
the contents. A common example is \n meaning “newline”; in this case,  
\xNN means the byte with hex code NN. So when you do each\_byte, that’s  
what you get, a single byte.

Change the double-quotes to single-quotes and you’ll actually get the  
four separate characters.

> But when read from a file:  
> …  
> l.each\_byte {|b| puts b}  
> …  
> 92 ← Here  
> 120 ← we  
> 100 ← are  
> 102 ← as 4 ASCII chars ‘\xdf’

That proves that the file actually contains the four characters  
'', ‘x’, ‘d’, ‘f’. If you want further proof, try

```
hexdump -C test.in

```

to take Ruby out of the loop completely.

So there’s neither UTF-8 nor ISO-8859-1 in that file, just plain ASCII  
characters.

If you want to turn this into something else, you would have to process  
it. For example:

l.gsub!(/\x([0-9a-f]{2})/i) { $1.hex.chr }

# or in ruby 1.9, if you want to tag the encoding:

l.gsub!(/\x([0-9a-f]{2})/i) { $1.hex.chr(“ISO-8859-1”) }

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [February 15, 2011, 9:05pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-unicode-with-hex-codepoint-problem/203029/5 "2011-02-15T21:05:21Z")

</div>

Sorry I haven’t responded earlier but it seems I’m not notified by  
email.

The thing is I do not control the input, it is the browscap file found  
here and is in ISO-8859-1  
[http://browsers.garykeith.com/downloads.asp](http://browsers.garykeith.com/downloads.asp)

\xdf (decimal 223) is a valid ISO-8859-1 code point  
([ISO/IEC 8859-1 - Wikipedia](http://en.wikipedia.org/wiki/ISO/IEC_8859-1))  
it appears as ‘?’ because my terminal is UTF-8 but the bytes are there:

$ cat test.rb  
a = “Der gro\xdfe BilderSauger”  
a.each\_byte { |b| puts b }

$ ruby test.rb  
68  
101  
114  
32  
103  
114  
111  
223 ← Here I am  
101  
32  
66  
105  
108  
100  
101  
114  
83  
97  
117  
103  
101  
114

You can also see that the length is 22, not 25.

Also if I  
puts a.encode(‘UTF-8’, ‘ISO-8859-1’)  
I see the proper character in my terminal

But when read from a file:

$ cat test.rb  
File.open(‘test.in’, ‘r:ISO-8859-1’).each\_line do |l|  
puts l  
puts ‘_**’  
puts l.length  
puts '**_’  
l.each\_byte {|b| puts b}  
end

$ ruby test.rb  
Der gro\xdfe BilderSauger

* * *

25

* * *

68  
101  
114  
32  
103  
114  
111  
92 ← Here  
120 ← we  
100 ← are  
102 ← as 4 ASCII chars ‘\xdf’  
101  
32  
66  
105  
108  
100  
101  
114  
83  
97  
117  
103  
101  
114

I also tried to put UTF-8 codepoints and read as UTF-8 without luck. It  
seems there is no escape sequence when reading from a stream, which I  
can understand.

What I can’t figure out is how to interpret these escape sequences when  
reading them from a file.

–Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [February 17, 2011, 1:07am UTC](https://www.ruby-forum.com/t/ruby-1-9-2-unicode-with-hex-codepoint-problem/203029/6 "2011-02-17T01:07:53Z")

</div>

Stupid of me. The file is indeed ISO-8859-1 (some other characters are  
encoded this way) just not this one, it’s escaped.

This:  
l.gsub!(/\x([0-9a-f]{2})/i) { $1.hex.chr }

# or in ruby 1.9, if you want to tag the encoding:

l.gsub!(/\x([0-9a-f]{2})/i) { $1.hex.chr(“ISO-8859-1”) }

is exactly what I want.

Thanks.
