简体   繁体   English

C#将byte []转换为string []

[英]C# convert byte[] to string[]

I am coding in C# and willing to use unsafe/fixed. 我正在用C#编码,并愿意使用不安全/已修复。

I would like to be able to convert from a byte[] to a string[]. 我希望能够从byte []转换为string []。 I started with a file of strings (terminated by \\n). 我从一个字符串文件开始(以\\ n终止)。 I replaced all of the \\n with \\0 in the byte array that I read from the file. 我从文件中读取的字节数组中的所有\\ n都替换为\\ 0。 I thought I might now just reinterpret the byte[] as a string[] since the newlines are now \\0s. 我想我现在可能只是将byte []重新解释为字符串[],因为换行符现在为\\ 0s。 I think that makes sense, but I could be wrong. 我认为这是有道理的,但我可能是错的。 If I recall from C++ (decades ago unfortunately) a string[] is just a char[][] where each inner char[] is null terminated. 如果我从C ++回忆起(不幸的是几十年前),string []只是一个char [] [],其中每个内部char []均以null终止。 So, I think the code below could work if I could do the (fancycast). 因此,我认为下面的代码可以(如果执行fancycast)可以工作。

// File contains strings on each line //文件的每一行都包含字符串

byte[] bytes = ReadFile();
Replace(bytes, '\n', \0');
string[] strings = (fancycast)bytes

I don't know how to do the (fancycast). 我不知道该怎么做。 Thank you very much. 非常感谢你。

I know about all of the Streams and Readers in C# and I have specific reasons why I am not using them. 我了解C#中的所有Streams和Readers,并且有不使用它们的特定原因。 Please don't suggest a different design. 请不要建议其他设计。 I would just like to reinterpret cast the array. 我只想重新解释转换数组。 Thank you for your help. 谢谢您的帮助。

C# uses PASCAL strings, not C strings. C#使用PASCAL字符串,而不是C字符串。 Your best bet is probably to leave the \\n characters alone and doing a Split(). 最好的选择是将\\n字符保留下来并执行Split()。

byte[] bytes = ReadFile();
string oneBigString = Encoding.ASCII.GetString(bytes);
string[] lines = oneBigString.Split('\n');

If you just want to read a file in C# you could simply use: 如果您只想读取C#文件,则可以使用:

string text = System.IO.File.ReadAllText("PathToFile");

Or 要么

string[] lines = System.IO.File.ReadAllLines("PathToFile");

Otherwise simply create a string from bytes and split the string: 否则,只需从字节创建一个字符串并拆分字符串:

bytes[] = ReadFile();
string allData = System.Text.Encoding.<Encoding>.GetString(result);
string[] lines = allData.Split('\n');

try 尝试

System.Text.Encoding.Default.GetString(bytes);

But, you don't have to read the file as byte arrays and then convert it to string array in C#. 但是,您不必将文件读取为字节数组,然后在C#中将其转换为字符串数组。 Instead you can directly read as string / string array using ReadAllText(path) or ReadAllLines(path) respectively. 相反,您可以分别使用ReadAllText(path)或ReadAllLines(path)直接读取为字符串/字符串数组。

string allText = File.ReadAllText("file path");
string[] allLines = File.ReadAllLines("file path");

There is an important (REALLY important) thing to know about C# strings: They are immutable sequences of Unicode characters, and that's the only truly certain thing that you can say about them. 关于C#字符串,有一件非常重要的事情要知道:它们是Unicode字符的不可变序列,这是您可以说的唯一真正确定的事情。 As such you cannot make assumptions about how big any one character might be, and you cannot make assumptions about the byte offset of any character in the string. 因此,您不能假设任何一个字符的大小,也不能假设字符串中任何字符的字节偏移。

Well, you can make assumptions, and most of the time it'll probably work, but when it doesn't work it will be a massive pain to debug. 好吧,您可以做一些假设,并且在大多数情况下它可能会起作用,但是当它不起作用时,调试将非常痛苦。

A Unicode character can require 8, 16, or 32 bits. Unicode字符可能需要8、16或32位。 C# uses UTF-16 encoding for strings, which means that characters in the string are AT LEAST 16 bits. C#对字符串使用UTF-16编码,这意味着字符串中的字符至少为16位。 32-bit characters are part of the Unicode specification (eg: Emojis tend to live in the 32-bit space, like this one at 0x1F44C: 👌) and C# makes no promises about how the resulting string might be laid out in memory. 32位字符是Unicode规范的一部分(例如:表情符号倾向于存在于32位空间中,例如0x1F44C:👌),并且C#不保证结果字符串如何在内存中布置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM