将字节数组转换为ascii的问题 [英] problem with casting byte array to ascii

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

问题描述

我使用字符串s = Encoding.ASCII.GetString(字节)输出应该是111187686557但是它多次给出了b $ b 111187686557 \ 0 \\\\\\\等等\ 0请问帮我?

字节数组显示10个值,其他值为0但转换为字符串时需要0作为\0帮助我

i am using string s=Encoding.ASCII.GetString(bytes) output shoul be 111187686557 but it gives
111187686557\0\0\0\0\ etc many times \0 plz help me?
byte array shows 10 value and other values are 0 but while converting to the string it takes 0 as \0 help me

推荐答案

为什么你认为应该给这个字符串?如果没有,那么存储在你的字节中的内容实际上并不是你所期望编码为ASCII的内容。



它可以是任何东西:1)你的原始字符串可以被编码不是ASCII,2)你的原始字符串可能真的包含很多零;这不是那种愚蠢的C风格的空终止字符串;在.NET字符串中,null有效; 3)它可能是其他任何东西。如果你必须序列化一些字符串,反序列化它并比较这个往返操作的结果,那将是一个不同的故事。如果您从字符串中显示代码 bytes ,我们可以考虑它。



-SA
Why do you think if should give this string? If it does not, the content stored in your bytes is really not what you expected encoded as ASCII.

It could be anything: 1) your original string could be encoded not as ASCII, 2) your original string could really contain many zeroes; this is not that silly C-style null-terminated string; in .NET strings, null is valid; 3) it could be anything else. If you had to serialize some string, deserialize it and compare the result of this round-trip operation, it would be a different story. If you show the code making bytes from your string, we can consider it.

—SA


你意识到



you do realise that

s=Encoding.ASCII.GetString(bytes)





也可以指定





can also be specified

s=Encoding.ASCII.GetString(bytes, offset, length)





抵消可能为0,根据SA的评论,它取决于你确定正确的长度



'g'



offset is likely 0, as per SA's comments, its up to you to determine the correct length

'g'


这篇关于将字节数组转换为ascii的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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