0

I am having trouble saving my file into binary into my database and so, to debug, I am trying to view the contents of the binary. I have tried this:

file.InputStream.Read(fileData, 0, size);
Debug.Print("binary=" + fileData);

It outputs this:

binary=System.Byte[]

How can I see the actual data which should look like 0x78421824794783741237FJKHASJKH etc?

In my database, it only goes in as 0x (which is the larger problem I am trying to solve).

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • 1
    possible duplicate of [byte\[\] to hex string](http://stackoverflow.com/questions/623104/byte-to-hex-string) EDIT: FYI, the reason you're getting "System.Byte[]" as your output is that's simply the `ToString()` representation of the byte array; since the representation is a bit arbitrary (Base 2? Base 16? Base 64?) Also, I'm assuming you want hexadecimal output, but your "should look like" example includes invalid characters ("JKHS"), but I guess that was just you mashing on the keyboard. :) – Chris Sinclair Feb 18 '13 at 19:59
  • Do you want to see it as a bunch of byte values? Ones and zeros? Base64? What? – Mike Christensen Feb 18 '13 at 19:59
  • Yes, I just want to make sure there is actual data in there; because it's only going in my database as `0x` and there should be a bunch of numbers (binary code). – user1477388 Feb 18 '13 at 20:00

1 Answers1

1

If you're simply trying to make sure the data is there, the easiest way is probably to output it in Base64 encoding. Something like:

byte[] foo = new byte[] { 1, 2, 3, 4, 5 };
string base64 = Convert.ToBase64String(foo);
Console.WriteLine(base64);

Demo

If there were no data (basically a Byte[0]), then base64 would be an empty string.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • Awesome, so that gives me `binary=iVBORw0KGgoAAAANSUhE...`. Can I just save that in my database and it will output properly? How can I save the data to a database? Currently, when I try to save it, it only stores `0x`. Do you happen to have any idea? Thanks for at least showing me this. – user1477388 Feb 18 '13 at 20:11
  • 1
    Sure, you could save that data in your database as a string (usually `varchar` or `text`). You'd have to of course convert it back into a `Byte[]` with `Convert.FromBase64String(dbValue)` when loading it. However, many databases support storing byte arrays natively, so technically you might not need to serialize the data at all. Your choice. – Mike Christensen Feb 18 '13 at 20:13
  • I am using an existing stored procedure which uses a SQL image column to store the data. It works for other pages, but not for this new thing that I am trying to create. I can't figure out why it's only saving `0x`... – user1477388 Feb 18 '13 at 20:16
  • 1
    You'd probably want to post the code that saves the `Byte[]`, as well as the stored procedure. I'd recommend posting a new question, and be sure to tag it with the appropriate database system you're using. – Mike Christensen Feb 18 '13 at 20:18
  • I already have http://stackoverflow.com/questions/14943333/cant-save-binary-data-to-database-in-c-sharp but no one seems to know what the problem is so far... – user1477388 Feb 18 '13 at 20:21
  • 2
    @user1477388 Using Base 64 is a good option, if you use simple string queries and don't want to escape your data, the tradeoff is that it occupies around 30% more than binary data, but it may be worth the loss. – Rafael Feb 18 '13 at 20:41