我知道这是编程史上最常见的问题,但我真的相信我现在已经尝试过但我仍然得到:incompatible character encodings: UTF-8 and ASCII-8BIT在我的页面#index.html文件中.
我正在运行Ruby 1.9.2和Rails 3.1.1.
这是我验证/测试/设置的内容:
在database.yml中,设置encoding:utf8
开发:适配器:mysql编码:utf8重新连接:false数据库:kopa_development pool:5用户名:root密码:host:localhost
使用Notepad ++检查术语中的文本文件是否编码为utf-8
一个有趣的事情是,如果我将index.html.erb设置为ANSI,它会抱怨application.html.erb没有正确的编码(而不是index.html.erb).我不知道这意味着什么,但它让我觉得编辑有些可疑.
我记得在使用电子文本编辑器时,我可以设置两件事(参考编码)编码和其他东西.那个"别的"我不记得它是什么,我不再有了.
关于错误消息,我是否正确理解,首先提到的是数据库编码,第二个是Rails(?)编码?我也看到了相反的错误消息(ASCII first和UTF-8 after).
编辑:我今天早上用Google搜索了一些,发现mysql-gem有问题.文章有点旧,我尝试了解决方案(如尝试mysql2和ruby-mysql)没有更好的运气.
我完全没有想法.你呢?
我正在编写一个Java项目,用附件发送电子邮件.
在我的测试用例中,我将一些日语单词"some Hiraganas and Katakanas"添加到我附加的testfile.txt(我用UTF-8编码保存.)
但是当我打开附加的测试文件后,我将测试邮件发送给自己. txt,每个日本人的Chars都变成了"????".
所以我只是想知道为什么会发生这种情况......?
谢谢
艾伦
PS更具体,这是我的代码.我正在使用mail.jar发送电子邮件.
以下是我获取文件的方法:
/**
* Add an attachment to the Email.
* @param filePath
*/
public void setFile(String filePath){
attachment = new File(filePath);
}
Run Code Online (Sandbox Code Playgroud)
以下是我如何将文件附加到我的MIME电子邮件部分.
/*Add attachment if an attachment is given.*/
if(attachment != null){
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(attachment);
multipart.addBodyPart(attachmentPart);
}
Run Code Online (Sandbox Code Playgroud) 我有一个XML文档,其中包含特殊字符,如'%',回车符,换行符,&,<,>,',".我试图对整个xml文档进行编码,但在使用xmldocument.load方法时无法加载在C#中.
删除这些特殊字符的最佳方法是什么,而不必硬编码以用相应的实体引用替换非法字符?
我想以最快的方式对文本文件进行编码,只是让人类无法读取.
也许可以翻转每个字节的第一位,或者可以非常快速地完成某些操作,这样就不会减慢脚本的速度.文本文件的长度将是几MB,因此无论哪种方法都可以使其无法读取.
我想将以下原始邮件转换为正常的UTF-8文本:
=?UTF-8?B?UmU6ICMyLUZpbmFsIEFjY2VwdGFuY2UgdGVzdCB3aXRoIG5ldyB0ZXh0IHdpdGggU2xvdg ==?= =?UTF-8?B?YWsgaW50ZXJwdW5jdGlvbnMgIivEvsWhxI3FpcW + w73DocOtw6khxYgi?=
真实的文本是(是的,其中有一些疯狂的变音符号):
回复:#2 - 最终验收测试,带有斯洛伐克语中间节点的新文本"+ľščťžýáíé!"
我处理邮件主题的方式:
function subject_imapUtf8($str){
$conv = '';
$subParts = preg_split('/[\r\n]+/',$str);
for($i=0;$i<count($subParts);$i++){
$conv .= imap_utf8(trim($subParts[$i]));
}
return $conv;
}
Run Code Online (Sandbox Code Playgroud)
这个例子给了我:
=?UTF-8?B?UmU6ICMyLUZpbmFsIEFjY2VwdGFuY2UgdGVzdCB3aXRoIG5ldyB0ZXh0IHdpdGggU2xvdg ==?= ak interpunctions"+ľščťžýáíé!"
因此,您可以看到主题的第二部分/行被转换为correclty.
我需要更改什么才能正确转换第一部分?
我已将excel 2007文档导出为CSV(以分号分隔).我正在使用CZECH office 2010和捷克Windows 7.
当我在.net C#中读取文件时,带有特殊捷克符号的文本已损坏.就在我使用的时候
像string [] lines = file.readalllines(path); (来自System.IO.File)
所以我想我需要专门提供正确的编码,对吗?所以我试过了:
string[] lines = File.ReadAllLines(path,encoding);
Run Code Online (Sandbox Code Playgroud)
编码变量被定义为
Encoding encoding = Encoding.UTF8 for example.
Run Code Online (Sandbox Code Playgroud)
没有选项有效.最奇怪的是,其中一些,比如Encoding.Unicode甚至扔了
IndexOutOfRandgeException
Run Code Online (Sandbox Code Playgroud)
.
我该如何解决这个编码问题?谢谢.
顺便说一句,我的办公室设法以正确的方式打开和阅读该文件.
我正在运行Ubuntu 10.04 LTS,Python 2.6.5(r265:79063,2010年4月16日,13:09:56)
>>> m = 'Šiven'
>>> m
'\xa6iven'
>>> unicode(m)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa6 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我应该如何正确设置它(编码,解码),以便它准确地写出它读取的内容?
说我有一个这样的字符串
"title": "\u041e\u0434\u0440\u0438\u043d\u044f-\u0423\u0440\u0431\u043e\u0432\u0430 ...
Run Code Online (Sandbox Code Playgroud)
什么是让它恢复正常的最佳方法?
我试过了
byte[] b = stringBuilder.toString().getBytes();
String jstring = new String(b,"UTF-8");
Run Code Online (Sandbox Code Playgroud)
但没有运气,它保持不变.
谢谢.
我正在尝试根据RFC 5849 OAuth 1.0将百分比编码为%C3%A5
http://tools.ietf.org/rfc/rfc5849.txt
这可以在GoCardless Ruby规范中看到 https://github.com/gocardless/gocardless-ruby/blob/master/spec/utils_spec.rb
it "encodes non-ascii alpha characters" do
subject["å"].should == "%C3%A5"
end
Run Code Online (Sandbox Code Playgroud)
我的C#代码如下所示:
private const string UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
public static string PercentEncode(string value)
{
var input = new StringBuilder();
foreach (char symbol in value)
{
if (UnreservedChars.IndexOf(symbol) != -1)
{
input.Append(symbol);
}
else
{
input.Append('%' + String.Format("{0:X2}", (int)symbol));
}
}
return input.ToString();
}
Run Code Online (Sandbox Code Playgroud)
这些测试失败了:
[Test]
public void It_encodes_non_ascii_alpha_characters()
{
Util.PercentEncode("å").ShouldBe("%C3%A5");
}
Expected string length 6 but was 3. Strings differ at index 1. …Run Code Online (Sandbox Code Playgroud) 我在数据库中有一个包含varbinary数据的AttachmentFile列.在c#中,我将它存储在byte []数组中.我需要将此内容作为字符串显示给用户.我尝试了几种不同的方法将这个字节数组转换为字符串,但似乎没有任何效果.
while (rdr.Read())
{
string name = rdr["AttachmentFileName"].ToString();
string mime = rdr["AttachmentMIMEType"].ToString();
byte[] content = (byte[])rdr["AttachmentFile"];
string contentStr = (???)ConvertToString(content);
r.AddHeader("Content-Disposition", "attachment; filename=" + name);
r.ContentType = mime;
r.Write(contentStr);
}
Run Code Online (Sandbox Code Playgroud)
string contentStr = Encoding.Default.GetString(content, 0, 10000);
string contentStr = Encoding.UTF8.GetString(content, 0, 10000);
content = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, content);
string contentStr = Encoding.UTF8.GetString(content, 0, 10000);
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string contentStr = enc.GetString(content);
string contentStr = System.Convert.ToBase64String(content);
Run Code Online (Sandbox Code Playgroud)
但是上面没有一个给我一个清晰的字符串.有任何想法吗?