There is no built-in function to do this. Writing a streaming function in C# to do it would be trivial (read 2 chars, emit a byte). You can either use a SQLCLR function to do it inside the engine, or you can do it in the client side. IF the input is large is arguably better to do it in the client side to avoid memory overload.
Here's an example of streaming conversion (no omptimizations, but highlights the important of not converting a string
into a byte[]
):
namespace hex2bin
{
class Program
{
public static void hex2bin(TextReader sin, BinaryWriter sout)
{
char[] block = new char[2];
bool eof = false;
do
{
int chars = sin.ReadBlock(block, 0, 2);
switch (chars)
{
case 0:
eof = true;
break;
case 1:
// Input is odd length, invalid case
throw new Exception("Invalid input");
case 2:
string sblock = new String(block);
byte b = Convert.ToByte(sblock, 16);
sout.Write(b);
break;
}
} while (!eof);
}
static void Main(string[] args)
{
using (StringReader sr = new StringReader("AD5829FC"))
{
using (BinaryWriter bw = new BinaryWriter(new MemoryStream()))
{
hex2bin(sr, bw);
}
}
}
}
}