0

I have a string in my database that represents an image. It looks like this:

0x89504E470D0A1A0A0000000D49484452000000F00000014008020000000D8A66040....
<truncated for brevity>

When I load it in from the database it comes in as a byte[]. How can I convert the string value to a byte array myself. (I am trying to remove the db for some testing code.)

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • Can you provide code for "loading from database"? – Nayan Apr 19 '10 at 19:33
  • It is loaded by linq-to-dataset for my app. (The app that loads it is a Compact framework app.) I tested the loading and saving to a file so I know that works. I then send the byte[] over the wire and store it in a SQL Server 2008 db (via linq-to-sql). I don't pull it out of the 2008 db yet. The goal of this is to find an easy way to verify that the image is in the db correctly. – Vaccano Apr 19 '10 at 19:42
  • Thanks to all the answers. I went with the linq version cause I like linq. Everyone who figured I need to go from hex to byte[] got an upvote from me. Thanks again! – Vaccano Apr 19 '10 at 19:46

6 Answers6

3
class Program
{
    static void Main()
    {
        byte[] bytes = StringToByteArray("89504E470D0A1A0A0000000D49484452000000");
    }

    public static byte[] StringToByteArray(string hex)
    {
        int length = hex.Length;
        byte[] bytes = new byte[length / 2];
        for (int i = 0; i < length; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

It sounds like you're asking how to convert a string with a particular encoding to a byte array.

If so, it depends on how the string is encoded. For example, if you have a base64 encoded string, then you could get a byte array using:

asBytes = System.Text.Encoding.UTF8.GetBytes(someString);

If the encoding is hexadecimal (as it appears to be in your example) there's nothing built into the BCL, but you could use LINQ (just remove the 0x at the head of the string first):

public static byte[] StringToByteArray(string hex) { 
    return Enumerable.Range(0, hex.Length). 
        Where(x => 0 == x % 2). 
        Select(x => Convert.ToByte(hex.SubString(x,2), 16)). 
        ToArray(); 
} 
Community
  • 1
  • 1
LBushkin
  • 129,300
  • 32
  • 216
  • 265
2

I believe you're actually trying to convert the hexadecimal number to a byte array.

If so, you can do it like this: (Remove the 0x first)

var bytes = new byte[str.Length / 2];
for(int i = 0; i < str.Length; i += 2)
    bytes[i / 2] = Convert.ToByte(str.Substring(i, 2), 16);

(Tested)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

If it comes in as a byte[], then it's not a string.

What is the column data type? Varbinary?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • It is varbinary[max]. But I am taking what is shown by SSMS and copying that. (So at that point it is a string.) I want to write an simple tool that will allow me to plug that value into the app and it will show me the image. – Vaccano Apr 19 '10 at 19:36
  • 1
    Then you need to parse the a hexadecimal number. See my answer. – SLaks Apr 19 '10 at 19:38
1

String in .NET means “text” – there’s a fundamental difference to a byte array and no 1:1 mapping exists (just as for other complex .NET types).

In particular, a string is encoded in an appropriate character encoding. As has already been posted, a given text can be decoded into a desired byte representation. In your particular case, it looks as though you’ve got a individual bytes represented as padded hexadecimal numbers, i.e. every byte is two characters wide:

int bytelength = (str.Length - 2) / 2;
byte[] result = new byte[byteLength]; // Take care of leading "0x"
for (int i = 0; i < byteLength; i++)
    result[i] = Convert.ToByte(str.Substring(i * 2 + 2, 2), 16);
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

String to Byte Array Conversion in C#
http://www.chilkatsoft.com/faq/dotnetstrtobytes.html

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I gave that a try and the conversion gave me a byte[] but it did not work when I plugged it into this: `MemoryStream stream = new MemoryStream(bytes); pictureBox1.Image = new Bitmap(stream);` – Vaccano Apr 19 '10 at 19:38