从字节数组字符串不使用C#得到修整? [英] String from byte array doesn't get trimmed in C#?

查看:118
本文介绍了从字节数组字符串不使用C#得到修整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的字节数组(16字节):

I have a byte array similar to this (16 bytes):

71 77 65 72 74 79 00 00 00 00 00 00 00 00 00 00

我用这个将其转换为一个字符串,并修剪结局空间:

I use this to convert it to a string and trim the ending spaces:

ASCIIEncoding.ASCII.GetString(data).Trim();

我得到的字符串精细,但它仍然有所有的结束空间。
所以我得到的东西像QWERTY ..........(其中圆点是由于计算器空格)。

I get the string fine, however it still has all the ending spaces. So I get something like "qwerty.........." (where dots are spaces due to StackOverflow).

我在做什么错了?

我也试过用.TrimEnd(),并使用UTF8编码,但它不会改变任何东西。

I also tried to use .TrimEnd() and to use an UTF8 encoding, but it doesn't change anything.

在此先感谢:)

推荐答案

您需要做的 TrimEnd(新的char [] {(char)的0}); 修复这个。这不是空间 - 它实际上是那些古怪转换空字符。我有这个问题了。

You have to do TrimEnd(new char[] { (char)0 }); to fix this. It's not spaces - it's actually null characters that are converted weirdly. I had this issue too.

这篇关于从字节数组字符串不使用C#得到修整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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