206 lines
5.9 KiB
C#
206 lines
5.9 KiB
C#
namespace NfcC7_DLL.NfcHanler.Utils
|
|
{
|
|
public class Conversion
|
|
{
|
|
/// <summary>
|
|
/// Attempts to parse a string as a uint. If the string starts with "0x",
|
|
/// it is assumed to be hex, otherwise, it is decimal.
|
|
/// </summary>
|
|
/// <param name="s">the string to parse</param>
|
|
/// <param name="value">the value represented by s, if successfully parsed</param>
|
|
/// <returns>true if the conversion was successful. False if s doesn't contain
|
|
/// a valid integer.</returns>
|
|
|
|
public static bool TryParseHexOrDecString(string s, out Byte value)
|
|
{
|
|
try
|
|
{
|
|
if ((s.Length > 2) && (s[0] == '0') && (s[1] == 'x'))
|
|
{
|
|
value = Convert.ToByte(s, 16);
|
|
}
|
|
else
|
|
{
|
|
value = Byte.Parse(s);
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
value = 0;
|
|
return false;
|
|
}
|
|
}
|
|
public static bool TryParseHexOrDecString(string s, out SByte value)
|
|
{
|
|
try
|
|
{
|
|
if ((s.Length > 2) && (s[0] == '0') && (s[1] == 'x'))
|
|
{
|
|
value = Convert.ToSByte(s, 16);
|
|
}
|
|
else
|
|
{
|
|
value = SByte.Parse(s);
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
value = 0;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool TryParseHexOrDecString(string s, out Int16 value)
|
|
{
|
|
try
|
|
{
|
|
if ((s.Length > 2) && (s[0] == '0') && (s[1] == 'x'))
|
|
{
|
|
value = Convert.ToInt16(s, 16);
|
|
}
|
|
else
|
|
{
|
|
value = Int16.Parse(s);
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
value = 0;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool TryParseHexOrDecString(string s, out UInt16 value)
|
|
{
|
|
value = 0;
|
|
try
|
|
{
|
|
if ((s.Length > 2) && (s[0] == '0') && (s[1] == 'x'))
|
|
{
|
|
s = s.Substring(2);
|
|
value = Convert.ToUInt16(s, 16);
|
|
}
|
|
else
|
|
{
|
|
value = Convert.ToUInt16(s);
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public static bool TryParseHexOrDecString(string s, out UInt32 value)
|
|
{
|
|
value = 0;
|
|
try
|
|
{
|
|
if ((s.Length > 2) && (s[0] == '0') && (s[1] == 'x'))
|
|
{
|
|
s = s.Substring(2);
|
|
value = Convert.ToUInt32(s, 16);
|
|
}
|
|
else
|
|
{
|
|
value = Convert.ToUInt32(s);
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool TryParseHexOrDecString(string s, out Int32 value)
|
|
{
|
|
value = 0;
|
|
try
|
|
{
|
|
if ((s.Length > 2) && (s[0] == '0') && (s[1] == 'x'))
|
|
{
|
|
s = s.Substring(2);
|
|
value = Convert.ToInt32(s, 16);
|
|
}
|
|
else
|
|
{
|
|
value = Convert.ToInt32(s);
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool TryParseHexOrDecString(string s, out UInt64 value)
|
|
{
|
|
try
|
|
{
|
|
if ((s.Length > 2) && (s[0] == '0') && (s[1] == 'x'))
|
|
{
|
|
value = Convert.ToUInt64(s, 16);
|
|
}
|
|
else
|
|
{
|
|
value = UInt64.Parse(s);
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
value = 0;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a hex string to an array of bytes.
|
|
/// </summary>
|
|
/// <param name="hexString">A hexadecimal string. Must have an even number of nybbles (ie, no lone nybbles).
|
|
/// Valid separators between bytes are ':', ',', '.', ' ', '\t', and '-'.
|
|
/// </param>
|
|
/// <returns>hexString converted to bytes. If hexString contains invalid characters or an odd number of nybbles, null is returned.</returns>
|
|
public static byte[] HexStringToBytes(string hexString)
|
|
{
|
|
hexString = hexString.Replace("0x", "");
|
|
hexString = hexString.Replace('\t', ' ');
|
|
hexString = hexString.Replace(':', ' ');
|
|
hexString = hexString.Replace('.', ' ');
|
|
hexString = hexString.Replace('-', ' ');
|
|
hexString = hexString.Replace(',', ' ');
|
|
hexString = hexString.Replace(" ", "");
|
|
|
|
// string length cannot be ODD!
|
|
if ((hexString.Length & 0x01) != 0) return null;
|
|
|
|
byte[] byteVals = new byte[hexString.Length / 2];
|
|
|
|
// parse the string one character at a time, going nybble by nybble
|
|
for (int i = 0; i < byteVals.Length; i++)
|
|
{
|
|
try
|
|
{
|
|
string byteStr = hexString.Substring(i * 2, 2);
|
|
|
|
byteVals[i] = (byte)int.Parse(byteStr, System.Globalization.NumberStyles.HexNumber);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
// the parsing failed, so skip this character in theChars
|
|
}
|
|
}
|
|
|
|
return byteVals;
|
|
}
|
|
|
|
}
|
|
}
|