-1

I have a list declared as:

List<byte> localData = new List<byte>();

The list contains: AA 01 53 54 41 43 4B 20 4F 56 45 52 46 4C 4F 57 34 5C , where from localData[2] to localData[15] i have the text STACK OVERFLOW, that i will output to a textBox.

I cannot find a converter to do this for me, therefore i used a table do convert it in this way:

//Here i have all printable ascii chars.
char[] asciiTable = {' ','!','"','#','$','%','&','\'','(',')'etc... }; 

To print the data out i used:

textBox1.Clear();
for (i = 2; i < 16; i++)
{
     textBox1.Text += asciiTable[localData[i] - 32];
}

But i think there must be a better conversion method. Note: I'm using Visual Studio 2015 Express for Desktop.

Edit: The suggested duplicate How to convert byte[] to string? does not cover the use of List.

Community
  • 1
  • 1
KrafT
  • 17
  • 6
  • possible duplicate of [How to convert byte\[\] to string?](http://stackoverflow.com/questions/1003275/how-to-convert-byte-to-string) – Bjørn-Roger Kringsjå Aug 30 '15 at 13:25
  • There is no text but encoded text. Are you sure the encoding is ASCII? Code dealing with text should be very clear about which encoding it handles. (Requirement specifications should be, too.) It should also be clear about what to do in exceptional cases. (Substitution characters like ? and � is a "best effort," quiet error technique, while throwing an exception is an "all or nothing," "validate your assumptions" technique.) – Tom Blodget Aug 30 '15 at 17:01

4 Answers4

2

You probably want

textBox1.Text =
    System.Text.Encoding.Default.GetString(localData.ToArray());

Encoding.Default gives you the default ANSI encoding for your system. Do not confuse this with ASCII, which is a 7 bit encoding. The ANSI encoding is an 8 bit encoding, but the first 127 code points are the same as in the ASCII encoding.

Kris Vandermotten
  • 10,111
  • 38
  • 49
2

Here is how you can do it, if you really only need ASCII, and only a subrange of your list:

List<byte> localData = new List<byte> { 0xAA, 0x01, 0x53, 0x54, 0x41, 0x43, 0x4B, 0x20, 0x4F, 0x56, 0x45, 0x52, 0x46, 0x4C, 0x4F, 0x57, 0x34, 0x5C };
textBox1.Text = Encoding.ASCII.GetString(localData.GetRange(2, 14).ToArray());

If you need a conversion that supports characters outside the ASCII range, just change the code snippet to use the appropriate Encoding property.

sstan
  • 35,425
  • 6
  • 48
  • 66
1

A solution using LINQ:

List<byte> localData = new List<byte> { 0xAA, 0x01, 0x53, 0x54, 0x41, 0x43, 0x4B, 0x20, 0x4F, 0x56, 0x45, 0x52, 0x46, 0x4C, 0x4F, 0x57, 0x34, 0x5C };

string txt = new string(localData.Where((b, index) => index >= 2 && index <= 15)
                                 .Select(b => (char)b).ToArray());

WriteLine(txt); // STACK OVERFLOW
w.b
  • 11,026
  • 5
  • 30
  • 49
0

There is System.Text.Encoding class. It has a few properties for encoding and decoding texts. There should be a property Default that provides a default encoding (sufficient in most cases).

There is GetString method which takes a byte-array. See https://msdn.microsoft.com/en-us/library/744y86tc(v=vs.110).aspx

You can call ToArray on your collection of byte and then feed it to GetString.

Example from MSDN

private static string ReadFromBuffer(FileStream fStream)
{
    Byte[] bytes = new Byte[MAX_BUFFER_SIZE];
    string output = String.Empty;
    Decoder decoder8 = enc8.GetDecoder();

    while (fStream.Position < fStream.Length) {
       int nBytes = fStream.Read(bytes, 0, bytes.Length);
       int nChars = decoder8.GetCharCount(bytes, 0, nBytes);
       char[] chars = new char[nChars];
       nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0);
       output += new String(chars, 0, nChars);                                                     
    }
    return output;
}
bash.d
  • 13,029
  • 3
  • 29
  • 42
  • Thanks to all. The w.b suggestion does what i need. I'll refer to all the answers to learn C#, where i'm just beginning. – KrafT Aug 30 '15 at 14:47