tags:

views:

252

answers:

5

Hello,

I'm trying to display the content of a byte array in a text file.

This is my code:

        var writer = new System.IO.StreamWriter(Application.StartupPath + @"\B323.txt");
        writer.Write(data.ToString());
        writer.Close();
        writer.Dispose();

data is a byte[] array.

The output is "System.Byte[]",why?

I'm trying to display the content of this array,where is the problem?

+7  A: 

When you call byte[].ToString() that just returns System.Byte[]. How were you intending the byte array to be converted? There are any number of ways of converting the bytes to a string.

If you want it as a "hex dump" you could you BitConverter.ToString(byte[]) which will produce output such as

5B-3E-5D

Is that what you're after? If you actually want to just write the bytes to a file, because they already represent encoded text, then you should use a FileStream instead and write them directly.

(Other points as asides: you should use a using statement to handle the writer; you don't need to call close because you're already disposing; File.WriteAllText is a simpler way of doing this to start with.)

Jon Skeet
I only wanted to know why it display "System.Byte[]".Yes that's what I want,but I use LINQ to display a space instead of a dash between the characters.I just wanted to know why it display System.Byte[]
John
The default implementation of `Object.ToString()` is to dump the type name. Array doesn't override this.
Jon Skeet
Thanks Jon! How is the progress of the second edition of your book? I can't wait!
John
It's coming along slowly :) Hopefully I'll be able to give information about "early access" fairly soon...
Jon Skeet
+3  A: 

Calling ToString on data will turn it into a textual representation (the same you will see in the Watch or Locals window when debugging).

You can either just write data to the stream, or use one of the functions located in System.Text.Encoding to convert your byte[] to a string.

Aistina
+3  A: 

The default ToString() implementation just echoes the class name. You probably want one of the Encoding.GetString() methods, like ASCIIEncoding.GetString.

Blair Conrad
+1  A: 

Which do you want to do? Write the data as bytes or as their text representation?

Binary:

FileStream fS = new FileStream(Application.StartupPath + @"\B323.txt");
BinaryWriter bW = new BinaryWriter(fS);
bW.Write(data);
fS.Close();

Or

Text Representation:

var writer = new System.IO.StreamWriter(Application.StartupPath + @"\B323.txt");
writer.Write(System.Text.ASCIIEncoding.ASCII.GetString(data));
writer.Close();
Berdon Magnus
+1  A: 

Depending on how you want the array to be represented in the text file, perhaps something like these:

// 12-34-AB
writer.Write(BitConverter.ToString(data));

// 1234AB
writer.Write(BitConverter.ToString(data).Replace("-", string.Empty));

// 0x1234AB
writer.Write("0x" + BitConverter.ToString(data).Replace("-", string.Empty));

// [ 12, 34, AB ]
writer.Write("[ " + BitConverter.ToString(data).Replace("-", ", ") + " ]");

// [ 0x12, 0x34, 0xAB ]
writer.Write("[ 0x" + BitConverter.ToString(data).Replace("-", ", 0x") + " ]");
LukeH