Created
March 25, 2024 20:41
-
-
Save JaimeStill/507f0dbdd39a4d1a1cc9462009f5463c to your computer and use it in GitHub Desktop.
Extract SQL Image Hex string to File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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