1

Given a byte array (byte[]) is there any quick (as in short and aesthetic) way of transforming this into a string och character array? Assume that the bytes in the array is text represented in ascii.

I'm working in c# right now, and can't find any obvious methods to use. But I'm also interested in a general solution applicable to any modern programming language.

Vincent McNabb
  • 33,327
  • 7
  • 31
  • 53
AnnaR
  • 3,166
  • 6
  • 35
  • 39

2 Answers2

6

System.Text.ASCIIEncoding.ASCII.GetString will return a string from the given byte array.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76
1

Important note: as noted here - Strings are Unicode, so you must specify an encoding on conversion.

System.Text.ASCIIEncoding is one option, but make sure that the byte array contains only ASCII encoded characters.

Community
  • 1
  • 1
gimel
  • 83,368
  • 10
  • 76
  • 104