package sort.bing.com;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;

public class ByteUtils {

public static byte[] int2byte(int res) {
byte[] targets = new byte[4];

targets[0] = (byte) (res & 0xff);// 最低位
targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
return targets;
}

public static int byte2int(byte[] res) {
// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000

int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) // | 表示安位或
| ((res[2] << 24) >>> 8) | (res[3] << 24);
return targets;
}

public static byte[] intToByteArray(int i) throws Exception {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(buf);
System.out.println("i:" + i);
out.writeInt(i);
byte[] b = buf.toByteArray();
System.out.println("i:" + b);
out.close();
buf.close();
return b;
}

/**
* 将16位的short转换成byte数组
*
* @param s
* short
* @return byte[] 长度为2
* */
public static byte[] shortToByteArray(short s) {
byte[] targets = new byte[2];
for (int i = 0; i < 2; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
}

/**
* 注释:字节数组到short的转换!
*
* @param b
* @return
*/
public static short byteToShort(byte[] b) {
short s = 0;
short s0 = (short) (b[0] & 0xff);// 最低位
short s1 = (short) (b[1] & 0xff);
s1 <<= 8;
s = (short) (s0 | s1);
return s;
}

/**
* 把byte[]转换成16进制进制字符串
* @param b
* @return
*/
public static String bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[ i ] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}

/**
* byte[]转换成bit
* @param b
* @return
*/
public static String bytesToBits(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for(byte b:bytes){
sb.append(byteToBits(b));
}
return sb.toString();
}

/**
* byte转换成8位bit
* @param b
* @return
*/
public static String byteToBits(byte b) {
int z = b; z |= 256;
String str = Integer.toBinaryString(z);
int len = str.length();
return str.substring(len-8, len);
}

/**
* 计算校验和
* @param bytes
* @return
*/
public static final int calculateCheckSum(byte[] bytes) {
int sum = 0;
for (byte b : bytes) {
sum += (short)b;
}
return sum>65535 ? (sum-65535) : sum;
}

public static void main(String[] args) throws UnsupportedEncodingException {
// String s = "12" ;
// short n = 100;
// byte[] buf = s.getBytes("UTF-8");
// byte[] buf2 = ByteUtils.shortToByteArray(n);
// System.out.println(calculateCheckSum(buf));
// System.out.println(calculateCheckSum(buf2));
// System.out.println(bytes2HexString(buf2));
byte i = -112;
System.out.println(i & 0xff);
}
}

ByteUtils的更多相关文章

  1. RPC之——HTTP协议栈

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/52531185 今天,给大家带来一篇稍有深度的文章——<RPC之——HTTP协 ...

  2. 怎样让你的代码更好的被JVM JIT Inlining

    好书推荐:Effective Java中文版(第2版) JVM JIT编译器优化技术有近100中,其中最最重要的方式就是内联(inlining).方法内联可以省掉方法栈帧的创建,方法内联还使让JIT编 ...

  3. JVM类加载以及执行的实战

    前几篇文章主要是去理解JVM类加载的原理和应用,这一回讲一个可以自己动手的例子,希望能从头到尾的理解类加载以及执行的整个过程. 这个例子是从周志明的著作<深入理解Java虚拟机>第9章里抄 ...

  4. 高CPU业务场景下的任务分发方案Gearman搭建一览

      Gearman是当年LiveJournal用来做图片resize的,大家也明白图片resize是一个高CPU的操作,如果让web网站去做这个高CPU的功能,有可能会拖垮你的 web应用,那本篇我们 ...

  5. 安卓版的pvr图片查看

    public class PVRTDecompress { /* author:FormatFa mail :1758759399@qq.com date :2017-6-14 */ //modify ...

  6. shiro + jwt 实现 请求头中的 rememberMe 时间限制功能

    前言: 上一篇提出, 通过修改 rememberMe 的编码来实现 rememberMe的功能的设想, 事后我去尝试实现了一番, 发现太麻烦, 还是不要那么做吧. 程序还是要越简单越好. 那功能总是要 ...

  7. java 手动实现远程执行功能(深入理解java虚拟机)

     1.功能类 功能类共有五,分别是: package org.jvm; import java.io.*; /** * 对字节数组操作的工具类 */ public class ByteUtils { ...

  8. 【JVM.8】类加载及执行子系统的案例与实战

    一. 案例分析 1. Tomcat:正统的类加载器架构 主流的Java Web服务器,如Tomcat.Jetty.WebLogic.WebSphere或其他服务器,都实现了自己定义的类加载器(一般都不 ...

  9. 高性能NIO框架Netty-对象传输

    http://cxytiandi.com/blog/detail/17403 上篇文章高性能NIO框架Netty入门篇我们对Netty做了一个简单的介绍,并且写了一个入门的Demo,客户端往服务端发送 ...

随机推荐

  1. [Poi] Setup PostCSS and Tailwind with Poi

    This lesson walks through setting up a Poi project using PostCSS and the popular Tailwind library fo ...

  2. poj2002 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 17666   Accepted: 6735 Descript ...

  3. STL使用————SET&MULTISET

    SET函数的基本用法 by hhl 使用set的好处 1. 当增加元素后,集合会自动删重并从小到大排列(时间比快排还快)2. 相当于一棵伸展树(能快速求出后继) 使用基础 #include<se ...

  4. 1.windows(64位)下使用curl命令

    转自:https://www.cnblogs.com/xing901022/p/4652624.html Curl命令可以通过命令行的方式,执行Http请求.在Elasticsearch中有使用的场景 ...

  5. python 写了一个批量拉取文件进excel文档

    路径如: C:\\Users\\huaqi\\Desktop\\信息收集 “信息收集”目录下有以下子目录:[技术,客服,运营,行政] “技术”目录下有以下子文件:[小白.txt,小红.txt,小黑.t ...

  6. 异常:error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

    error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System. ...

  7. cf 865 B. Ordering Pizza

    B. Ordering Pizza It's another Start[c]up finals, and that means there is pizza to order for the ons ...

  8. 在AT151上面测试串口通讯

    如下图所示,分别用putty打开两个窗口,一个是串口打开的,另外一个是网口连接的,分别是接收和发送,硬件上面RXD和TXD进行短接进行数据回流. 使用microcom工具,ctrl+x可以退出 参考文 ...

  9. H+后台主题UI框架---整理

    本篇文章是对H+这种框架进行整理,顺便了解一下标准的代码规范的写法. 一.表单: 1).下面是一个基本表单: 现在来看这个表单的结构: 1.整个表单的外框结构是一个div,至于padding和marg ...

  10. Gonet2 游戏server框架解析之Agent(3)

    客户端消息在Agent中的预处理流程. Agent定义好的三种请求: //api.go var RCode = map[int16]string{ 0: "heart_beat_req&qu ...