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).

3
  • 1
    possible duplicate of 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. :) Commented Feb 18, 2013 at 19:59
  • Do you want to see it as a bunch of byte values? Ones and zeros? Base64? What? Commented Feb 18, 2013 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). Commented Feb 18, 2013 at 20:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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.
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...
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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.