/// <summary>
/// データ変換(byte[]->Structure)
/// </summary>
/// <param name="argBytes"></param>
/// <param name="argType"></param>
/// <param name="argDataInfo"></param>
private void BytesToStructure(byte[] argBytes, Type argType, out Object argDataInfo)
{
argDataInfo = new Object();
int iSize = Marshal.SizeOf(argType);
IntPtr structPtr = Marshal.AllocHGlobal(iSize);
Marshal.Copy(argBytes, 0, structPtr, iSize);
//Marshal.Copy(bytes, 0, structPtr, bytes.Length);
argDataInfo = Marshal.PtrToStructure(structPtr, argType);
Marshal.FreeHGlobal(structPtr);
}
/// <summary>
/// データ変換(Structure->byte[])
/// </summary>
/// <param name="argDataInfo"></param>
/// <param name="argType"></param>
/// <returns></returns>
private byte[] StructureToBytes(object argDataInfo, Type argType)
{
// StructureToPtr
IntPtr pnt = IntPtr.Zero;
pnt = Marshal.AllocHGlobal(Marshal.SizeOf(argDataInfo));
Marshal.StructureToPtr(argDataInfo, pnt, false);
// IntPtrToBytes
int size = Marshal.SizeOf(argType);
byte[] bValue = new byte[size];
Marshal.Copy(pnt, bValue, 0, size);
Marshal.FreeHGlobal(pnt);
return bValue;
}
/// <summary>
/// データ変換(Short->byte[])
/// </summary>
/// <param name="argValue1"></param>
/// <param name="argValue2"></param>
/// <returns></returns>
private byte[] ShortToBytes(ushort argValue1, ushort argValue2)
{
byte[] bReturn = new byte[2];
byte[] bValue1 = BitConverter.GetBytes(argValue1);
byte[] bValue2 = BitConverter.GetBytes(argValue2);
Array.Copy(bValue1, 0, bReturn, 0, 1);
Array.Copy(bValue2, 0, bReturn, 1, 1);
return bReturn;
}
/// <summary>
/// データ変換(bit->byte[])
/// </summary>
/// <param name="argBitValue"></param>
/// <param name="argShortValue"></param>
/// <returns></returns>
private byte[] BitToBytes(ushort argBitValue, ushort argShortValue)
{
byte[] bValue;
if (argBitValue == 1)
{
int iValue = (int)argShortValue | (1 << 15);
bValue = BitConverter.GetBytes(iValue);
}
else
{
bValue = BitConverter.GetBytes(argShortValue);
}
return bValue;
}
/// <summary>
/// データ変換(byte[]->Short)
/// </summary>
/// <param name="argValue"></param>
/// <returns></returns>
private ushort BytesToShort(byte[] argValue)
{
int iValue = BitConverter.ToUInt16(argValue, 0);
if ((iValue & (1 << 15)) == (1 << 15))
{
iValue = iValue - (1 << 15);
}
return ushort.Parse(iValue.ToString());
}
/// <summary>
/// データ変換(byte[]->String)
/// </summary>
/// <param name="argBytes"></param>
/// <param name="argReceiveDataSize"></param>
/// <returns></returns>
public string ConvertBytesToStr(byte[] argBytes, int argReceiveDataSize)
{
char[] cDummyChars;
string strValue = String.Empty;
cDummyChars = new char[argReceiveDataSize];
for (int i = 0; i < cDummyChars.Length; i++)
{
cDummyChars[i] = Convert.ToChar(argBytes[i]);
}
strValue = new String(cDummyChars);
return strValue;
}