Boris Raznikov
Boris Raznikov

Reputation: 2443

.NET C# special character

I have a C# application in which I am trying to make a string that will contain near the end \0. I didn't manage to do that.
I did:

string msg = "GGGG..8";  
byte []b = System.Text.encoding.GetBytes(msg);

but in b where ".." are the representation is 46 and not 00. I want instead of .. to have \0\0
The final goal was to have the string contain the message.

Upvotes: 2

Views: 1100

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500525

Creating an appropriate string is easy:

string msg = "GGGG\0\08";

It's not clear what encoding you were trying to use in the second line, but I'd expect most single-byte encodings to encode that '\0' as a 0 byte.

If you want to take a message which originally includes periods and convert them to '\0' characters, that's easy too:

string msg = "GGGG..8";
string replaced = msg.Replace('.', '\0');

Upvotes: 5

Shadow Wizard
Shadow Wizard

Reputation: 66389

This should work:

string msg = "GGGG..8\0";

If no luck try this:

string msg = "GGGG..8" + '\0';

Upvotes: 0

Related Questions