-1

Trying to write the values from a byte array to a text file. When I write the data to a text box on screen it displays correctly, but not when I open the text file. Here's how I display it to the screen:

foreach (ManagementObject FailData in FailDataSet)
{
    Byte[] data = (Byte[])FailData.Properties["VendorSpecific"].Value;

    for (int i = 0; i < data[0] - 1; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            richTextBox2.Text = richTextBox2.Text + data[i * 12 + j] + "\t";
        }
        richTextBox2.Text = richTextBox2.Text + "\n";
    }
}

But when I write data to a notepad file I get the following:

/ db % dd # ?d 2 cct 3 dd0 ......

Here's how I write the byte array to the text file

File.WriteAllBytes(@"c:\folder\file.txt", data);

How can I write it to a text file the same way it appears on screen?

user3290504
  • 1
  • 2
  • 5
  • 2
    What is the character encoding? – rory.ap Jan 20 '15 at 17:15
  • 2
    You seem to be doing two very different things. In one you are converting the bytes to strings before writing them to the textbox, in the second you are just writing the raw bytes to a file... If you want the values of the bytes written as text to the file then write them as strings, not bytes. – Chris Jan 20 '15 at 17:18
  • look at this link for several options in regards to writing the Encoded byte as a string http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string – MethodMan Jan 20 '15 at 17:20
  • @Chris how can I write these values to strings before writing? I have been banging my head with this for an hour? – user3290504 Jan 20 '15 at 17:21
  • 3
    Hint: `richTextBox2.Text` is a `string` – Jcl Jan 20 '15 at 17:23
  • @user3290504: I'd probably as first effort go `data.Select(x=>x.ToString())` to get an `IEnumerable`. Then use `String.Join` to make them into a single string. I'd have to test the code to see if that does the exact right thing (hence not an answer) but it should give you an idea. Making bytes into strings shouldn't be something hard to do. `ToString` works on most objects (which it makes sense to represent as a string). – Chris Jan 20 '15 at 17:24
  • @Chris I'd actually go for `Text.Encoding` functions – Jcl Jan 20 '15 at 17:32
  • @Jcl: unless I'm wrong though we are not interpreting the bytes as encoded text. The line `richTextBox2.Text + data[i * 12 + j] + "\t"` that is used in the printing to textbox will just implicitly convert the byte to a string I think. ie if the byte is value 32 it will print the string "32", not the ascii character 32. – Chris Jan 20 '15 at 17:46
  • @Chris yes, you are right, I saw that later... I've added an answer :-) – Jcl Jan 20 '15 at 17:47

2 Answers2

3

Do the same thing both ways.

Byte[] data = (Byte[])FailData.Properties["VendorSpecific"].Value;
var sb = new StringBuilder();

for (int i = 0; i < data[0] - 1; i++)
{
    for (int j = 0; j < 12; j++)
    {
        sb.Append(data[i * 12 + j] + "\t");
    }
    sb.AppendLine();
}
File.WriteAllText("filename.ext", sb.ToString());
0

Reading the question, I assume the byte array format is just "length" at byte 0, then the rest must be written as interpretation of the ANSI or ASCII characters (1 byte per character).

You are putting a tab between each byte and a newline every 12.

So I'd do this:

 var data = (byte[])FailData.Properties["VendorSpecific"].Value;
 var sb = new StringBuilder();
 for(var i = 0; i < data[0]-1; i++)
 {
   sb.Append(data[i+1] + "\t");
   if((i % 12)==11) sb.AppendLine();
 }
 File.WriteAllText(@"c:\folder\file.txt", sb.ToString());

Otherwise, once you have the text (using your code) in the RichTextBox, you can just do:

File.WriteAllText(@"c:\folder\file.txt", richTextBox2.Text);

Or the even clearer:

richTextBox2.SaveFile(@"c:\folder\file.txt", RichTextBoxStreamType.PlainText);

(I'm assuming a WinForms RichTextBox here, but taking the name of the control, I'd bet on it)

Jcl
  • 27,696
  • 5
  • 61
  • 92