473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Byte Converting for *8*-bit ASCII characters

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
6 33798
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tomas Deman | last post by:
Hi, I need a fast method for converting an int array to a byte array. At the moment, I'm using this: public static byte Int2ByteArray(int array) { byte lbytRetval = new byte; int lintIdxHi;...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
4
by: Ciaran | last post by:
Hi there, I'm not very good at c and I was wondering if anyone could give me a hand converting the following code snippet to c# ... BYTE checksum(BYTE *InStr, BYTE len) { BYTE i, sum = 0;
2
by: Matt | last post by:
I just wanted to know if I am converting to/from streams the easiest and correct way. I am performing the following statements to convert byte arrays to and from streams during different...
10
by: JT | last post by:
Need some help in converting a byte to a signed int. This is what I have attempted to do: byte bytes = new byte { 0xFF, 0xFF, 0x9C}; StringBuilder hexString = new StringBuilder(); foreach...
9
by: Dante | last post by:
I'm converting a C# program to VB.net, but i'm having problems converting a integer to a byte in the same way as the c# program does. //C# program int i = 137694; byte b = (byte) i; //b...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
12
by: O.B. | last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it, "as" is the keyword to use. Unfortunately, the compiler is trying to do the cast a compilation time. See below. /*...
1
by: Anitha Naidu | last post by:
I have structure as below in C# which is equal to Cobol Copy book structue public class InputStruct { public string CustomerNumber; public string...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.