C# 类型转换

/// <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;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值