字符串到字节数组然后字节数组到字符串转换错误 [英] string to byte array then byte array to string conversion error

查看:100
本文介绍了字符串到字节数组然后字节数组到字符串转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

/////////////////////////////////////// 
byte[] bytestring= somefunction(); 
//somefunction() return a byte array..
 string st1 =  Encoding.UTF32.GetString(bytestring);
 byte[] bytestring2 = Encoding.UTF32.GetBytes(st1);
 bool st =  bytestring2.Equals(bytestring);

// RESULT IS st = false  
// Desired result st = true
///////////////////////////////////////////////////



我得到st = false,但我认为应该是真的...

ablove代码中的问题我尝试了UTF8,UnicodeEncoding。但结果是一样的。

如果bytestring2有编码值那么如何解码它(如何得到我的旧价值)...



提前致谢

Hemant Singh [ ^ ]

推荐答案





String.Equals在字节数组上没有任何重载,当你将字节数组传递给equals函数时一个s对象,所以没有编译错误但是它返回false。



:)
Hi,

String.Equals don''t have any overload on byte array, when you pass byte array to equals function that treated as object, so no compilation error occur but it''s returns false.

:)


你必须知道什么在方法的引擎盖下进行

You must exactly know what is going on under the hood in method
byte[] bytestring= somefunction(); 





例如,它从特定的文本编码转换为字节数组,

和我有什么看来,你只是尝试用UTF编码转换它的字节,并检查它是否相等



For exampple what is it converted fron a specific text encoding to byte array,
and what i have seen, you just try to convert it bytes with UTF encoding , and check thei equality


这篇关于字符串到字节数组然后字节数组到字符串转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆