1

I have an image data type in my table. When I query using SQL Server Management Studio, I get the following in my results window, both grid and text.

0x255044462D312E320D0A0D0A332030206F[Truncated for Length]

I am attempting to replicate this in a little c# program.

However, when I use either SqlCommand or LINQ I get the output: JVBERi0xLjINCg0KMyAwIG9iag0KPDwNCi9FIDIxODgyDQovSCBbIDExNTAgMTUxIF0NCi9MIDIyM[TRUNCATED]

Any thoughts to what I need to do to get the 0x25... output to display? I have tried doing byte[] conversions and Encoding, but can't seem to find the right one.

If I am not clear, please let me know.

Edit: I am not trying to display the image, just the 0x25...

Thoughts?

1
  • "JVBERi0xLjINCg0...." looks like encoded. What do you get when you print the byte array? Commented Sep 1, 2009 at 22:38

2 Answers 2

4
string forDisplay = "0x" + BitConverter.ToString(yourByteArray).Replace("-", "");
Sign up to request clarification or add additional context in comments.

Comments

1

AFAIK, .Net does not include any methods to print an arbitrary byte array as a single hex number. (Until .Net 4.0's System.Numerics.BigInteger)

However, you should be able to write

"0x" + BitConverter.ToString(bytes).Replace("-", "")

Note that this is incredibly inefficient; if you want to do this in real code, use a StringBuilder in a loop.
EDIT: Like this:

public static string ToHexString(this byte[] bytes) {
    var builder = new StringBuilder(bytes.Length * 2 + 2);

    builder.Append("0x");

    for (int i = 0; i < bytes.Length; i++)
        builder.Append(bytes[i].ToString("X2"));

    return builder.ToString();
}

2nd EDIT: Here's a new version which really is faster. (Yes, I know you don't need it; here it is anyway)

private static char ToHexDigit(int i) {
    if (i < 10) 
        return (char)(i + '0');
    return (char)(i - 10 + 'A');
}
public static string ToHexString(byte[] bytes) {
    var chars = new char[bytes.Length * 2 + 2];

    chars[0] = '0';
    chars[1] = 'x';

    for (int i = 0; i < bytes.Length; i++) {
        chars[2 * i + 2] = ToHexDigit(bytes[i] / 16);
        chars[2 * i + 3] = ToHexDigit(bytes[i] % 16);
    }

    return new string(chars);
}

5 Comments

Thank you also for your response. this program will not go into production, so i don't have a worry for performance. I appreciate your responses!
@SLaks: I just ran some quick tests, and the StringBuilder loop appears to be 2 or 3 times slower than the "incredibly inefficient" BitConverter/Replace.
I cannot imagine why; I'll take a look.
I reproduced the slowness. I suspect it's because of byte.ToString, which needs to parse its input & handle cultures, etc. Here's a version which really is faster.
@SLaks: Yep, the new version is faster! This is pretty much what BitConverter.ToString does behind-the-scenes, but obviously you skip adding the hyphens which means that you don't then need the Replace call to remove them afterwards.

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.