How to convert Byte[] into String?

E

EOS

Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I wanted
into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

It's very simple, you have to use one of the encoding classes provided in
the framework. You see, depending of how you generated the bytes[] from the
original string you get a different set of data, UNICODE are stored as two
bytes, where ASCII are encoded as 1 byte.

Once you know this you use osmething like ASCIIEncoding.GetString( byte[] );

cheers,
 
P

Peter Rilling

If you need something more complex that takes into account different
encoding such as ascii vs. unicode, then use something like
Encoding.GetString(...).

James Curran said:
You actually want:

string StringIWant = BitConverter.ToString(byteData);

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
EOS said:
Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I wanted
into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )
 
E

EOS

Actually what I wanted is rather simple.

In C/C++, it is;

byte byteData[500] = .....

CString szMyString = (CString) byteData

It is that simple and straight forward because what is inside the byte array
is exactly the same char[]. Even a simple memcpy will work.

Any ideas on how to implement these simple object convertion in C#?

By the way, I am develope on Compact .NET which is on PDA.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

It's as simple in C#:

byte byteData[500] = ...

string s = Encoding..ASCII.GetString( byteData);

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



EOS said:
Actually what I wanted is rather simple.

In C/C++, it is;

byte byteData[500] = .....

CString szMyString = (CString) byteData

It is that simple and straight forward because what is inside the byte
array is exactly the same char[]. Even a simple memcpy will work.

Any ideas on how to implement these simple object convertion in C#?

By the way, I am develope on Compact .NET which is on PDA.

EOS said:
Hi,

I would like to ask on how to convert array of bytes, Byte[] into String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I
wanted into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )
 
E

EOS

Wow... Thanks.. and it works!

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

It's as simple in C#:

byte byteData[500] = ...

string s = Encoding..ASCII.GetString( byteData);

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



EOS said:
Actually what I wanted is rather simple.

In C/C++, it is;

byte byteData[500] = .....

CString szMyString = (CString) byteData

It is that simple and straight forward because what is inside the byte
array is exactly the same char[]. Even a simple memcpy will work.

Any ideas on how to implement these simple object convertion in C#?

By the way, I am develope on Compact .NET which is on PDA.

EOS said:
Hi,

I would like to ask on how to convert array of bytes, Byte[] into
String?

I tried StringIwant = System.Convert.ToString(byteData);

but it actually return "System.Byte[]" rather than convert the data I
wanted into string.

I guess it should be a rather simple question.

By the way, I am using compact framework for PDA.

Thanks in advance. : )
 
J

Jon Skeet [C# MVP]

EOS said:
Actually what I wanted is rather simple.

In C/C++, it is;

byte byteData[500] = .....

CString szMyString = (CString) byteData

It is that simple and straight forward because what is inside the byte array
is exactly the same char[]. Even a simple memcpy will work.

Hmm... that sounds unlikely, as a byte array has one byte per entry,
but a char array has two bytes per entry in .NET.

It's all a case of working out what encoding you're using. I suspect
that Encoding.Default.GetString(data) is what you want (or possibly the
overload which takes an index and size, if your byte array is longer
than the "real" data in it; you don't want to include the null
terminator, for instance).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top