参考MSDN

代码:

 public class BytesOperate
{
/// <summary>
/// 计算校验和,SUM
/// </summary>
public byte CalculateCheckSum(byte[] data)
{
int sum = data.Aggregate(, (current, t) => current + t);
return (byte)(sum & 0x00ff);
} public short CombineBytesToShort(byte high, byte low)
{
short value = (short) (high << );
value += low;
return value ;
}
}
 BytesOperate bytesOperate = new BytesOperate();
Assert.AreEqual(, bytesOperate.CombineBytesToShort(0x01, 0x06));
Assert.AreEqual(-, bytesOperate.CombineBytesToShort(0xff, 0xfe));
Assert.AreEqual(-1.6, (double)bytesOperate.CombineBytesToShort(0xff, 0xf0) / );

使用unchecked:

        [TestMethod]
public void SignedTest()
{
int valueInt = 0xfff0;
Console.WriteLine("原始值:"+ valueInt);
Console.WriteLine("原始值十六进制:"+ valueInt.ToString("x"));
byte high = (byte)(valueInt >> );
Console.WriteLine("高位值:"+high);
Console.WriteLine("高位值十六进制:"+high.ToString("x"));
byte low = (byte)valueInt;
Console.WriteLine("低位值:"+low);
Console.WriteLine("低位值十六进制:"+low.ToString("x"));
short valueShort =(short)(high << ); Console.WriteLine("高位左移8:"+valueShort);
Console.WriteLine("高位左移8十六进制:"+valueShort.ToString("X"));
valueShort += low;
Console.WriteLine("加上低位"+valueShort);
Console.WriteLine(valueShort);
Console.WriteLine(valueShort.ToString("X"));
Assert.AreEqual(-,valueShort); unchecked
{
short anotherValue = (short)0xfff0;
Assert.AreEqual(-,anotherValue);
}
}

c# unchecked关键字。byte 合并short的更多相关文章

  1. c# checked unchecked 关键字

    checked 和 unchecked关键字用来限定检查或者不检查数学运算溢出的:如果使用了checked发生数学运算溢出时会抛出OverflowException:如果使用了unchecked则不会 ...

  2. java.lang基础数据类型boolean、char、byte、short、int、long、float、double (JDK1.8)

    java.lang.Boolean public static int hashCode(boolean value) { return value ? 1231 : 1237; } JDK 1.8新 ...

  3. 【C#】数据类型(sbyte,byte,short,ushort,int,uint,long,ulong和char。、、、)

    C#的数据类型可以分为3类:数值类型,引用类型,指针类型.指针类型仅在不安全代码中使用. 值类型包括简单类型(如字符型,浮点型和整数型等),集合类型和结构型.引用类型包括类类型,接口类型,代表类型和数 ...

  4. C# checked和unchecked 关键字详解

    checked 和 unchecked关键字用来限定检查或者不检查数学运算溢出的:如果使用了checked发生数学运算溢出时会抛出OverflowException:如果使用了unchecked则不会 ...

  5. java 基础--8 种基本数据类型:整型、浮点型、布尔型、字符型 整型中 byte、short、int、long 的取值范围 什么是浮点型?什么是单精度和双精度?为什么不能用浮点型表示金额?

     一.8种基本数据类型(4整,2浮,1符,1布): 整型:byte(最小的数据类型).short(短整型).int(整型).long(长整型): 浮点型:float(浮点型).double(双精度浮点 ...

  6. Java7中Switch为什么只支持byte、short、char、int、String

    Java 7中,switch的参数可以是String类型了,这对我们来说是一个很方便的改进.到目前为止switch支持这样几种数据类型:byte short int char String .但是,作 ...

  7. Java中int类型和tyte[]之间转换及byte[]合并

    JAVA基于位移的 int类型和tyte[]之间转换 [java] view plaincopy /** * 基于位移的int转化成byte[] * @param int number * @retu ...

  8. java音视频编解码问题:16/24/32位位音频byte[]转换为小端序short[],int[],以byte[]转short[]为例

    前言:Java默认采用大端序存储方式,实际编码的音频数据是小端序,如果处理单8bit的音频当然不需要做转换,但是如果是16bit或者以上的就需要处理成小端序字节顺序. 注:大.小端序指的是字节的存储顺 ...

  9. Java中byte、short、char、int、long运算时自动类型转化问题

    -------------------------------------------------------------------------------------------------- ★ ...

随机推荐

  1. 哈,我自己翻译的小书,马上就完成了,是讲用python处理大数据框架hadoop,spark的

    花了一些时间, 但感觉很值得. Big Data, MapReduce, Hadoop, and Spark with Python Master Big Data Analytics and Dat ...

  2. POJ2104 K-th Number(归并树)

    平方分割一直TLE,最后用归并树处理过了,使用STL会比较慢. #include<cstdio> #include<iostream> #include<cstdlib& ...

  3. 8-04流程控制语句BEGIN ..END

     流程控制语句: 是用来控制程序流程的语句. 常用的流程控制语句的分类: 顺序结构:BEGIN...END 分支结构: IF ..ELSE 或CASE ..END 循环结构:WHILE 顺序结构 语法 ...

  4. ppt - 常规策划

    1 比较图2 progress3 目标 proposal4 market leadership5 分析 - 设计 - 开发 - 实施 - 评估6 innovation7 时间区间表述8 阶梯式9 主 ...

  5. Could not load file or assembly 'System.ServiceModel.DomainServices.Hosting'.系统找不到指定文件

    项目部署到服务器后出现如下错误信息: Parser Error Message: Could not load file or assembly 'System.ServiceModel.Domain ...

  6. supervisor:How is this different from daemontools ?

    "daemontools has too much focus on security as opposed to being a process manager for my taste. ...

  7. Shell 编程基础之 [ 与 [[ 的异同

    一.简介 [ 与 test 等价,是 bash 的内部命令,GNU/linux 系统的 coreutils 软件包通常带 /usr/bin/test 和 /usr/bin/[ 命令.如果我们不用绝对路 ...

  8. Xcode8 更新后的坑及常见错误

    1.Xcode更新后,command + / 注释整行代码不起作用了,可以终端运行sudo  /usr/libexec/xpccachectl,并重启电脑 可以参考http://blog.csdn.n ...

  9. c++利用循环数组建立FIFO模板队列

    可直接编译运行,其中status()方法效果如图: #include <iostream> using std::cout; template<typename T> clas ...

  10. shell 套用expect

    先用if 判断expect有没有安装 没有就yum install expect #!/bin/bash passwd='123456' /usr/bin/expect <<-EOF se ...