package com.linghushaoxia.javacv11;
import java.nio.ByteBuffer;
/**@功能说明:
* @author:linghushaoxia
* @time:2015年11月27日上午9:43:03
* @version:1.0
*
*/
public class ByteUtil {
private static ByteBuffer buffer = ByteBuffer.allocate(8);
public static void main(String[] args) {
//测试 int 转 byte
int int0 = 234;
byte byte0 = intToByte(int0);
System.out.println("byte0=" + byte0);//byte0=-22
//测试 byte 转 int
int int1 = byteToInt(byte0);
System.out.println("int1=" + int1);//int1=234
//测试 int 转 byte 数组
int int2 = 1417;
byte[] bytesInt = intToByteArray(int2);
System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced
//测试 byte 数组转 int
int int3 = byteArrayToInt(bytesInt);
System.out.println("int3=" + int3);//int3=1417
//测试 long 转 byte 数组
long long1 = 2223;
byte[] bytesLong = longToBytes(long1);
System.out.println("bytes=" + bytesLong);//bytes=[B@c17164
//测试 byte 数组 转 long
long long2 = bytesToLong(bytesLong);
System.out.println("long2=" + long2);//long2=2223
}
//byte 与 int 的相互转换
public static byte intToByte(int x) {
return (byte) x;
}
public static int byteToInt(byte b) {
//Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
return b & 0xFF;
}
//byte 数组与 int 的相互转换
public static int byteArrayToInt(byte[] b) {
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}
public static byte[] intToByteArray(int a) {
return new byte[] {
(byte) ((a >> 24) & 0xFF),
(byte) ((a >> 16) & 0xFF),
(byte) ((a >> 8) & 0xFF),
(byte) (a & 0xFF)
};
}
//byte 数组与 long 的相互转换
public static byte[] longToBytes(long x) {
buffer.putLong(0, x);
return buffer.array();
}
public static long bytesToLong(byte[] bytes) {
buffer.put(bytes, 0, bytes.length);
buffer.flip();//need flip
return buffer.getLong();
}
}
javacv的demo
2星 需积分: 24 83 浏览量
2018-10-29
09:32:30
上传
评论 1
收藏 99KB ZIP 举报
邵婧萱(金生水起万缘和合)
- 粉丝: 90
- 资源: 27
最新资源
- 基于STM32使用HAL库实现USB组合设备之多路CDC源码+说明文档.zip
- 金融贸易项目springboot
- mybatis动态sqlSQL 映射 XML 文件是所有 sql 语句
- 基于基于STM32的智能家居系统源码+qt上位机源码.zip
- 深圳房地产资源数据报告
- 基于stm32的智能门禁系统源码+设计文档+演示视频.zip
- cef + chromium 完整源码支持h265和h264
- 基于SpringBoot的API管理平台源代码+数据库,以项目的形式管理API文档,可以进行API的编辑、测试、Mock等操作
- protobuf 3.11版本,静态编译
- 2023NOC创客智慧编程赛项真题图形化-选拔赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈