473,721 Members | 1,933 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 5396
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
13920
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
10307
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 I'm not aware of that could cause problems. I'm doing this because it's easier to do some of my...
3
2341
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 in C#? Thanks.
8
4563
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...
1
9577
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
4183
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 to char and append to a string.
8
4197
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...
16
5446
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
8396
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': inner = md5(challenge + password).hexdigest() elif challenge == 'sha1': inner =...
0
8736
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
9373
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
9227
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...
1
9143
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
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
4497
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...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2588
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2137
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.