Victor
Victor

Reputation: 13368

Convert character to code in Ruby

I have the following string:

string = "Good « Bad"

The « has a code of X00AB. How can I convert string to "Good \X00AB Bad"?

I am doing this to search for \X00AB, then replace it to the nearest symbol, which is ". I don't want to search « because it's inaccurate.

Upvotes: 1

Views: 117

Answers (2)

kirushik
kirushik

Reputation: 1314

You can replace non-ASCII codepoints with their UTF code representations by using Array#pack:

string.gsub(/[^[:ascii:]]/) do |char|
  char.codepoints.pack("s>")
end # => "Good \x00\xAB Bad" 

But maybe it's better not to hardcode those conversions by hand, and use a standard tool for those conversions, iconv?

require 'iconv'
Iconv.conv('ASCII//TRANSLIT', 'UTF8', string) # => "Good << Bad"

Upvotes: 0

Martin
Martin

Reputation: 7714

You don't need to convert it at all. Unicode characters in Ruby can be used like this:

c = "\u00AB"

(notice that it's \u, not \X)

"\u00AB" and "«" are actually the same character:

"\u00AB" == "«"
=> true

So you can search as you wanted:

string = "Good « Bad"
string.include?("\u00AB")
=> true

Upvotes: 3

Related Questions