1. 程式人生 > >各類型轉換成byte[] 和HexString

各類型轉換成byte[] 和HexString

ret class bst 字節 turn utf-8 AS ati har

public class ByteUtil
{
/// <summary>
/// string >>Length
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int getLength(String str)
{
return getBytes(str).Length;
}
/// <summary>
/// string >Encoding>byte[]
/// </summary>
/// <param name="data"></param>
/// <param name="charsetName"></param>
/// <returns></returns>
public static byte[] getBytes(String data, String charsetName)
{
return System.Text.Encoding.GetEncoding(charsetName).GetBytes(data);
}
/// <summary>
/// string >GBK>byte[]
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] getBytes(String data)
{
return getBytes(data, "GBK");
}
/// <summary>
/// short>>byte[]
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] getBytes(short data)
{
byte[] bytes = new byte[2];
bytes[0] = (byte)(data & 0xFF);
bytes[1] = (byte)((data & 0xFF00) >> 8);
return bytes;
}
/// <summary>
/// char>>byte[定長]
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] getBytes(char c, int count)
{
byte[] bytes = new byte[count];
for (int i = 0; i < bytes.Length; ++i)
{
bytes[i] = (byte)c;
}
return bytes;
}
/// <summary>
/// int>length==4>byte[]
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] getBytes(int data)
{
byte[] bytes = new byte[4];
bytes[0] = (byte)(data & 0xFF);
bytes[1] = (byte)((data & 0xFF00) >> 8);
bytes[2] = (byte)((data & 0xFF0000) >> 16);
bytes[3] = (byte)((data & 0xFF000000) >> 24);
return bytes;
}
/// <summary>
/// long>>byte[]
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] getBytes(long data)
{
byte[] bytes = new byte[8];
bytes[0] = (byte)(int)(data & 0xFF);
bytes[1] = (byte)(int)(data >> 8 & 0xFF);
bytes[2] = (byte)(int)(data >> 16 & 0xFF);
bytes[3] = (byte)(int)(data >> 24 & 0xFF);
bytes[4] = (byte)(int)(data >> 32 & 0xFF);
bytes[5] = (byte)(int)(data >> 40 & 0xFF);
bytes[6] = (byte)(int)(data >> 48 & 0xFF);
bytes[7] = (byte)(int)(data >> 56 & 0xFF);
return bytes;
}
/// <summary>
/// float>>byte[]
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] getBytes(float data)
{
int intBits = BitConverter.ToInt32(BitConverter.GetBytes(data), 0);

return getBytes(intBits);
}
/// <summary>
/// GB2312 byte【】==》 中文
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string LanChange(byte[] data)
{
Encoding utf8;
Encoding gb2312;
utf8 = Encoding.GetEncoding("UTF-8");
gb2312 = Encoding.GetEncoding("GB2312");
data = Encoding.Convert(gb2312, utf8, data);
return utf8.GetString(data);
}
/// <summary>
/// 中文==》 GB2312 byte【】
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] ChangeLan(string data)
{
byte[] bs = Encoding.GetEncoding("UTF-8").GetBytes(data);
bs = Encoding.Convert(Encoding.GetEncoding("UTF-8"), Encoding.GetEncoding("GB2312"), bs);
return bs;
}
/// <summary>
/// 16進制字符串轉回string
/// </summary>
/// <param name="hs"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string HexStringToString(string hs, Encoding encode)
{
string strTemp = "";
byte[] b = new byte[hs.Length / 2];
for (int i = 0; i < hs.Length / 2; i++)
{
strTemp = hs.Substring(i * 2, 2);
if (strTemp != "\0\0")
{
b[i] = Convert.ToByte(strTemp, 16);
}
}
//按照指定編碼將字節數組變為字符串
return encode.GetString(b);
}
/// <summary>
/// string 轉回16進制字符串
/// </summary>
/// <param name="hs"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string StringToHexString(string s, Encoding encode)
{
byte[] b = encode.GetBytes(s);//按照指定編碼將string編程字節數組
string result = string.Empty;
for (int i = 0; i < b.Length; i++)//逐字節變為16進制字符
{
result += Convert.ToString(b[i], 16);
}
return result;
}
}

各類型轉換成byte[] 和HexString