Moritz
Moritz

Reputation: 153

C# wrong encoding

I ran in to some Problems when I try to do this:

public byte[] Login()
    {
        return Encoding.ASCII.GetBytes("WA\x01\x00\x00\x19\xf8\x05\x01\xa0\x8a\x84\xfc\x11iPhone-2.6.7-5222\x00\x08\xf8\x02\x96\xf8\x01\xf8\x01\x7e\x00\x07\xf8\x05\x0f\x5a\x2a\xbd\xa7");
    }

these bytes \x8a\x84 get both encoded to \xbf\xbf what I do not want. I already tried with Default Encoding, UTF and Unicode, but they all do not provide what I need.

This is a socket client.

Upvotes: 0

Views: 214

Answers (2)

Squirrelsama
Squirrelsama

Reputation: 5570

ASCII is not defined above value 127.

I would wonder why you are using a string intermediary to convert bytes to bytes.

You could, instead, do something like this and eliminate this problem altogether.

Encoding.ASCII.GetBytes("WA")
.Concat(new byte[] { 1, 2, 3, 4, 5 })
.Concat(Encoding.ASCII.GetBytes("iPhone-2.6.7-5222"))
.Concat(new byte[] { 6, 7, 8, 9, 10 })
.ToArray()

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503859

but they all do not provide what I need

Well we've no idea what you actually do need.

If you're trying to get the byte values 0x8a and 0x84 then I suggest you avoid using text to start with - it doesn't look like this is really all text data in the first place. It looks like there are probably bits of text within it, and you could use an encoding for those aspects, but otherwise when you want fixed binary data, you should specify that as bytes, rather than decoding "text" which isn't really text.

It's possible that using Encoding.GetEncoding(28591) (ISO-8859-1) you'll get what you were expecting, but it's still not what you should be doing.

Upvotes: 3

Related Questions