Skip to content

Instantly share code, notes, and snippets.

@JaimeStill
Created March 25, 2024 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaimeStill/507f0dbdd39a4d1a1cc9462009f5463c to your computer and use it in GitHub Desktop.
Save JaimeStill/507f0dbdd39a4d1a1cc9462009f5463c to your computer and use it in GitHub Desktop.
Extract SQL Image Hex string to File
const string BaseData = "0xFFD8FFE0..."
await ConvertToImage("data", BaseData);
static async Task ConvertToImage(string file, string hex)
{
string format = GetFormat(hex);
byte[] data = Convert.FromHexString(hex.Remove(0, 2));
await File.WriteAllBytesAsync($"{file}{format}", data);
}
static string GetFormat(string hex) => new string(hex.Take(6).ToArray()) switch
{
"0xFFD8" => ".jpg",
"0x8950" => ".png",
_ => throw new Exception("The provided data is not an image")
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment