Suppose I want to use the ASCII special character FS(0x1C) in a .Net string, and then be able to format a byte array from that same string with the special character properly represented as a single byte, how would I do that? I can't seem to get my head around it.
假设我想在.Net字符串中使用ASCII特殊字符FS(0x1C),然后能够从同一个字符串格式化字节数组,并将特殊字符正确表示为单个字节,我该怎么做?我似乎无法理解它。
Thanks for any help you can give.
谢谢你提供的所有帮助。
3 个解决方案
#1
3
It rather depends on the nature of the serial port data format. If the data consists of mostly ASCII text characters interspersed with the occasional control character, then you can embed them in the string, e.g.
它取决于串行端口数据格式的性质。如果数据主要由偶然包含控制字符的ASCII文本字符组成,那么您可以将它们嵌入到字符串中,例如,
var data1 = Encoding.ASCII.GetBytes("Foo\x1CBar\x1CBaz");
However, if the data consists of several fields of various data types, then the BitConverter
class may be more useful, e.g.
但是,如果数据由各种数据类型的几个字段组成,则BitConverter类可能更有用,例如,
var data2 = new List<byte>();
// Add an int value
data2.AddRange(BitConverter.GetBytes(6));
// Add a control character
data2.Add(0x1C);
// Add an ASCII-encoded string value
data2.AddRange(Encoding.ASCII.GetBytes("Hello"));
As others have pointed out, ASCII is not the only string encoding you could use, but from a serial port it is the most likely.
正如其他人所指出的那样,ASCII并不是你可以使用的唯一字符串编码,但从串口开始,它最有可能。
#2
3
Here is how to embed it in a string:
以下是如何将其嵌入字符串中:
"\x1C"
Is the string representation of that single character. Suppose you wanted to get it out as an array of bytes:
是该单个字符的字符串表示形式。假设你想把它作为一个字节数组得到它:
byte[] bytes = Encoding.ASCII.GetBytes("\x1C");
And if you wanted to get a string from an array of bytes:
如果你想从一个字节数组中获取一个字符串:
string s = Encoding.ASCII.GetString(bytes);
Be aware that there are also more encodings, like UTF8, in the Encoding namespace that might work better depending on your neeed - though reading from a serial line you probably want plain ASCII.
请注意,编码命名空间中还有更多编码,如UTF8,可能会根据您的需要更好地工作 - 尽管从串行线读取您可能需要纯ASCII。
#3
1
You should look at the ASCIIEncoding class http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx If you want to deal with ASCII. .Net strings are unicode (UCS2). If you want a specific encoding you need to use one of the encoding classes.
您应该查看ASCIIEncoding类http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx如果您想处理ASCII。 .Net字符串是unicode(UCS2)。如果需要特定编码,则需要使用其中一种编码类。
You can embed a non printable character by escaping it "My string \x1c"
您可以通过转义“我的字符串\ x1c”来嵌入不可打印的字符
#1
3
It rather depends on the nature of the serial port data format. If the data consists of mostly ASCII text characters interspersed with the occasional control character, then you can embed them in the string, e.g.
它取决于串行端口数据格式的性质。如果数据主要由偶然包含控制字符的ASCII文本字符组成,那么您可以将它们嵌入到字符串中,例如,
var data1 = Encoding.ASCII.GetBytes("Foo\x1CBar\x1CBaz");
However, if the data consists of several fields of various data types, then the BitConverter
class may be more useful, e.g.
但是,如果数据由各种数据类型的几个字段组成,则BitConverter类可能更有用,例如,
var data2 = new List<byte>();
// Add an int value
data2.AddRange(BitConverter.GetBytes(6));
// Add a control character
data2.Add(0x1C);
// Add an ASCII-encoded string value
data2.AddRange(Encoding.ASCII.GetBytes("Hello"));
As others have pointed out, ASCII is not the only string encoding you could use, but from a serial port it is the most likely.
正如其他人所指出的那样,ASCII并不是你可以使用的唯一字符串编码,但从串口开始,它最有可能。
#2
3
Here is how to embed it in a string:
以下是如何将其嵌入字符串中:
"\x1C"
Is the string representation of that single character. Suppose you wanted to get it out as an array of bytes:
是该单个字符的字符串表示形式。假设你想把它作为一个字节数组得到它:
byte[] bytes = Encoding.ASCII.GetBytes("\x1C");
And if you wanted to get a string from an array of bytes:
如果你想从一个字节数组中获取一个字符串:
string s = Encoding.ASCII.GetString(bytes);
Be aware that there are also more encodings, like UTF8, in the Encoding namespace that might work better depending on your neeed - though reading from a serial line you probably want plain ASCII.
请注意,编码命名空间中还有更多编码,如UTF8,可能会根据您的需要更好地工作 - 尽管从串行线读取您可能需要纯ASCII。
#3
1
You should look at the ASCIIEncoding class http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx If you want to deal with ASCII. .Net strings are unicode (UCS2). If you want a specific encoding you need to use one of the encoding classes.
您应该查看ASCIIEncoding类http://msdn.microsoft.com/en-us/library/system.text.asciiencoding.aspx如果您想处理ASCII。 .Net字符串是unicode(UCS2)。如果需要特定编码,则需要使用其中一种编码类。
You can embed a non printable character by escaping it "My string \x1c"
您可以通过转义“我的字符串\ x1c”来嵌入不可打印的字符