.NET framework provides methods to convert a byte array into a Hexadecimal string ( byte.To String(“X”) ), it is not so easy to convert a hexadecimal string back into a byte array.This Example Shows u how to convert byte array into a Hexadecimal string and Hexadecimal string to byte array. Let us See
This is for Byte array to String:-
Here U can pass Sql Rowversion also.
public static string ByteArrayToString(byte[] rowVersion)
{
string retVal = "0x";
string toAdd = "";
foreach (byte b in rowVersion)
{
toAdd = b.ToString("X");
if (toAdd.Length == 1)
{
retVal += "0" + toAdd;
}
else
{
retVal += toAdd;
}
}
return retVal;
}
This for Hexadecimal string to byte array:-
public static byte[] StringToByteArray(string rowVersion)
{
if (rowVersion.Length != 18)
{
throw new Exception();
}
byte[] retVal = new byte[8];
for (int index = 2; index < 18; index += 2)
{
retVal[(index / 2) - 1] =
(byte)int.Parse(
rowVersion.Substring(index, 2),
System.Globalization.NumberStyles.HexNumber);
}
return retVal;
}
I hope this two methods will help u
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment