Question

How can I insert ASCII special characters (e.g. with the ASCII value 0x01) into a string?

I ask because I am using the following:

str.Replace( "<TAG1>", Convert.ToChar(0x01).ToString() );

and I feel that there must be a better way than this. Any Ideas?

Update:

Also If I use this methodology, do I need to worry about unicode & ASCII clashing?

Was it helpful?

Solution

I believe you can use \uXXXX to insert specified codes into your string.

ETA: I just tested it and it works. :-)

using System;
class Uxxxx {
    public static void Main() {
        Console.WriteLine("\u20AC");
    }
}

OTHER TIPS

Also If I use this methodology, do I need to worry about unicode & ASCII clashing?

Your first problem will be your tags clashing with ASCII. Once you get to TAG10, you will clash with 0x0A: line feed. If you ensure that you will never get more than nine tags, you should be safe. Unicode-encoding (or rather: UTF8) is identical to ASCII-encoding when the byte-values are between 0 and 127. They only differ when the top-bit is set.

and I feel that there must be a better way than this. Any Ideas?

It looks as if you're trying to manipulate a binary chunk using textual tools. If you want to insert the byte 0x01, for example, you're not manipulating text anymore, since you don't care what that byte might represent, and since it looks like you don't even care which encoding you'll be outputting.

A better way would be to treat the thing you're manipulating as a binary chunk of data, which would let you insert bits and bytes easily, without using brittle workarounds and worrying about side effects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top