473,888 Members | 1,378 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 33884
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
4575
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.: <Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA...
4
1693
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
7386
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 BinaryReader(strm); byteArray= br.ReadBytes(Convert.ToInt32(strm.Length));
10
11408
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
38555
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
11357
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 = System.Convert.ToBase64String(result2, 0, result2.Length) Debug.WriteLine(IIf(data64_1 = data64_2, "Equals", "NOT Equals"))
8
4212
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 bytParam() As Byte 'load the byte array here...
12
3345
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
2493
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
9800
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11185
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10439
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7148
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5824
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4247
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.