You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.6 KiB
129 lines
3.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Configuration;
|
|
using System.Threading;
|
|
namespace SiaSun.LMS.Common
|
|
|
|
{
|
|
/// <summary>
|
|
/// 所有进制之间的转换函数库
|
|
/// Creator:Richard.liu
|
|
/// </summary>
|
|
public static class CCarryConvert
|
|
{
|
|
static Mutex mutex=new Mutex();
|
|
|
|
/// <summary>
|
|
/// 十进制转十六进制
|
|
/// </summary>
|
|
/// <param name="dec">十进制数</param>
|
|
/// <returns>十六进制字符串</returns>
|
|
public static string DecimalToHex(byte dec)
|
|
{
|
|
return Convert.ToString(dec, 16);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 十进制转十六进制
|
|
/// </summary>
|
|
/// <param name="dec"></param>
|
|
/// <returns></returns>
|
|
public static string DecimalToHex(int dec)
|
|
{
|
|
return Convert.ToString(dec, 16);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 十进制转八进制
|
|
/// </summary>
|
|
/// <param name="dec">十进制数</param>
|
|
/// <returns>八进制字符串</returns>
|
|
public static string DecimalToOct(byte dec)
|
|
{
|
|
return Convert.ToString(dec, 8);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 二进制转十进制
|
|
/// </summary>
|
|
/// <param name="bin">二进制字符串</param>
|
|
/// <returns>十进制数</returns>
|
|
public static Int32 BinToDecimal(string bin)
|
|
{
|
|
return Convert.ToInt32(bin, 2);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 八进制转十进制
|
|
/// </summary>
|
|
/// <param name="bin">八进制字符串</param>
|
|
/// <returns>十进制数</returns>
|
|
public static Int32 OctToDecimal(string Oct)
|
|
{
|
|
return Convert.ToInt32(Oct , 8);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 十六进制转十进制
|
|
/// </summary>
|
|
/// <param name="bin">十六进制字符串</param>
|
|
/// <returns>十进制数</returns>
|
|
public static Int32 HexToDecimal(string hex)
|
|
{
|
|
return Convert.ToInt32(hex , 16);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 十六进制转字节型十进制
|
|
/// </summary>
|
|
/// <param name="bin">十六进制字符串</param>
|
|
/// <returns>字节型十进制数</returns>
|
|
public static byte HexToByte(string hex)
|
|
{
|
|
return Convert.ToByte(hex, 16);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 在十进制数获得指定bit位的二进制字符:0/1
|
|
/// </summary>
|
|
/// <param name="Int">十进制数</param>
|
|
/// <param name="bitno">比特位</param>
|
|
/// <returns></returns>
|
|
public static byte GetBitFromInteger(int Int, int bitno)
|
|
{
|
|
if ((Int & Convert.ToInt32(Math.Pow(2, Convert.ToDouble(bitno)))) == Convert.ToInt32(Math.Pow(2, Convert.ToDouble(bitno))))
|
|
{
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// byte[0]存放高八位,byte[1]存放低八位
|
|
/// </summary>
|
|
/// <param name="_int16"></param>
|
|
/// <returns></returns>
|
|
public static byte[] GetByteFromInt16(int _int16)
|
|
{
|
|
char[] cc = new char[1] { '.' };
|
|
string[] sp;
|
|
byte[] _bytes = new byte[2];
|
|
sp = Convert.ToString(_int16 >> 8).Split(cc);
|
|
_bytes[0] =Convert.ToByte( sp[0]);
|
|
_bytes[1] =Convert.ToByte( _int16 & 255);
|
|
return _bytes;
|
|
}
|
|
|
|
public static UInt16 GetInt16FromBytes(byte[] _bytes)
|
|
{
|
|
UInt16 _int16;
|
|
_int16 =Convert.ToUInt16( _bytes[1] +( _bytes[0]*256));
|
|
return _int16;
|
|
}
|
|
}
|
|
}
|