Byte conversion to string

G

Guest

I can't seem to find any byte array conversion functions to strings.
I'm receiving data via a network packet and want to convert the byte array
contents into there actual character equivilent.

For example
Convert
byte[] byteArray = { 64, 65, some ascii numbers }
To
char[] charArray = { A, B, some characters }
by the way I don't think my ascii numbers are correct


In C++ you would just typecast them but I can't seem to do that.
 
F

Francois Beaussier

Hello Daniel,

you can use the following method:

string myStr = System.Text.Encoding.ASCII.GetString(byteArray);

You can use others encoding depending on your needs.
 
N

newbie

you want a conversion from the acii numbers to the characters ?
like chr(32) / returns space
chr(33) /returns !
chr(65) /returns A

asc("A") / returns 65
asc(" ") /returns 32

Make a for next loop and convert your array ?
 
G

Guest

This solution works also but I think the other solution is better for my
purposes just because it converts the whole array at once.

Thanks for your input though I can see where I would use this tecnique over
the other one.

newbie said:
you want a conversion from the acii numbers to the characters ?
like chr(32) / returns space
chr(33) /returns !
chr(65) /returns A

asc("A") / returns 65
asc(" ") /returns 32

Make a for next loop and convert your array ?


Daniel said:
I can't seem to find any byte array conversion functions to strings.
I'm receiving data via a network packet and want to convert the byte array
contents into there actual character equivilent.

For example
Convert
byte[] byteArray = { 64, 65, some ascii numbers }
To
char[] charArray = { A, B, some characters }
by the way I don't think my ascii numbers are correct


In C++ you would just typecast them but I can't seem to do that.
 
G

Guest

This works well
I knew there had to be a solution somewhere!!!
Navigating the docs to find this isn't easy how did you find out about it?

Francois Beaussier said:
Hello Daniel,

you can use the following method:

string myStr = System.Text.Encoding.ASCII.GetString(byteArray);

You can use others encoding depending on your needs.

--
Francois Beaussier


Daniel said:
I can't seem to find any byte array conversion functions to strings.
I'm receiving data via a network packet and want to convert the byte array
contents into there actual character equivilent.

For example
Convert
byte[] byteArray = { 64, 65, some ascii numbers }
To
char[] charArray = { A, B, some characters }
by the way I don't think my ascii numbers are correct


In C++ you would just typecast them but I can't seem to do that.
 

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