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运算时自动类型转化问题
-------------------------------------------------------------------------------------------------- ★ ...
随机推荐
- 【Java EE 学习 16 上】【dbcp数据库连接池】【c3p0数据库连接池】
一.回顾之前使用的动态代理的方式实现的数据库连接池: 代码: package day16.utils; import java.io.IOException; import java.lang.ref ...
- ListView没有分割线怎么办?
<ListView android:layout_width="match_parent" android:layout_height="match_parent& ...
- TMS 例子63 分组,子node
procedure TForm1.InitGrid; begin advstringgrid1.Grouping.MergeHeader := true; //这个什么作用没有是 advstringg ...
- 如何在Ubuntu 14.04服务器上自动化部署Spring Boot的应用
https://segmentfault.com/a/1190000003944843
- CodeForces462 A. Appleman and Easy Task
A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Android入门(九):CheckBox多选清单和ScrollView滚动条
字符串资源文件strings.xml: <resources> <string name="hello">主类main</string> < ...
- 【DP】HDU 1114
HDU 1144 Piggy-Bank 题意:有这么个存钱罐,给你空的时候重量和满的时候的重量,再给你N中类型的硬币(给出N种硬币总数量和总重量,可多次使用),问你怎样恰好填满存钱罐,而让填入的硬币数 ...
- 初用idea建立javaweb遇到的问题与心得
1.直接用idea建立的web项目,其自动生成的web.xml里version=3.1,这样的话建立servlet-name等标签会报错(因为3.1不支持这种做法,更提倡用注解的办法),解决办法是将w ...
- Lua字符串库
1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回字符串s重复n次的结 ...
- WPF整理-为User Control添加依赖属性
依赖属性 ".NET properties are nothing more than syntactic sugar over set and get methods." 我们知 ...