Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8196211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:24:27+00:00 2026-06-07T05:24:27+00:00

I know there are multiple similar questions about this error, and I’ve tried many

  • 0

I know there are multiple similar questions about this error, and I’ve tried many of them without luck. The problem I’m having involves the byte \xA1 and is throwing

ArgumentError: invalid byte sequence in UTF-8

I’ve tried the following with no success:

"\xA1".encode('UTF-8', :undef => :replace, :invalid => :replace,
    :replace => "").sub('', '')
"\xA1".encode('UTF-8', :undef => :replace, :invalid => :replace,
    :replace => "").force_encoding('UTF-8').sub('', '')
"\xA1".encode('UTF-8', :undef => :replace, :invalid => :replace,
    :replace => "").encode('UTF-8').sub('', '')

Each line throws the error for me. What am I doing wrong?

UPDATE:

The above lines fail only in IRB. However, I modified my application to encode lines of a CVS file using the same String#encode method and arguments, and I get the same error when reading the line from a file (note: it works if you perform the operations on the same string w/o using IO).

bad_line = "col1\tcol2\tbad\xa1"

bad_line.sub('', '') # does NOT fail
puts bad_line # => col1 col2    bad?

tmp = Tempfile.new 'foo' # write the line to a file to emulate real problem
tmp.puts bad_line
tmp.close

tmp2 = Tempfile.new 'bar'

begin
  IO.foreach tmp.path do |line|
    line.encode!('UTF-8', :undef => :replace, :invalid => :replace, :replace => "")
    line.sub('', '') # fail: invalid byte sequence in UTF-8
    tmp2.puts line
  end
  tmp2.close

  # this would fail if the above error didn't halt execution
  CSV.foreach(tmp2.path) do |row|
    puts row.inspect # fail: invalid byte sequence in UTF-8
  end
ensure
  tmp.unlink
  tmp2.close
  tmp2.unlink
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:24:29+00:00Added an answer on June 7, 2026 at 5:24 am

    It would seem that ruby thinks that the string encoding is already utf8, so when you do

    line.encode!('UTF-8', :undef => :replace, :invalid => :replace, :replace => "")
    

    it doesn’t actually do anything because the destination encoding is the same as the current encoding (at least that’s my interpretation of the code in transcode.c)

    The real question here is whether your starting data is valid in some encoding that isn’t utf-8 or whether this is data that is supposed to be utf-8 but has a few warts in it that you want to discard.

    In the first case, the correct thing to do is tell ruby what this encoding is. You can do this when you open the file

    File.open('somefile', 'r:iso-8859-1')
    

    will open the file, interpreting its contents as iso-8859-1

    You can even get ruby to transcode for you

    File.open('somefile', 'r:iso-8859-1:utf-8')
    

    will open the file as iso-8859-1, but when you read data from it the bytes will be converted to utf-8 for you.

    You can also call force_encoding to tell ruby what a string’s encoding is (this doesn’t modify the bytes at all, it just tells ruby how to interpret them).

    In the second case, where you just want to dump whatever nasty stuff has got into your utf-8, you can’t just call encode! as you have because that’s a no-op. In ruby 2.1 and higher, you can use String#scrub, in previous versions you can do this

    line.encode!('UTF-16', :undef => :replace, :invalid => :replace, :replace => "")
    line.encode!('UTF-8')
    

    We first convert to utf-16. Since this is a different encoding, ruby will actually replace our invalid sequences. We can then convert back to utf-8. This won’t lose us any extra data because utf-8 and utf-16 are just two different ways of encoding the same underlying character set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there is a similar question to this one Multiple Jquery modal Dialog
I know there are other posts similar to this one, but none of them
I know there are many other posts on here with similar questions, but I
I know a great thing about perl is that there's multiple ways of doing
Disclaimer: there are many similar questions on SO, but I am looking for a
Disclaimer: I tried to search for similar question, however this returned about every C++
I know there are very similar questions and answers posted. And I have used
I have a similar problem as this question . I have multiple web services
I know, there are already a lot of similar questions here but I did
I know there are multiple solutions online, but some are for windows, some are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.