473,387 Members | 1,834 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,387 software developers and data experts.

How to convert Byte[] into String?

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. : )
Dec 2 '05 #1
7 57374
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" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
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. : )

Dec 2 '05 #2
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,

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

"EOS" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
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. : )

Dec 2 '05 #3
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" <ja*********@mvps.org> wrote in message
news:ew**************@TK2MSFTNGP14.phx.gbl...
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" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
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. : )


Dec 2 '05 #4
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.

"EOS" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
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. : )

Dec 2 '05 #5
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" <no****@nospam.org> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
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" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
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. : )


Dec 2 '05 #6
EOS
Wow... Thanks.. and it works!

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OA*************@TK2MSFTNGP11.phx.gbl...
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" <no****@nospam.org> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
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" <no****@nospam.org> wrote in message
news:ek**************@TK2MSFTNGP12.phx.gbl...
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. : )



Dec 2 '05 #7
EOS <no****@nospam.org> wrote:
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).

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

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

Similar topics

5
by: Daniel | last post by:
in C# fastest way to convert a string into a MemoryStream
2
by: Brian | last post by:
I have a string that I received from a web service call and I want to write it to a Stream. I cannot figure out how to access the string as a byte array and write it to the Stream. Any thoughts. I...
7
by: Wilfried Mestdagh | last post by:
Hi, how to typecast or convert a byte to a string and the way arount ? I have string from a textbox and wants to transmit it trough a socket. witch accepts a byte. Now I make a copy from the...
4
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
4
by: John Smith | last post by:
Hello, Suppose I have the following C# code which I want to convert to VB: for (int i = 0; i < nFieldLength; i++) Console.Write((char) sValue); sValue is a byte array. The problem is...
4
by: Sam | last post by:
Hi, I'm trying to convert a string to a binary format (byte) I've looked at BitConverter class, but I can't figure out how to use it for a Strin. Does anyone know ? Thx
5
by: EOS | last post by:
Hi, Thanks for this great forum and my convertion from Byte into String works beautifully with Encoding.ASCII.GetString() Anyone how to do the reverse, I mean convert a String into Byte? In...
15
by: Steve | last post by:
Hi All, I have a registration code that currently has 4 bytes that are unused. I'd like to store a future date in those 4 bytes. Is there a way to convert a date to a 4 byte string? When I...
6
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and...
1
by: Slippy27 | last post by:
How do I test a byte string in Python? I want to manually convert (no libraries or functions) a UTF-8 string into UTF-16. My basic solution is to read from the stream some number of UTF-8 bytes,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
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...
0
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,...
0
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...

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.