using System;
using System.Collections.Generic;
using System.Text;
namespace Neo.ActiveX.FingerprintReader
{
class ByteFunctions
{
///
/// Converts a byte array to a readable string format.
///
/// The byte array to be queried.
/// The byte array as a hex string.
public static string ByteArrayToHexString(byte[] buf)
{
if(buf == null || buf.Length <= 0)
{
return null;
}
const string digits = "0123456789ABCDEF";
byte ch = 0x00;
int i = 0;
StringBuilder sb = new StringBuilder(buf.Length * 2);
while(i < buf.Length)
{
ch = (byte)(buf[i] & 0xF0); // strip off high nibble
ch = (byte)(ch >> 4); // shift the bits down
ch = (byte)(ch & 0x0F); // must do this if high order bit is on!
sb.Append(digits[(int)ch]); // convert the nibble to a String Character
ch = (byte)(buf[i] & 0x0F); // strip off low nibble
sb.Append(digits[(int)ch]); // convert the nibble to a String Character
i++;
}
return sb.ToString();
}
///
/// Converts the given hex string to a byte array.
///
/// The string to be converted.
/// A byte array.
public static byte[] HexStringToByteArray(string s)
{
byte[] buffer = new byte[s.Length / 2];
for(int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)Int32.Parse(s.Substring(2 * i, 2), System.Globalization.NumberStyles.HexNumber);
}
return buffer;
}
}
}