Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

Is there an easy way to convert a byte array to a string so the following unit test passes? I can't find an encoding that works for all values.

  [TestMethod]
  public void TestBytToString()
  {
     byte[] bytArray = new byte[256];
     for (int i = 0; i < bytArray.Length; i++)
     {
        bytArray[i] = (byte)i;
     }
     string x = System.Text.Encoding.Default.GetString(bytArray);
     for (int i = 0; i < x.Length; i++)
     {
        int y = (int)x[i];
        Assert.AreEqual(i, y);
     }
  }
share|improve this question

marked as duplicate by John Kraft, chue x, Jeremy J Starcher, ryan1234, S.D. Jul 25 '13 at 4:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Array.ConvertAll should work for creating a char[], which you can pass to a string constructor. –  Ben Voigt Jul 24 '13 at 20:46
    
Avoiding the "why would you do this?" question, the only encoding this operation would be valid in would be ASCII, I think. –  JerKimball Jul 24 '13 at 20:59
    
Ascii fails Expected: <128>, Actual: <63> –  user2227596 Jul 24 '13 at 21:11
    
When I got your code and tested with the following , test passes string x = new string(bytArray.Select(Convert.ToChar).ToArray()); Credit goes to @Ricky –  Mechanical Object Jul 24 '13 at 21:56

5 Answers 5

up vote 0 down vote accepted

This worked:

  [TestMethod]
  public void TestBytToString()
  {
     byte[] bytArray = new byte[256];
     ushort[] usArray = new ushort[256];
     for (int i = 0; i < bytArray.Length; i++)
     {
        bytArray[i] = (byte)i;

     }

     string x = System.Text.Encoding.Default.GetString(bytArray);
     for (int i = 0; i < x.Length; i++)
     {
        int y = System.Text.Encoding.Default.GetBytes(x.Substring(i, 1))[0];
        Assert.AreEqual(i, y);
     }
  }
share|improve this answer

As far as i know everthing above value 127 in byte is considered a negative number and as char can only take positive values it results with an unknown char in every encoding you take.

You might want to convert the byte array to unsigned short (ushort) and then to string...

share|improve this answer
var str = System.Text.Encoding.Default.GetString(bytArray);
share|improve this answer
    
When I use the above it fails whe Expected: <128>, Actual: <8364> –  user2227596 Jul 24 '13 at 21:01
string x = Encoding.UTF8.GetString(bytArray, 0, bytArray.Length);
share|improve this answer
    
When I use the above it fails with Expected: <128>, Actual: <65533>. –  user2227596 Jul 24 '13 at 20:53

The System.Text.Encoding.UTF8 should do a trick for you.

share|improve this answer
    
What if it's not in UTF8? –  It'sNotALie. Jul 24 '13 at 20:41
    
So what encoding is it? If it's a ASCII, it still in the margins of UTF8, which is famous for shrinking to the necessary size. –  Tigran Jul 24 '13 at 20:43
    
When I use UTF8 it fails with Expected: <128>, Actual: <65533>. –  user2227596 Jul 24 '13 at 20:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.