473,587 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Enc oding.ASCII.Get String(ba);

Console.WriteLi ne(s);

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

Console.WriteLi ne("{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 33855
Try UTF-8.

DalePres

"clintp" <cl*******@geek salad.org> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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.Enc oding.ASCII.Get String(ba);

Console.WriteLi ne(s);

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

Console.WriteLi ne("{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.Enc oding.Default.G etString(ba);

Console.WriteLi ne(s);

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

Console.WriteLi ne("{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*******@geek salad.org> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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.Enc oding.Default.G etString(ba);

Console.WriteLi ne(s);

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

Console.WriteLi ne("{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.d e
Nov 16 '05 #5
Use the following to maintain your 8 bit ascii.

Encoding enc = Encoding.GetEnc oding(1252);

mc****@cox.net
"Joerg Jooss" <ne********@joe rgjooss.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.d e

Nov 16 '05 #6
AndyM wrote:
Use the following to maintain your 8 bit ascii.

Encoding enc = Encoding.GetEnc oding(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.d e
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
407
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; int lintIdxLo; for (int i = 0; i < array.GetLength(0); i++)
8
4547
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 XML, and embedded the array as some child element in an xml file, i.e.: ...
4
1680
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
7351
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 processing in my application: Here is a sample where I convert from a stream to a byte array: strm = mqMsg.BodyStream; BinaryReader br = new...
10
11363
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 (byte tmpByte in bytes) hexString.Append(tmpByte.ToString("X2")); int intValue = int.Parse(hexString.ToString(), NumberStyles.AllowHexSpecific);
9
38498
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 returns as value 222 'VB program
2
11296
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 loaded Dim data64_1 As String = System.Convert.ToBase64String(result1, 0, result1.Length) Dim data64_2 As String =...
8
4190
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, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim...
12
3312
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. /* TestA.cs */ namespace TEST { class TestA { public ushort val1; public ushort val2; public ushort val3;
1
2339
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 CustomerType;
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.