Ruby: character to ascii from a string
The c
variable already contains the char code!
"string".each_byte do |c|
puts c
end
yields
115
116
114
105
110
103
ASCII value of character in Ruby
The String#ord
method will do the trick:
ruby-1.9.2-p136 > 'x'.ord
=> 120
ruby-1.9.2-p136 > '0'.ord
=> 48
ruby-1.9.2-p136 > ' '.ord
=> 32
Getting an ASCII character code in Ruby using `?` (question mark) fails
Ruby before 1.9 treated characters somewhat inconsistently. ?a
and "a"[0]
would return an integer representing the character's ASCII value (which was usually not the behavior people were looking for), but in practical use characters would normally be represented by a one-character string. In Ruby 1.9, characters are never mysteriously turned into integers. If you want to get a character's ASCII value, you can use the ord
method, like ?a.ord
(which returns 97).
Convert string with hex ASCII codes to characters
You can use Array#pack
:
["666f6f626172"].pack('H*')
#=> "foobar"
H
is the directive for a hex string (high nibble first).
Replacing non standard characters in Ruby
If you want to remove non-ASCII chars, then
strings.map{| s | s.encode('ASCII', 'binary', invalid: :replace, undef: :replace, replace: '')}
What's the opposite of chr() in Ruby?
If String#ord didn't exist in 1.9, it does in 2.0:
"A".ord #=> 65
gsub ASCII code characters from a string in ruby
Update (2018): This code does not work in current Ruby versions. Please refer to other answers.
You can also try
s.gsub(/\xA0|\xC2/, '')
or
s.delete 160.chr+194.chr
How to check if a string contains ASCII code
\x
is just a hexadecimal escape sequence. It has nothing to do with encodings on its own. US-ASCII goes from "\x00"
to "\x7F"
(e.g. "\x41"
is the same as "A"
, "\x30"
is "0"
). The rest ("\x80"
to "\xFF"
) however are not US-ASCII characters since it's a 7-bit character set.
If you want to check if a string contains only US-ASCII characters, call String#ascii_only?
:
p "A\xC3B".ascii_only? # => false
p "\x41BC".ascii_only? # => true
Another example based on your code:
str = "A\xC3B"
unless str.ascii_only?
str.force_encoding(Encoding::ISO_8859_1).encode!(Encoding::UTF_8)
end
p str.encoding # => #<Encoding:UTF-8>
Related Topics
Why Pg::Uniqueviolation: Error: Duplicate Key Value Violates Unique Constraint
How Mix in Routes in Sinatra for a Better Structure
How to Avoid Putting the Magic Encoding Comment on Top of Every Utf-8 File in Ruby 1.9
Coding Style Checker or Code Formatter for Ruby/Rails
What Is the %W "Thing" in Ruby
Error Installing Nokogiri 1.6.0 on MAC (Libxml2)
Kernel#Gets Attempts to Read File Instead of Standard Input
Ruby 2.0 How to Uninclude a Module Out from a Module After Including It
Retrieving a Braintree Customer's Subscriptions
Ruby: Character to Ascii from a String
How to Use Jquery-Tokeninput and Acts-As-Taggable-On
Extracting the Last N Characters from a Ruby String
Cannot Load Such File -- Readline (Loaderror) When Running Heroku Create --Stack Cedar
Edit Each Line in a File in Ruby
Failed to Build Gem Native Extension