c# unchecked关键字。byte 合并short
代码:
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的更多相关文章
- c# checked unchecked 关键字
checked 和 unchecked关键字用来限定检查或者不检查数学运算溢出的:如果使用了checked发生数学运算溢出时会抛出OverflowException:如果使用了unchecked则不会 ...
- 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新 ...
- 【C#】数据类型(sbyte,byte,short,ushort,int,uint,long,ulong和char。、、、)
C#的数据类型可以分为3类:数值类型,引用类型,指针类型.指针类型仅在不安全代码中使用. 值类型包括简单类型(如字符型,浮点型和整数型等),集合类型和结构型.引用类型包括类类型,接口类型,代表类型和数 ...
- C# checked和unchecked 关键字详解
checked 和 unchecked关键字用来限定检查或者不检查数学运算溢出的:如果使用了checked发生数学运算溢出时会抛出OverflowException:如果使用了unchecked则不会 ...
- java 基础--8 种基本数据类型:整型、浮点型、布尔型、字符型 整型中 byte、short、int、long 的取值范围 什么是浮点型?什么是单精度和双精度?为什么不能用浮点型表示金额?
一.8种基本数据类型(4整,2浮,1符,1布): 整型:byte(最小的数据类型).short(短整型).int(整型).long(长整型): 浮点型:float(浮点型).double(双精度浮点 ...
- Java7中Switch为什么只支持byte、short、char、int、String
Java 7中,switch的参数可以是String类型了,这对我们来说是一个很方便的改进.到目前为止switch支持这样几种数据类型:byte short int char String .但是,作 ...
- Java中int类型和tyte[]之间转换及byte[]合并
JAVA基于位移的 int类型和tyte[]之间转换 [java] view plaincopy /** * 基于位移的int转化成byte[] * @param int number * @retu ...
- java音视频编解码问题:16/24/32位位音频byte[]转换为小端序short[],int[],以byte[]转short[]为例
前言:Java默认采用大端序存储方式,实际编码的音频数据是小端序,如果处理单8bit的音频当然不需要做转换,但是如果是16bit或者以上的就需要处理成小端序字节顺序. 注:大.小端序指的是字节的存储顺 ...
- Java中byte、short、char、int、long运算时自动类型转化问题
-------------------------------------------------------------------------------------------------- ★ ...
随机推荐
- Vitamio
前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的And ...
- MySQL - 问题集 - 触发器更新本表数据异常"Can’t update table ‘tbl’ in stored function/trigger because it is already used by statement which invoked this"
如果你在触发器里面对刚刚插入的数据进行了 insert/update, 则出现这个问题.因为会造成循环的调用. create trigger test before update on test fo ...
- hdu1561 The more, The Better (树形dp+背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...
- zookeeper原理及作用
ZooKeeper是Hadoop Ecosystem中非常重要的组件,它的主要功能是为分布式系统提供一致性协调(Coordination)服务,与之对应的Google的类似服务叫Chubby.今天这篇 ...
- Daily Scrum Meeting ——SixthDay
一.Daily Scrum Meeting照片 佳恺请假了...可能去约会了罢 二.Burndown Chart 欣慰,但是还是感到"鸭梨山大"! 三.项目进展 1.活动列表查询功 ...
- Delphi中滚动文字的应用
1.添加一个Timer控件,Interval属性设置为20. 2.添加一个Label控件,Name为labMessage. 3.在Timer的OnTimer事件添加如下代码: procedure TF ...
- 清华学堂 列车调度(Train)
列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In ...
- 字体在网页中画ICON图标
用字体在网页中画ICON图标有三种小技巧: 1.用CSS Sprite在网页中画小图标 实现方法: 首先将小图片整合到一张大的图片上 然后根据具体图标在大图上的位置,给背景定位.background- ...
- Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- pythonchallenge 解谜
所有代码均使用python 3.5.1 版本 最近在学python,闲来无事觉得这个解谜还挺有意思. 解谜网址 http://www.pythonchallenge.com/ 接下来会写破解教程~