Byte Converting for *8*-bit ASCII characters

C

clintp

I have a byte array that contains 8-bit ascii characters. I'm not
particular about the codepage used to display them, but I have to
preserve the position in the string with something and be able to back
to the original byte implementation.

For example, this doesn't work:

// Bytes for .02{cent} in my standard codepage
byte [] ba = new Byte [] { 0x2e, 0x30, 0x32, 0x9b };

string s = System.Text.Encoding.ASCII.GetString(ba);

Console.WriteLine(s);

byte [] nb = Encoding.ASCII.GetBytes(s);

Console.WriteLine("{0:x} {1:x} {2:x} {3:x}",
nb[0], nb[1], nb[2], nb[3]);

What I get is:
..02{left arrow}
2e 30 32 1b

As expected, the 9b became 1b because the encoding was ASCII -- 7 bits.
[The cent sign became a left-arrow because of the codepage
differences. Fine. That's okay.]

What I want is:
..02{cent}
2e 30 32 9b

What I absolutely need to be able to do is convert the byte array into
a string, and then back to a byte array and then have it come back with
the same byte representation on the way out again.

Any help?
 
C

clintp

Quick answer, but wrong. I tried it with no joy. Try it and see.

An e-mail reply I just received suggested:

byte [] ba = new Byte [] { 0x2e, 0x30, 0x32, 0x9b };

string s = System.Text.Encoding.Default.GetString(ba);

Console.WriteLine(s);

byte [] nb = Encoding.Default.GetBytes(s);

Console.WriteLine("{0:x} {1:x} {2:x} {3:x}",
nb[0], nb[1], nb[2], nb[3]);

And this yeilds the expected results.

Thanks for replying, though!
 
A

AndyM

Use the following to maintain your 8 bit ascii.

Encoding enc = Encoding.GetEncoding(1252);

(e-mail address removed)
 
J

Joerg Jooss

AndyM said:
Use the following to maintain your 8 bit ascii.

Encoding enc = Encoding.GetEncoding(1252);

Again,

there's no 8 bit ASCII, just 8 bit character sets like Windows 125x or
ISO-8859-x that extend the original ASCII character set as defined as
ISO-646.


Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top