0

I have such a string :

string x = 0x424D3630090000000000360010028000000C00100200011111111111333333333000000C40E0000C40E000000088888888BBBBCC262281FF231F7EFF251D81FF....."

I'm told to convert this string to JPEG image, I'm not sure about the actual data type of this string and I don't know how to convert it to JPEG. Could you give me at least a few tips about that? Thanks in advance.

EDIT:

I converted the string to byte Array like this :

 byte[] bytes = Convert.FromBase64String("0x424D363009..");

And I get this exception :

Invalid length for a Base-64 char array or string.

jason
  • 6,163
  • 28
  • 94
  • 178
  • 2
    Possible duplicate of [converting a base 64 string to an image and saving it](https://stackoverflow.com/questions/5400173/converting-a-base-64-string-to-an-image-and-saving-it) – SᴇM Nov 06 '17 at 12:56
  • Convert it to a set of bytes and then you have "convert bytes to jpeg in C#", of which there are numerous questions and answers here on Stack Overflow. – Lasse V. Karlsen Nov 06 '17 at 12:57
  • 1
    Not much familiar with c# but, this seems to be hex string, so first find out how to convert hex string to byte array, then byte array to image – Rahul K Nov 06 '17 at 12:59
  • Looking at the first two hex values - 0x42 0x4D - I would guess the string is a [BitMap](https://en.wikipedia.org/wiki/BMP_file_format) file - that should give a starting point after conversion to byte array. – PaulF Nov 06 '17 at 13:22
  • @PaulF please see my update. – jason Nov 06 '17 at 13:29
  • 1
    It is not a base 64 string - each pair of characters represents an 8 bit value - so your array should be - 0x42, 0x4D, 0x36, 0x30 ..... so you may have to manually convert each pair of characters. Looking at your string I think there is an error in there as I would expect 00,00,00,28 to follow 36 - see also this : https://asecuritysite.com/forensics/bmp?file=activated.bmp – PaulF Nov 06 '17 at 13:33
  • @SeM-ՍեՄ Thanks but it didn't work.. – jason Nov 06 '17 at 13:34
  • https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array As @PaulF stated, this is not base64. Look at examples of base64 and that will be obvious. Follow the steps in the linked SO question first, then convert from BitMap to JPEG – Novaterata Nov 06 '17 at 13:46
  • if using link from @Novaterata then you either need to remove the leading "0x" or skip it in the returned values by changing the where clause to _".Where(x => x > 1 && x % 2 == 0)"_ – PaulF Nov 06 '17 at 13:54
  • What class is this for? File format reverse engineering? Because that data starts with "BM6" as file format indicator, which seems like some kind of bmp to me. – Nyerguds Nov 16 '17 at 15:34

1 Answers1

3

UPDATE:

One more possible solution, you can get byte[] from that HEX string using:

string x = GetYouHexString();
x = x.Remove(0,2);
string[] stringArr = Enumerable.Range(0, x.Length / 2)
                               .Select(i => x.Substring(i * 2, 2))
                               .ToArray();
byte[] byteArr = Array.ConvertAll(stringArr , b => Convert.ToByte(b, 16));

then save it using MemoryStream and Image:

using(Image image = Image.FromStream(new MemoryStream(byteArr)))
{
    image.Save("output.jpg", ImageFormat.Jpeg);
}
SᴇM
  • 6,506
  • 3
  • 23
  • 36
  • This gave "Parameter is not valid." error at this line : `using(Image image = Image.FromStream(new MemoryStream(byteArr)))` – jason Nov 06 '17 at 13:54
  • @jason where did you tested it? – SᴇM Nov 06 '17 at 13:57
  • On Visual Studio. – jason Nov 06 '17 at 13:58
  • If your string is as above - then it is likely incorrect as it has the wrong header info - as I said above I would expect 00000028 after the second 36. – PaulF Nov 06 '17 at 13:58
  • I've created console application and tested, works perfectly fine. – SᴇM Nov 06 '17 at 13:59
  • @PaulF The original first part is like this : 424D36300900000000003600000028000000C00100005001000001 – jason Nov 06 '17 at 14:16
  • @PaulF I understand that, only I was busy and tested only this one, but this will give him a basic idea, how to do that. For example he can add a leading 0x to each string byte inside Convert.ToByte method. – SᴇM Nov 06 '17 at 14:22
  • @Jason: that makes more sense - it is a 448 (0x000001C0) x 336 (0x00000150) bitmap. - Note the values are stored low byte first. – PaulF Nov 06 '17 at 14:23
  • @PaulF so what should I do? Is sem-utu's answer applicable? I've tried other conversion methods that I found on SO, they didn't work either.. – jason Nov 06 '17 at 14:30
  • The conversion to byte array looks correct, as does the one in Novaterata's link above (if you skip 0x as I said) - this link may work : https://stackoverflow.com/questions/21555394/how-to-create-bitmap-from-byte-array/21555447 – PaulF Nov 06 '17 at 14:36
  • I have tried @Novaterata's link and I get this : **System.ArgumentOutOfRangeException' Index and length must refer to a location within the string** – jason Nov 06 '17 at 14:49
  • What is the length of your string? - That looks like you have only skipped the 0 not the 0x, or after skipping the two characters you have a string with an odd number of characters - as each byte is represented by 2 chars in the string - the string passed to StringToByteArray MUST be even in length. I have tried @SeM-ՍեՄ's answer + the one I just linked to & both work when given a valid byte array. – PaulF Nov 06 '17 at 14:56
  • Yeah, there is something wrong with the string, its length is always odd... – jason Nov 06 '17 at 15:04
  • For a basic 448x336, 24bit bitmap - I would expect a file size of 451638 bytes ((448 x 336) x 3 bytes per pixel + 54 bytes header) so a string length of 903278 (including 0x on front). – PaulF Nov 06 '17 at 15:13
  • @PaulF Is there a way to convert strings with odd length to image? – jason Nov 06 '17 at 15:29
  • As I said - each byte is represented by 2 hex characters, so there is absolutely no way a valid string can have an odd length. You could try removing the last character or adding an extra "0" (or other valid hex character). You should be able to save the byte array using File.WriteAllBytes as a .bmp file & see what you get. – PaulF Nov 06 '17 at 15:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158332/discussion-between-jason-and-paulf). – jason Nov 06 '17 at 15:41
  • @PaulF, When I add '0' or remove the last character, I'm getting _Parameter is not valid._ exception... – jason Nov 06 '17 at 15:42
  • When I save the .bmp, Windows complains that it's not a valid .bmp file. – jason Nov 06 '17 at 15:46