//十六进制字符串转换为十进制
string str = "0c";
int i = int.Parse(str, System.Globalization.NumberStyles.HexNumber); //输出i=12
i = Convert.ToInt32(str, 16); // //输出i=12
public static byte[] getLongToBytes(long s, bool asc) ///ASC=true正常升序输出,false降序输出
{
// long L = 391768031;
byte[] buf = new byte[8];
if (asc)
{
for (int i = 0; i < buf.Length; i++)
{
buf[i] = (byte)(s & 0x00000000000000ff); //和0与去掉高位数据,获取最低位数据
s >>= 8; //右移8位
}
}
else
{
for (int i = buf.Length - 1; i >= 0; i--)
{
buf[i] = (byte)(s & 0x00000000000000ff);
s >>= 8;
}
}
return buf;
}
//byte[]转换为long
public static long getByteToLong(byte[] buf, bool asc)
{
if (buf == null)
{
throw new Exception("byte array is null!");
}
if (buf.Length > 8)
{
throw new Exception("byte array size > 8 !");
}
long r = 0;
if (asc)
{
for (int i = 0; i < buf.Length; i++)
{
r <<= 8;
r |= buf[i] & (long)(0x00000000000000ff);
// r |= Convert.ToInt64((buf[i] & 0x00000000000000ff));
}
}
else
for (int i = buf.Length - 1; i >= 0; i--)
{
r <<= 8;
r |= buf[i] & (long)(0x00000000000000ff);
}
return r;
}
/// <summary>
/// 十六进制字符串转化为List<byte>
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public List<byte> HexStringTobytes(string str)
{
List<byte> lst = new List<byte>();
string TmpStr = str.ToUpper().Replace("0X", "");
try
{
for (int i = 0; i < TmpStr.Length; i += 2)
{
int iStr = Convert.ToInt32(TmpStr.Substring(i, 2), 16);
lst.Add((byte)iStr);
}
// byte[] b = lst.ToArray(); //List<byte>转换为byte[]
}
catch (Exception e)
{
lst = null;
}
return lst;
}
.