C# 数据类型转换--(ushort 转换补充)

using System;
using System.Collections.Generic;
using System.Text;

namespace ConvertDataType
{
    class Program
    {
        static void Main(string[] args)
        {
            ushort[] data;
            //Convert ushort array to Long, data[0]=>Low Word, data[1]=>High Word
            data = new ushort[2] { 4, 65535 };
            long longValue;
            longValue =(Int32)(((UInt32)data[1] << 16) | (UInt32)data[0]);
            Console.WriteLine(longValue.ToString()); //-65532
            Console.ReadLine();
            
            //Convert ushort array to UnsignedLong, data[0]=>Low Word, data[1]=>High Word
            data = new ushort[2] { 25520,13587 };
            ulong ulongValue;
            ulongValue = (UInt32)(((UInt32)data[1] << 16) | (UInt32)data[0]);
            Console.WriteLine(ulongValue.ToString()); //890463152
            Console.ReadLine();
            
            //Convert ushort array to Hex 
            data = new ushort[1] { 17988 };          
            Console.WriteLine(data[0].ToString("X4")); //FFFF
            Console.ReadLine();
           
            //Convert ushort array to Float 
            data = new ushort[2] { 19311, 65529 };         
            float[] floatData = new float[data.Length / 2];
            Buffer.BlockCopy(data, 0, floatData, 0, data.Length * 2);
            for (int index = 0; index < floatData.Length; index += 2)
            {
                Console.WriteLine(floatData[index / 2].ToString("0.0000")); //123.4560
                Console.ReadLine();              
            }
            
            //Convert to double
            data = new ushort[4] { 65512, 59784, 64790, 16675 };
            double[] doubleData = new double[2];
            Buffer.BlockCopy(data, 0, doubleData, 0, 8);
            Console.WriteLine(doubleData[0].ToString());


            //Convert ushort value to Int16                         
            ushort ushortValue = 65516 ;
            Int16 int16Value = (Int16)ushortValue;            
            Console.WriteLine(int16Value.ToString()); //-20
            Console.ReadLine();

            //------------------------------------------------------------------------------------------
            
            ushort[] uintData = new ushort[2];  
   
            //Convert Long value to ushort array                             
            long[] longData ;
            longData = new long[1] { -65532 };
            Buffer.BlockCopy(longData, 0, uintData,0 , 4);
            for (int index = 0; index < uintData.Length; index ++)
            {
                //uintData[0] = 4 ;uintData[1] = 65535
                Console.WriteLine(string.Format("uintData[{0}] = {1}", index, uintData[index]));              
            }
            Console.ReadLine();

            //Convert UnsignedLong value to ushort array          
            ulong[] ulongData;
            ulongData = new ulong[1] { 890463152 }; 
            Buffer.BlockCopy(ulongData, 0, uintData, 0, 4);
            for (int index = 0; index < uintData.Length; index++)
            {
                //uintData[0] = 25520;uintData[1] = 13587
                Console.WriteLine(string.Format("uintData[{0}] = {1}", index, uintData[index]));
            }
            Console.ReadLine();

            //Convert Hex to ushort
            string hexValue = "FFEC";     
            Console.WriteLine(Convert.ToInt32(hexValue, 16));  //65516       
            Console.ReadLine();

            //Convert Float to short 
            floatData = new float[1] { 223.4560f };
            Buffer.BlockCopy(floatData, 0, uintData, 0, 4);
            for (int index = 0; index < uintData.Length; index++)
            {
                //uintData[0] = 29884 ;uintData[1] = 17247
                Console.WriteLine(string.Format("uintData[{0}] = {1}", index, uintData[index]));
            }
            Console.ReadLine();
      
            //Convert Int16 value to ushort           
            int16Value = -35;
            ushort uintValue = (ushort)int16Value;
            Console.WriteLine(uintValue.ToString()); //65501
            Console.ReadLine();
            
        }
    }
}

 

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值