473,801 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting array of byte data type to string.

Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..

Nov 17 '05 #1
5 2478
Hi,

Why not use this function:

public string writeByteArrayT oString(byte[] byteArray)
{
ASCIIEncoding ascii = new ASCIIEncoding() ;
char[] charArray = ascii.GetChars( byteArray);
return new string(charArra y);
}

Don't forget to include System.Text

Cheers,
Christiaan

<sh*********@te chie.com> schreef in bericht
news:11******** *************@g 49g2000cwa.goog legroups.com...
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..

Nov 17 '05 #2
Hi,
string str = System.Text.Enc oding.ASCII.Get String (bytes);

Ab.
http://joehacker.blogspot.com
<sh*********@te chie.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..

Nov 17 '05 #3
Hi,

You need to know the format the bytes are in, are they ASCII, UTF8 or what?

Then you use the correct Encoding class.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<sh*********@te chie.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..

Nov 17 '05 #4
Christiaan van Bergen <cv********@ber gler.nl> wrote:
Why not use this function:

public string writeByteArrayT oString(byte[] byteArray)
{
ASCIIEncoding ascii = new ASCIIEncoding() ;
char[] charArray = ascii.GetChars( byteArray);
return new string(charArra y);
}

Don't forget to include System.Text


Reasons not to use this function:

1) Its name doesn't follow the .NET naming conventions
2) It creates a new instance of ASCIIEncoding for no reason
3) It creates a new char array for no reason
4) It's less clear than Encoding.ASCII. GetString(whate ver);

You did ask ;)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
<sh*********@te chie.com> wrote:
Hi all,

I am a Beginner to c#.
I Need to convert array of byte data type to string. How to do this..

ex:

byte[] bytes = new byte[1000];

this bytes get filled up. Now I need to convert to string.
How to do this..


You need to choose the right encoding, and then ask it to decode your
bytes to a string. Unless your byte array is absolutely full of useful
data, you should specify how much of it to decode.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.

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

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

Similar topics

5
13928
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
10313
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...
2
2683
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is there any way. Regards, Govind.
4
16472
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope someone can help Thanks
3
2344
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.
1
12562
by: Darrel | last post by:
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster method for converting an array of sbytes to an array of bytes? Thanks, Darrel
1
1987
by: KK | last post by:
Hi, I need to save certain content to SQL server and then use SQL Server indexing facility to search. SQL Server have recomended using Image data type with a filter type (for .htm, .doc etc..) Now, because of this I save normal html content also in binary format in the Image field.
8
10720
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
2
11582
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public void loadBytes(InputStream is){ DataSource ds=new DataSource(is); player = Manager.createPlayer(ds);
0
9698
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9556
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
10516
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
10292
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
10052
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...
1
7589
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6829
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
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3773
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.