473,385 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

byte[] -> string

how can I convert a byte[] to a string ?
Any idea?

Andrea

Nov 15 '05 #1
9 2230
RF
Andrea,

string text = (new System.Text.ASCIIEncoding()).GetString(byte[]);
RF

"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:e9*************@TK2MSFTNGP09.phx.gbl...
how can I convert a byte[] to a string ?
Any idea?

Andrea

Nov 15 '05 #2
"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:e9*************@TK2MSFTNGP09.phx.gbl...
how can I convert a byte[] to a string ?
Any idea?

Andrea


How are the characters encoded in the byte array? You need to examine the
System.Text.Encoding class and its related classes:
System.Text.ASCIIEncoding, System.Text.UnicodeEncoding. Here's a basic
example, if the byte array is ASCII:

System.Text.ASCIIEncoding AE = new System.Text.ASCIIEncoding();
byte[] ByteArray = { 69, 110, 99, 111, 100, 105, 110, 103, 32, 83, 116,
114, 105, 110, 103, 46 };
char[] CharArray = AE.GetChars(ByteArray);
string s = new string(CharArray);
MessageBox.Show(s);

Erik
Nov 15 '05 #3
joe

"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:e9*************@TK2MSFTNGP09.phx.gbl...
how can I convert a byte[] to a string ?
Any idea?

Andrea


Encoding.ASCII.GetString(byteArray);
Nov 15 '05 #4
Erik Frey <er*******@hotmail.com> wrote:
"Andrea" <andreno_spamalmieri@enmo_spamm> wrote in message
news:e9*************@TK2MSFTNGP09.phx.gbl...
how can I convert a byte[] to a string ?
Any idea?

Andrea


How are the characters encoded in the byte array? You need to examine the
System.Text.Encoding class and its related classes:
System.Text.ASCIIEncoding, System.Text.UnicodeEncoding. Here's a basic
example, if the byte array is ASCII:

System.Text.ASCIIEncoding AE = new System.Text.ASCIIEncoding();
byte[] ByteArray = { 69, 110, 99, 111, 100, 105, 110, 103, 32, 83, 116,
114, 105, 110, 103, 46 };
char[] CharArray = AE.GetChars(ByteArray);
string s = new string(CharArray);
MessageBox.Show(s);


Or, slightly more simply:

MessageBox.Show (Encoding.ASCII.GetString(bytes));

There's no need to create a new ASCIIEncoding, or to get a character
array and then create a string from that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Andrea <andreno_spamalmieri@enmo_spamm> wrote:
how can I convert a byte[] to a string ?
Any idea?


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
> Or, slightly more simply:

MessageBox.Show (Encoding.ASCII.GetString(bytes));

There's no need to create a new ASCIIEncoding, or to get a character
array and then create a string from that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


That's what I get for cutting and pasting from MSDN :)

Erik
Nov 15 '05 #7
Erik Frey <er*******@hotmail.com> wrote:
That's what I get for cutting and pasting from MSDN :)


Could you let me know which MSDN page it is? I'm more than happy to
write an email suggesting a change...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Erik Frey <er*******@hotmail.com> wrote:
That's what I get for cutting and pasting from MSDN :)


Could you let me know which MSDN page it is? I'm more than happy to
write an email suggesting a change...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Encoding Base Types:
http://msdn.microsoft.com/library/de...gbasetypes.asp

The article places an emphasis on coverting to chars, so the methodology may
be appropriate (if roundabout).

Erik
Nov 15 '05 #9
Erik Frey <er*******@hotmail.com> wrote:
Encoding Base Types:
http://msdn.microsoft.com/library/de...ary/en-us/cpgu
ide/html/cpconencodingbasetypes.asp
Cheers.
The article places an emphasis on coverting to chars, so the methodology may
be appropriate (if roundabout).


That much is fine, but there's no excuse for creating a new
ASCIIEncoding instance.

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

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

Similar topics

10
by: Eric | last post by:
I have an array that contains over 30000+ bits. The size varies at runtime. I need to move through this chunk of memory and count bits every so often. Example: First 9 bits has 2 ones, next 10...
16
by: Samuel Thomas | last post by:
Hello Friends, I understand(could be wrong) that the smallest chunk of memory is called a word. If that is correct, that means if I am using a 32 bit OS a word is 4 bytes. So that's why the size...
13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
8
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
3
by: diadomraz | last post by:
Hi, This is a question about declaring a signed 8bit numeric type in C++ that prints like a numeric variable and not like a char variable. I would like to declare a type with name 'byte' which...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.