import java.nio.ByteBuffer;

public class Program
{
public static void main(String[] args)
{
ByteBuffer buf = ByteBuffer.allocate(3); writeInt24(-113, buf);
buf.flip();
int i1 = readInt24(buf); buf.clear();
writeInt24(9408399, buf);
buf.flip();
int i2 = readUnsigedInt24(buf);//readInt24(buf); System.out.println("i1 = " + i1);
System.out.println("i2 = " + i2);
} static void writeInt24(int val, ByteBuffer buf)
{
buf.put((byte)(val >> 16));
buf.put((byte)(val >> 8));
buf.put((byte)val);
} static int readInt24(ByteBuffer buf)
{
byte[] data = new byte[3];
for (int i = 0; i < 3; ++i)
data[i] = buf.get(); return (data[0] << 16)
| (data[1] << 8 & 0xFF00)
| (data[2] & 0xFF);// Java总是把byte当做有符号处理;我们可以通过将其和0xFF进行二进制与来得到有符号的整型
} static int readUnsigedInt24(ByteBuffer buf)
{
byte[] data = new byte[3];
for (int i = 0; i < 3; ++i)
data[i] = buf.get(); return (data[0] << 16 & 0xFF0000)
| (data[1] << 8 & 0xFF00)
| (data[0] & 0xFF);
}
}

byte数组和int互转的更多相关文章

  1. 【转】java中byte数组与int类型的转换(两种方式)----不错

    原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...

  2. byte数组和int之间相互转化的方法

    Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形in ...

  3. Java 中 byte、byte 数组和 int、long 之间的转换

    Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...

  4. java byte数组与String互转

      java byte数组与String互转 CreationTime--2018年7月6日14点53分 Author:Marydon 1.String-->byte[] 方法:使用String ...

  5. [转载] java中byte数组与int,long,short间的转换

    文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * ...

  6. java byte数组与int,long,short,byte转换

    public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return conver ...

  7. byte数组与int,long,short,byte转换 (转载)

    byte数组和short数组转换 public short bytesToShort(byte[] bytes) { return ByteBuffer.wrap(bytes).order(ByteO ...

  8. byte[] 数组和字符串的转换,与byte[] 数组和int类型的之间的转化

    我们先来看看byte bool  int ushort  等的定义 首先时byte[]数组与string之间的转换 string 转换位byte[] 数组 string str = "1-1 ...

  9. byte[]数组和int之间的转换

    这里简单记录下两种转换方式: 第一种: 1.int与byte[]之间的转换(类似的byte short,long型) /** * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高 ...

随机推荐

  1. java synchronized wait

    在多个线程要互斥访问数据,但线程间需要同步时——例如任务分多个阶段,特定线程负责特定阶段的情况,经常合作使用synchronized 和 wait() /** * * 计算输出其他线程锁计算的数据 * ...

  2. UVA 10041 Vito's Family (中位数)

      Problem C: Vito's family  Background The world-known gangster Vito Deadstone is moving to New York ...

  3. LINUX进程优先级实现

    首先linux进程优先级的范围是-20到19 将当前目录下的documents目录打包,但不希望tar占用太多CPU: nice -19 tar zcf pack.tar.gz documents 这 ...

  4. 【转】cocos2d-x游戏开发(十四)用shader使图片背景透明

    转自:http://blog.csdn.net/dawn_moon/article/details/8631783 好吧,终于抽时间写这篇文章了. 手头上有很多人物行走图,技能特效图等,但这些图都有个 ...

  5. android利用WebView实现浏览器的封装

    android提供了封装浏览器的接口,可以让开发者利用自己的view显示网页内容.今天又实现研究了一下,利用WebView显示浏览器内容,还可以利用 WebViewClient显示自己需要的内容. 参 ...

  6. 负载均衡、LVS概述

    1. 负载均衡概述 负载均衡的基本思路是:在一个服务器集群中尽可能的平衡负载量.通常的做法是在服务器前端设置一个负载均衡器(一般是专门的硬件设备).然后负载均衡器将请求的连接路由到最空闲的可用服务器. ...

  7. Myeclipse最简单的svn插件安装方法

    首先来这儿下载插件 http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 找个最新的下载   解压到对应的位置, ...

  8. 【Linux.Python】Python进程后台启动

    嗯,比较忧伤. 前几天写了个tornado,启动了,很开心,后来每天要用时都发现it是kill掉的.好吧,是我太蠢啦.百度了下资料 python的启动方式: 1 python yourfile.py ...

  9. HDU 4081 Qin Shi Huang's National Road System 最小生成树

    分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...

  10. 单元测试工具之Xunit

    在.NET开发中的单元测试工具之——xUnit.Net  原始出处 http://zhoufoxcn.blog.51cto.com/792419/1172320 在上一篇<在.NET开发中的单元 ...