int byte[]配列との相互回転方法、整数+浮動小数点型
10128 ワード
整数:
浮動小数点:
http://www.cnblogs.com/jhabb/archive/2011/05/06/2038777.html
int byte
public static byte[] intToBytes2(int n){
byte[] b = new byte[4];
for(int i = 0;i < 4;i++)
{
b[i]=(byte)(n>>(24-i*8));
}
return b;
}
byte int
public static int byteToInt2(byte[] b) {
int mask=0xff;
int temp=0;
int n=0;
for(int i=0;i<b.length;i++){
n<<=8;
temp=b[i]&mask;
n|=temp;
}
return n;
}
浮動小数点:
/// <summary>
/// 16 10
/// </summary>
/// <param name="instr"></param>
/// <returns></returns>
public static string ByteToFloat(string instr)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(instr))
{
byte[] floatVals1 = StringToBytes(instr);
result = BitConverter.ToSingle(floatVals1, 0).ToString();
}
return result;
}
/// <summary>
/// 16 10
/// </summary>
/// <param name="instr"></param>
/// <returns></returns>
public static string ByteToInt(string instr)
{
string result = string.Empty;
int n = 0;
if (!string.IsNullOrEmpty(instr))
{
byte[] b = StringToBytes(instr);
int mask = 0xff;
int temp = 0;
for (int i = 0; i < b.Length; i++)
{
n <<= 8;
temp = b[i] & mask;
n |= temp;
}
}
return result = n.ToString();
}
/// <summary>
/// 16 16
/// </summary>
/// <param name="HexString"> 16 </param>
/// <returns> </returns>
public static byte[] StringToBytes(string HexString)
{
byte[] temdata = new byte[HexString.Length / 2];
for (int i = 0; i < temdata.Length; i++)
{
temdata[i] = Convert.ToByte(HexString.Substring(i * 2, 2), 16);
}
return temdata;
}
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string RemoveAllSpace(string str)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(str))
{
result = Regex.Replace(str, @"\s+", "");
}
return result;
}
http://www.cnblogs.com/jhabb/archive/2011/05/06/2038777.html