473,545 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting byte array to string.

Hi,

We are having problem in converting a byte array to string, The byte array
has char(174), char(175), char(240), char(242) and char(247) as delimiters
for the message.

when we use "System.Text.En coding.ASCII.Ge tString(bytearr ay)" of .Net
library, we found that the char (delimiters) specified above are replaced
with different char.

Can any one help me in this ?

My sample program is,

using System;

using System.Text;

using System.Net.Sock ets;

namespace StrideAPIParse

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

public static char PACKET_BEGIN_DE LIMIT = (char)175;

public static char HEADER_DELIMIT = (char)240;

public static char RECORD_FIELD_DE LIMIT = (char)242;

public static char RECORD_DELIMIT = (char)247;

public static char PACKET_END_DELI MIT = (char)174;

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//
TcpClient tcpClient = new TcpClient();

tcpClient.Conne ct("172.16.10.1 64", 5403);

NetworkStream networkStream = tcpClient.GetSt ream();

byte[] bytes = new byte[tcpClient.Recei veBufferSize];

int readleagth = networkStream.R ead(bytes, 0, (int)
tcpClient.Recei veBufferSize);

bytes[readleagth] = 0;

String stridemsg = Encoding.ASCII. GetString(bytes );

string [] msg = stridemsg.Split (HEADER_DELIMIT );

foreach(string m in msg)

{

Console.WriteLi ne(m);

}

tcpClient.Close ();

Console.ReadLin e();

}

}

}
Nov 16 '05 #1
4 5365
Prabhu,

The reason for this is that 174, 175, 240, and 242 are not valid ascii
codes. They are valid in the set of extended ascii codes, but I don't
believe that there is an encoding that .NET provides which will handle this.
You will have to implement your own, or find an existing one.

Another possibility would be to pad every byte with an empty byte, and
then using the UnicodeEncoding class to decode your bytes into a string.
This should produce valid values that you can work with.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:e$******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

We are having problem in converting a byte array to string, The byte array
has char(174), char(175), char(240), char(242) and char(247) as delimiters
for the message.

when we use "System.Text.En coding.ASCII.Ge tString(bytearr ay)" of .Net
library, we found that the char (delimiters) specified above are replaced
with different char.

Can any one help me in this ?

My sample program is,

using System;

using System.Text;

using System.Net.Sock ets;

namespace StrideAPIParse

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

public static char PACKET_BEGIN_DE LIMIT = (char)175;

public static char HEADER_DELIMIT = (char)240;

public static char RECORD_FIELD_DE LIMIT = (char)242;

public static char RECORD_DELIMIT = (char)247;

public static char PACKET_END_DELI MIT = (char)174;

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//
TcpClient tcpClient = new TcpClient();

tcpClient.Conne ct("172.16.10.1 64", 5403);

NetworkStream networkStream = tcpClient.GetSt ream();

byte[] bytes = new byte[tcpClient.Recei veBufferSize];

int readleagth = networkStream.R ead(bytes, 0, (int)
tcpClient.Recei veBufferSize);

bytes[readleagth] = 0;

String stridemsg = Encoding.ASCII. GetString(bytes );

string [] msg = stridemsg.Split (HEADER_DELIMIT );

foreach(string m in msg)

{

Console.WriteLi ne(m);

}

tcpClient.Close ();

Console.ReadLin e();

}

}

}

Nov 16 '05 #2
I believe the Encoding.ASCII uses a 7-bit set. Not sure which one you
should use.
Nov 16 '05 #3
UTF-8 ?

"Laszlo Szijarto" <ls*******@nosp am.mrdoc.cc> wrote in message
news:c9******** **@sulawesi-fi.lerc.nasa.go v...
I believe the Encoding.ASCII uses a 7-bit set. Not sure which one you
should use.

Nov 16 '05 #4
Prabhu <Pr******@e-tapaal.com> wrote:
We are having problem in converting a byte array to string, The byte array
has char(174), char(175), char(240), char(242) and char(247) as delimiters
for the message.

when we use "System.Text.En coding.ASCII.Ge tString(bytearr ay)" of .Net
library, we found that the char (delimiters) specified above are replaced
with different char.

Can any one help me in this ?


Have a look at http://www.pobox.com/~skeet/csharp/unicode.html

I suspect Encoding.Defaul t is what you're after, but it looks like
you're not really after those *characters*, you're after those *bytes*
as delimiters. In that case, you should process the delimiters first,
and then decode each chunk of bytes between delimiters.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

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

Similar topics

5
13902
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do something like: char chars = "Hello!"; byte bytes = (byte) chars;
4
10281
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with bytes that are not printable characters? I've tested this and it seems to work fine, but I want to make sure there isn't some condition or situation...
3
2330
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes need to be converted to an enum. The next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct with the data. How does one do this...
8
4538
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.: ...
1
9550
by: Benoit | last post by:
Hi, what is the most performing way to convert a byte stream to a string? Byte() to be converted to String. Thanks, iBen. Sorry if it ia a double post in this newsgroup, I cannot set my first one.
5
4164
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a serial ports as bytes and some of these represent strings. (Like the Packed BCD date/time stamp etc). At present I read through each Byte and convert...
8
4184
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...
16
5432
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still learning all help would be greatly apperciated. Attribute VB_Name = "Module1" Option Explicit
6
8372
by: ogtheterror | last post by:
Hi I have a very limited understanding of Python and have given this the best shot i have but still have not been able to get it working. Is there anyone that knows how to get this into a .net assembly? _________________________________________________________ def encrypt_password(challenge, password): if challenge == 'md5': ...
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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
7921
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...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5982
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...
1
5343
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
4958
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
3465
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...
1
1900
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

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.