这些都是没用的
这篇文章呢是我转载的,是一篇对于C#的各种数据类型转换成byte数组的
因为在写网络的时候经常会遇到各种奇怪的数据类型,就得需要你转成byte数组,所以就有了这篇文章,话不多说,上代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KSNET
{
using int8 = System.SByte;
using int16 = System.Int16;
using int32 = System.Int32;
using int64 = System.Int64;
using uint8 = System.Byte;
using uint16 = System.UInt16;
using uint32 = System.UInt32;
using uint64 = System.UInt64;
using size_t = System.UInt32;
public class OPBYTE
{
/**********************************************************************
* Function: AnyToByte Overloaded
* Version: 1.00
* Author: KylinStar Lee
* Date: 2014-01-03
* Description: Convert any number to byte[].
* Range of supported types:
* short / int / int8 / int16 / int32 / int64 / double / float
*
* @param arg -- a const any type of number value
*
* @return a new combination byte
**********************************************************************/
public static byte[] AnyToByte(int8 arg)
{
byte[] b = new byte[1];
b[0] = (byte)(arg & 255);
Array.Reverse(b);
return b;
}
public static byte[] AnyToByte(int16 arg)
{
byte[] b = new byte[2];
b[0] = (byte)(arg >> 8);
b[1] = (byte)(arg & 255);
Array.Reverse(b);
return b;
}
public static byte[] AnyToByte(int32 arg)
{
byte[] b = new byte[4];
b[0] = (byte)(arg >> 24);
b[1] = (byte)((arg >> 16) & 255);
b[2] = (byte)((arg >> 8) & 255);
b[3] = (byte)(arg & 255);
Array.Reverse(b);
return b;
}
public static byte[] AnyToByte(int64 arg)
{
byte[] b = new byte[8];
b[0] = (byte)(arg >> 56);
b[1] = (byte)((arg >> 48) & 255);
b[2] = (byte)((arg >> 40) & 255);
b[3] = (byte)((arg >> 32) & 255);
b[4] = (byte)((arg >> 24) & 255);
b[5] = (byte)((arg >> 16) & 255);
b[6] = (byte)((arg >> 8) & 255);
b[7] = (byte)(arg & 255);
Array.Reverse(b);
return b;
}
public static byte[] AnyToByte(float arg)
{
//int32 n = (int32)(arg * 100000);
//return AnyToByte(n);
byte[] b = new byte[4];
b = BitConverter.GetBytes(arg);
return b;
}
public static byte[] AnyToByte(double arg)
{
//int64 n = (int64)(arg * 10000000);
//return AnyToByte(n);
byte[] b = new byte[8];
b = BitConverter.GetBytes(arg);
return b;
}
public static byte[] AnyToByte(string arg)
{
byte[] b = System.Text.Encoding.Default.GetBytes(arg);
return b;
}
/// <summary>
/// <para>Function: [ ByteToAny Main Func ]</para>
/// <para>Author: KylinStar Lee.</para>
/// <para>Create Date: 2014-04-15.</para>
/// <para>Update Date: 0000-00-00</para>
/// <para>Description: Convert byte[] value to any types value.</para>
/// <para>Range of supported types: All types it has been supported.</para>
/// </summary>
/// <param name="arg">@param arg are any types of the const byte[] value.</param>
/// <returns>@return a type of T's data.</returns>
/// <example>
/// This is example for how to using ByteToAny opreating byte to any types value
/// <code>
/// public static object ByteToAnyUInt(byte[] arg, object any)
/// {
/// return OPBYTE.ByteToAny (arg);
/// }
/// </code>
/// </example>
/// <typeparam name="T">The type of data currently being operated. </typeparam>
public static int8 ByteToInt8(byte[] arg)
{
SByte b = Convert.ToSByte(arg[0]);
return b;
}
public static int16 ByteToInt16(byte[] arg)
{
Int16 b = BitConverter.ToInt16(arg, 0);
return b;
}
public static int32 ByteToInt32(byte[] arg)
{
int32 b = BitConverter.ToInt32(arg, 0);
return b;
}
public static int64 ByteToInt64(byte[] arg)
{
int64 b = BitConverter.ToInt64(arg, 0);
return b;
}
public static double ByteToDouble(byte[] arg)
{
double b = BitConverter.ToDouble(arg, 0);
return b;
}
public static float ByteToFloat(byte[] arg)
{
float b = BitConverter.ToSingle(arg, 0);
return b;
}
public static int ByteToInt(byte[] arg)
{
int b = BitConverter.ToInt32(arg, 0);
return b;
}
public static uint32 ByteToUInt(byte[] arg)
{
uint32 b = BitConverter.ToUInt32(arg, 0);
return b;
}
public static uint8 ByteToUInt8(byte[] arg)
{
uint8 b = (uint8)ByteToInt8(arg);
return b;
}
public static uint16 ByteToUInt16(byte[] arg)
{
uint16 b = BitConverter.ToUInt16(arg, 0);
return b;
}
public static uint32 ByteToUInt32(byte[] arg)
{
uint32 b = BitConverter.ToUInt32(arg, 0);
return b;
}
public static uint64 ByteToUInt64(byte[] arg)
{
uint64 b = BitConverter.ToUInt64(arg, 0);
return b;
}
public static string BtyeToString(byte[] arg)
{
string b = System.Text.Encoding.Default.GetString(arg);
return b;
}
/**********************************************************************
* Function: GetByteByRange
* Version: 1.00
* Author: KylinStar Lee
* Date: 2014-01-03
* Description: Get bytes from src by range
*
* @param src -- is being operated src of Bytes
* @param startindex -- is being operated src's start index
* @param range -- is being operated src range for get bytes
*
* @return a new combination byte
**********************************************************************/
public static byte[] GetByteByRange(byte[] src, int startindex, int range)
{
int nAbsolutelyRange = startindex + range;
if (src.Length < nAbsolutelyRange)
{
return null;
}
byte[] b = new byte[range];
for (int i = 0; i <= range - 1; i++)
{
b[i] = src[startindex + i];
}
return b;
}
/**********************************************************************
* Function: InsertByteIntoByte
* Version: 1.00
* Author: KylinStar Lee
* Date: 2014-01-03
* Description: Insert resByte to byte[].
*
* @param srcByte -- is being operated srcByte
* @param startindex -- is being operated srcByte's start index
* @param resByte -- is resources to be inserted into the byte
*
* @return a new combination byte
**********************************************************************/
public static byte[] InsertByteIntoByte(byte[] srcByte, int startindex, byte[] resByte)
{
if (startindex > srcByte.Length)
{
startindex = srcByte.Length;
}
int insertLength = startindex + resByte.Length;
int newByteLen = srcByte.Length + resByte.Length;
byte[] b = new byte[newByteLen];
int j = 0;
for (int i = 0; i < newByteLen; i++)
{
if (i <= startindex)
{
b[i] = srcByte[i];
}
else if (i > insertLength && insertLength < srcByte.Length)
{
b[i] = srcByte[i - insertLength];
}
else if (j < (resByte.Length - startindex))
{
b[i] = resByte[j];
j++;
}
}
srcByte = b;
return b;
}
/**********************************************************************
* Function: AppendByteOnTail
* Version: 1.00
* Author: KylinStar Lee
* Date: 2014-01-03
* Description: Append resByte to byte[].
*
* @param srcByte -- is being operated srcByte
* @param resByte -- is resources to be appended to the byte
*
* @return a new combination byte
**********************************************************************/
public static byte[] AppendByteOnTail(byte[] srcByte, byte[] resByte)
{
if (srcByte == null)
{
srcByte = new byte[0];
}
int count = 0;
int newByteLength = srcByte.Length + resByte.Length;
byte[] b = new byte[newByteLength];
if (srcByte.Length != 0)
{
for (int i = 0; i <= srcByte.Length - 1; i++)
{
b[i] = srcByte[i];
count = i;
}
}
if (srcByte.Length == 0)
{
for (int j = 0; j <= resByte.Length - 1; j++)
{
b[count + j] = resByte[j];
}
}
else
{
for (int j = 0; j <= resByte.Length - 1; j++)
{
b[count + j + 1] = resByte[j];
}
}
srcByte = b;
return b;
}
/**********************************************************************
* Function: ModifyByteByRange
* Version: 1.00
* Author: KylinStar Lee
* Date: 2014-01-03
* Description: Modify Byte By Range
*
* @param srcByte -- is being operated srcByte
* @param startindex -- is being operated srcByte's start index
* @param range -- is being operated srcByte range for modify bytes
* @param resByte -- is resources to be inserted into the byte
*
* @return a new combination byte
**********************************************************************/
public static byte[] ModifyByteByRange(byte[] srcByte, int startindex, int range, byte[] resByte)
{
int ModMaxIndex = startindex + range;
if (srcByte.Length <= ModMaxIndex)
{
return null;
}
for (int i = startindex; i <= range; i++)
{
srcByte[i] = resByte[i - startindex];
}
return srcByte;
}
/**********************************************************************
* Function: ModifyByteByRange Overloaded
* Version: 1.00
* Author: KylinStar Lee
* Date: 2014-01-03
* Description: Modify Byte By Range
*
* @param srcByte -- is being operated srcByte
* @param startindex -- is being operated srcByte's start index
* @param resByte -- is resources to be inserted into the byte
*
* @return a new combination byte
**********************************************************************/
public static byte[] ModifyByteByRange(byte[] srcByte, int startindex, byte[] resByte)
{
int ModMaxIndex = startindex + resByte.Length;
if (srcByte.Length < ModMaxIndex)
{
return srcByte;
}
for (int i = startindex; i <= resByte.Length - 1 + startindex; i++)
{
srcByte[i] = resByte[i - startindex];
}
return srcByte;
}
/**********************************************************************
* Function: ShowBytes
* Version: 1.00
* Author: KylinStar Lee
* Date: 2014-01-03
* Description: Just printf bytes method
*
* @param srcByte -- is being showed srcByte
**********************************************************************/
public static void ShowBytes(byte[] b)
{
//Int16 temp = 0;
//Console.WriteLine((Int16)ByteToAnyInt(b,temp));
//Console.WriteLine(temp);
}
}
}