我将字符串转换为UTF-16编码字符的byteArray没有问题,但我尝试与之通信的应用程序(用Erlang编写)只能理解Latin-1编码.有没有办法从Actionscript 3中的字符串生成一个完整的Latin-1字符代码的byteArray?
我有一个编码为iso-8859-1的文件,其中包含ô等字符.
我正在使用java代码读取此文件,例如:
File in = new File("myfile.csv");
InputStream fr = new FileInputStream(in);
byte[] buffer = new byte[4096];
while (true) {
int byteCount = fr.read(buffer, 0, buffer.length);
if (byteCount <= 0) {
break;
}
String s = new String(buffer, 0, byteCount,"ISO-8859-1");
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
然而,ô字符总是乱码,通常打印为?.
我已经阅读了这个主题(并且在路上学到了一点),例如
但仍然无法使这个工作
有趣的是,这适用于我的本地电脑(xp),但不适用于我的Linux机箱.
我已经检查过我的jdk支持所需的字符集(它们是标准的,所以这并不令人惊讶)使用:
System.out.println(java.nio.charset.Charset.availableCharsets());
Run Code Online (Sandbox Code Playgroud) 大约2年前,我错误地使用iso-8859-1启动了一个大型网站.我现在遇到一些字符问题,特别是在使用ajax将数据发送到服务器时.因此,我想切换到使用UTF-8.
你看到了什么问题?我知道我必须搜索网站以查找需要更改的字符?他们的真实角色.但是,这样做还有其他风险吗?有没有人这样做过?
我记得当我以前在日本开发网站时 - 有三种不同的字符编码货币 - 开发人员有一个技巧来"强制"编码源文件,所以它总是在他们的IDE中以正确的编码打开.
他们所做的是在包含日语字符的文件顶部放置注释,该日语字符仅存在于该特定字符编码中 - 它不在任何其他字符编码中!这非常有效.
我记得这一点,因为现在我有一个类似的,虽然是英语国家的问题.
我有一些文件必须是ISO-8859-1,但在我的编辑器(Linux上的Bluefish 1.0.7)中保持打开为UTF-8.除了磅(£)符号和诸如此类的东西之外,这通常不是问题.不要误会我的意思,我可以修复文件并将其另外保存为ISO-8859-1,但我希望它始终在我的编辑器中以ISO-8859-1打开.
那么,有没有任何类型的角色黑客 - 就像我上面提到的 - 这样做?还是其他任何方法?
PS.Unicode倡导者/福音传道者不必浪费时间试图转换我,因为我已经是其中之一!这是我继承的摇摇欲坠的旧系统:-(
PPS.请不要说"使用不同的编辑器"因为我是一个老屁并按我的方式设置:-)
我正在尝试使用VS 2008,.net 3.5,WCF(环境:Windows XP和VS 2008)来使用外部Web服务(Web服务具有PHP实现).我添加服务引用到Web服务,VS生成WCF代理.
绑定是basicHttpBinding.
我调用Web Service中的方法,使用Proxy,然后我开始收到ProtocolException,我收到以下错误信息:
System.ServiceModel.ProtocolException:内容类型text/xml; charset =响应消息的ISO-8859-1与绑定的内容类型(text/xml; charset = utf-8)不匹配.如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法.
响应的前644个字节是:
这很成功
好吧,我需要用iso-8859-1编码来调用服务.
任何有用的示例源代码都可以解决吗?
更新:
WCF中使用的默认编码器仅适用于UTF-8和UTF-16(大端和小端).
如果我在app.config中使用textEncoding ="iso-8859-1",
我收到此错误:
System.ArgumentException:No se admitelacodificacióndetexto'iso-8859-1'usada en el formato de mensaje de texto.Nombredelparámetro:编码.
System.ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding(Encoding encoding)System.ServiceModel.Channels.TextMessageEncodingBindingElement.set_WriteEncoding(Encoding value)System.ServiceModel.BasicHttpBinding.set_TextEncoding(Encoding value)System.ServiceModel.Configuration.BasicHttpBindingElement.OnApplyConfiguration(Binding binding) System.ServiceModel.Configuration.StandardBindingElement.ApplyConfiguration(Binding binding)System.ServiceModel.Description.ConfigLoader.LookupBinding(String bindingSectionName,String configurationName,ContextInformation context)System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint,String configurationName)System. ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName,EndpointAddress address)ctor(String endpointConfigurationName,EndpointAddress remoteAddress)CreateSimplexFactory()CreateChannelFactory()CreateChannelFactoryRef(EndpointTrait`1 endpointTrait)InitializeChannelFactoryRef()ctor()IntegracionEasyVista.ServiceEasyVista.WebServicePortTypeClient..ctor()in
更新:
调用从WCF使用ISO-8859-1编码的Web服务
这个MSDN页面(http://msdn.microsoft.com/en-us/library/ms751486(v=VS.90).aspx)展示了如何创建一个"CustomTextEncoder",它可以支持超过utf-8,utf- 16和unicode编码.它包含完整的示例源代码,对我来说非常有用.
我使用CustomTextMessageEncodingElement,但是我收到错误:
内容类型text/xml; charset = ISO-8859-1的响应消息与绑定的内容类型不匹配(text/xml; charset = iso-8859-1).如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法.响应的前1024个字节是:**
从Samples MSDN的代码中,我修改了CustomTextMessageEncoder类的构造函数:
public CustomTextMessageEncoder(CustomTextMessageEncoderFactory factory)
{
writerSettings = new XmlWriterSettings();
writerSettings.Encoding …Run Code Online (Sandbox Code Playgroud) 如果上述任何一个答案都是否定的,那么不相交的字符是什么?我正在测试一些检测字符集的逻辑,并希望编写测试来验证检测是否正常工作.
我有一个字符串$ data,用utf-8编码.我假设我不知道这个字符串是utf-8还是iso-8859-1.我想使用Perl Encode :: Guess模块来查看它是否是其中之一.我无法弄清楚这个模块是如何工作的.
我尝试了以下四种方法(来自http://perldoc.perl.org/Encode/Guess.html):
use Encode::Guess qw/utf8 latin1/;
my $decoder = guess_encoding($data);
print "$decoder\n";
Run Code Online (Sandbox Code Playgroud)
结果: iso-8859-1或utf8
use Encode::Guess qw/utf8 latin1/;
my $enc = guess_encoding($data, qw/utf8 latin1/);
ref($enc) or die "Can't guess: $enc";
my $utf8 = $enc->decode($data);
print "$utf8\n";
Run Code Online (Sandbox Code Playgroud)
结果:无法猜测:iso-8859-1或utf8 at encodage-windows.pl第25行,第18110行.
use Encode::Guess qw/utf8 latin1/;
my $decoder = Encode::Guess->guess($data);
die $decoder unless ref($decoder);
my $utf8 = $decoder->decode($data);
print "$utf8\n";
Run Code Online (Sandbox Code Playgroud)
结果: iso-8859-1或utf8 at encodage-windows.pl第30行,第18110行.
use Encode::Guess qw/utf8 latin1/;
my $utf8 = Encode::decode("Guess", $data);
print "$utf8\n";
Run Code Online (Sandbox Code Playgroud)
结果: …
我扫描了Rust文档,以便在字符编码之间进行转换,但没有找到任何内容.我错过了什么?
是否由Rust语言及其标准库支持(直接或间接),甚至计划在不久的将来?
由于其中一个答案表明有一个简单的解决方案,因为u8可以转换为(Unicode)chars.Unicode是ISO-8859-1中代码点的超集,即1:1映射,它编码为UTF-8中的多个字节,这是StringRust 中s 的内部编码.
fn main() {
println!("{}", 196u8 as char);
println!("{}", (196u8 as char) as u8);
println!("{}", 'Ä' as u8);
println!("{:?}", 'Ä'.to_string().as_bytes());
println!("{:?}", "Ä".as_bytes());
println!("{}",'Ä' == 196u8 as char);
}
Run Code Online (Sandbox Code Playgroud)
得到:
Ä
196
196
[195, 132]
[195, 132]
true
Run Code Online (Sandbox Code Playgroud)
哪个我甚至没有考虑过工作!
我正在为一些受限制的环境编写自己的BASE64编码器/解码器.
我发现Base64.Encoder#encodeString说它ISO-8859-1用于从那些编码的字节构造一个String.
我完全假设ISO-8859-1charset也涵盖了所有base64字母.
有没有可能的理由不使用US-ASCII?
有了ord(ch)你可以得到字符数字代码ch最多127.是否有任何函数返回0-255之间的数字,所以还要覆盖ISO 8859-1字符?
编辑:按照我上一版本的代码和错误得到
#!/usr/bin/python
# coding: iso-8859-1
import sys
reload(sys)
sys.setdefaultencoding('iso-8859-1')
print sys.getdefaultencoding() # prints "iso-8859-1"
def char_code(c):
return ord(c.encode('iso-8859-1'))
print char_code(u'à')
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:TypeError:ord()期望一个字符,但找到长度为2的字符串