Encoding::UndefinedConversionError: "\xE5" from ASCII-8BIT to UTF-8
程序员文章站
2022-07-05 12:17:17
...
在做编码转换时,遇了这个问题。原代码如下:
str.encode('UTF-8') if str.encoding != 'UTF-8'
所以就遇到了错误:Encoding::UndefinedConversionError: "\xE5" from ASCII-8BIT to UTF-8
后来看了https://github.com/lassebunk/webcam_app/issues/1
将代码修改为以下,
str.force_encoding('UTF-8') if str.encoding != 'UTF-8'
这只是改了字符串的encoding值,实际数据并未转码。
问题等待解决中...
2011-08-01问题解决 ,代码:
str = str.force_encoding('GBK') str = str.encode('UTF-8')
思考:
当str.encoding == 'ASCII-8BIT'时,ruby并不知道这个str的编码。我用其他编辑器得知它的编码是GBK,所以我先强制将它标记为GBK,再转为UTF-8, 就成功了。
推荐阅读
-
Encoding::UndefinedConversionError: "\xE5" from ASCII-8BIT to UTF-8
-
Encoding::UndefinedConversionError: "\xE5" from ASCII-8BIT to UTF-8
-
linux svn 检出更新时报错svn: Can't convert string from 'UTF-8' to native encoding:解决办法
-
linux svn 检出更新时报错svn: Can't convert string from 'UTF-8' to native encoding:解决办法
-
linux svn 检出更新时报错svn: Can't convert string from 'UTF-8' to native encoding:
-
linux svn 检出更新时报错svn: Can't convert string from 'UTF-8' to native encoding: