By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

Byte Converting for *8*-bit ASCII characters

P: n/a
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?

Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Try UTF-8.

DalePres

"clintp" <cl*******@geeksalad.org> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Nov 16 '05 #2

P: n/a
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!

Nov 16 '05 #3

P: n/a
Glad you got it worked out.

DalePres

"clintp" <cl*******@geeksalad.org> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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!

Nov 16 '05 #4

P: n/a
clintp wrote:
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.


There's no such thing as 8-bit ASCII, and being "not particular about
the codepage" is pretty much the wrong approach to solve the problem.

Cheers,

--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 16 '05 #5

P: n/a
Use the following to maintain your 8 bit ascii.

Encoding enc = Encoding.GetEncoding(1252);

mc****@cox.net
"Joerg Jooss" <ne********@joergjooss.de> wrote in message
news:xn****************@msnews.microsoft.com...
clintp wrote:
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.


There's no such thing as 8-bit ASCII, and being "not particular about
the codepage" is pretty much the wrong approach to solve the problem.

Cheers,

--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Nov 16 '05 #6

P: n/a
AndyM wrote:
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,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.