Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write ASCII special characters in a .Net string

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.

Thanks for any help you can give.

like image 884
JimDaniel Avatar asked May 27 '10 15:05

JimDaniel


People also ask

How do you type special characters in ASCII?

Inserting ASCII characters To insert an ASCII character, press and hold down ALT while typing the character code. For example, to insert the degree (º) symbol, press and hold down ALT while typing 0176 on the numeric keypad. You must use the numeric keypad to type the numbers, and not the keyboard.

How do you write special characters in C#?

Special Characters string text = "This is a "string" in C#. "; C# includes escaping character \ (backslash) before these special characters to include in a string. Use backslash \ before double quotes and some special characters such as \,\n,\r,\t, etc. to include it in a string.

How do I get the ASCII value of a character in C#?

To display Unicode (ASCII) characters in HTML use &#XXX; in C# use Convert. ToChar(XXX), in VBScript, VB.NET and PHP use the chr(XXX) function, in JavaScript use String. fromCharCode(XXX), where XXX is the entity number.

How do I get the Ascii code for a string?

ASCII value of a String is the Sum of ASCII values of all characters of a String. Step 1: Loop through all characters of a string using a FOR loop or any other loop. Step 3: Now add all the ASCII values of all characters to get the final ASCII value.


2 Answers

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.

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.

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.

like image 101
Christian Hayter Avatar answered Sep 28 '22 03:09

Christian Hayter


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.

like image 34
Goyuix Avatar answered Sep 28 '22 03:09

Goyuix