为什么不byte []来字符串和返回按预期工作 [英] why doesn't byte[] to string and back work as expected
问题描述
我有以下代码:
Int32 i1 = 14000000;
byte[] b = BitConverter.GetBytes(i1);
string s = System.Text.Encoding.UTF8.GetString(b);
byte[] b2 = System.Text.Encoding.UTF8.GetBytes(s);
Int32 i2 = BitConverter.ToInt32(b2,0);;
i2等于-272777233。
为什么不是输入值? (14000000)?
i2 is equal to -272777233. Why isn't it the input value? (14000000) ?
编辑:我要执行的操作是将其附加到另一个字符串,然后使用WriteAllText将其写入文件
what I am trying to do is append it to another string which I'm then writing to file using WriteAllText
推荐答案
因为 Encoding
类不能满足任何需求。如果字符(在UTF-8中可能是几个字节)在该特定字符集中(在您的情况下是UTF-8)不是有效字符,它将使用替换字符。
Because an Encoding
class is not going to just work for anything. If a "character" (possibly a few bytes in case of UTF-8) is not a valid character in that particular character set (in your case UTF-8), it will use a replacement character.
单个问号(U + 003F)
a single QUESTION MARK (U+003F)
(来源: http://msdn.microsoft.com/ zh-cn / library / ms404377.aspx#FallbackStrategy )
在某些情况下,它只是一个?
,例如在ASCII / CP437 / ISO 8859-1中,但是您可以选择一种处理方式。 (请参见上面的链接)
Some case it is just a ?
, for example in ASCII/CP437/ISO 8859-1, but there is a way for you to choose what to do with it. (See the link above)
例如,如果您尝试将(byte)128
转换为ASCII:
For example if you try to convert (byte)128
to ASCII:
string s = System.Text.Encoding.ASCII.GetString(new byte[] { 48, 128 }); // s = "0?"
然后将其转换回去:
byte[] b = System.Text.Encoding.ASCII.GetBytes(s); // b = new byte[] { 48, 63 }
您将不获取原始字节数组。
You will not get the original byte array.
这可以作为参考:检查字符是否在编码中
我无法想象为什么需要将字节数组转换为字符串。显然这没有任何意义。假设您要写入流,则可以直接写入 byte []
。如果需要在某种文本表示形式中使用它,则只需将 yourIntegerVar.ToString()
转换为字符串并使用 int .TryParse
找回。
I can't imagine why you would need to convert a byte array to a string. It obviously doesn't make any sense. Let's say you're going to write to a stream, you could just directly write byte[]
. If you need to use it in some text representation, it makes perfect sense to just convert it to a string by yourIntegerVar.ToString()
and use int.TryParse
to get it back.
编辑:
您可以将字节数组写入文件,但是您不会将字节数组连接为字符串并使用惰性方法 File.WriteAllText
,因为它将处理编码转换,您可能最终会遇到问号?
全部在您的文件上。而是打开 FileStream
并使用 FileStream.Write
直接写入字节数组。或者,您可以使用 BinaryWriter
直接以其二进制形式(以及字符串)写入一个整数,并使用其对应的 BinaryReader
读回。
You can write a byte array to a file, but you are not going to "concatenate" the byte array to a string and use the lazy method File.WriteAllText
because it is going to handle the encoding conversion and you will probably end up having question marks ?
all over your file. Instead, Open a FileStream
and use FileStream.Write
to directly write the byte array. Alternatively, you can use a BinaryWriter
to directly write an integer in its binary form (and also a string) and use its counterpart BinaryReader
to read it back.
示例:
FileStream fs;
fs = File.OpenWrite(@"C:\blah.dat");
BinaryWriter bw = new BinaryWriter(fs, Encoding.UTF8);
bw.Write((int)12345678);
bw.Write("This is a string in UTF-8 :)"); // Note that the binaryWriter also prefix the string with its length...
bw.Close();
fs = File.OpenRead(@"C:\blah.dat");
BinaryReader br = new BinaryReader(fs, Encoding.UTF8);
int myInt = br.ReadInt32();
string blah = br.ReadString(); // ...so that it can read it back.
br.Close();
此示例代码将生成与以下十六进制转储匹配的文件:
This example code will result in a file which matches the following hexdump:
00 4e 61 bc 00 1c 54 68 69 73 20 69 73 20 61 20 73 Na¼..This is a s
10 74 72 69 6e 67 20 69 6e 20 55 54 46 2d 38 20 3a tring in UTF-8 :
20 29 )
请注意, BinaryWriter.Write(string)
还会在字符串前面加上长度,并且在回读时取决于字符串,因此不适合使用文本编辑器来编辑结果文件。 (那么您正在以二进制形式编写一个整数,因此我希望这是可以接受的吗?)
Note that BinaryWriter.Write(string)
also prefix the string with its length and it depends on it when reading back, so it is not appropriate to use a text editor to edit the resulting file. (Well you are writing an integer in its binary form so I expect this is acceptable?)
这篇关于为什么不byte []来字符串和返回按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!