1

I'm trying to send a string contained a varbinary into an ImageView

   string ImageHexAsString = "0xFFD8FFE000104A464946000101010...";//Here is my string as VarBinary


    byte[] toBytes = Encoding.ASCII.GetBytes(ImageHexAsString); //Here i'm converting string to byte[]
    Bitmap bitmap = BitmapFactory.DecodeByteArray(toBytes, 0, toBytes.Length);
    imageView.SetImageBitmap(bitmap); //and here i'm send it to imageview

I get an empty white image nothing more.Is something wrong?

NicoRiff
  • 4,803
  • 3
  • 25
  • 54
Dimitris
  • 69
  • 7
  • are you sure the data that you're decoding is the same as the original encoded data? Have you debugged each step of the encode/decode process? – Jason Nov 02 '17 at 19:28
  • `GetBytes` will literally give you the bytes for the string. You need to find a way to convert your string representation of the bytes. – TyCobb Nov 02 '17 at 19:30
  • How should i check that? – Dimitris Nov 02 '17 at 19:31

2 Answers2

0

UPDATE: Just realized this is about c#, not Java. Answer is the same though. Possible duplicate of How do you convert a byte array to a hexadecimal string, and vice versa?

This is probably a duplicate of Convert a string representation of a hex dump to a byte array using Java?.

What's happening is that you shouldn't try and interpret the hex string as text (with Encoding.ASCII.GetBytes()), because it isn't, it's an image.

Hintham
  • 1,078
  • 10
  • 29
0

SOLVED:

   ImageHexAsString = "0xFFD8FFE000104A4649460001010100....";
        List<byte> byteList = new List<byte>();

        string hexPart = ImageHexAsString.Substring(2);
        for (int i = 0; i < hexPart.Length / 2; i++)
        {
            string hexNumber = hexPart.Substring(i * 2, 2);
            byteList.Add((byte)Convert.ToInt32(hexNumber, 16));
        }

        byte[] original = byteList.ToArray();
        Bitmap bitmap = BitmapFactory.DecodeByteArray(original, 0, original.Length);
        imageView.SetImageBitmap(bitmap);

it seems to work with 256x256 resolution I dont know how to change the struck if image is bigger

Dimitris
  • 69
  • 7