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. QxtFlowView(libqxt)

    https://github.com/mnutt/libqxt/tree/master/examples http://libqxt.bitbucket.org/doc/0.5/class_qxt_f ...

  2. 概率图模型之有向图与无向图之间的关系 I map D map perfect map(完美图) 概念

    我们已经讨论了有向图和无向图框架下的概率模型,那么我们有必要讨论一下它们二者的关系.

  3. linux切换用户

    当前使用root账户 [root@localhost chucklu]# cd[root@localhost ~]# pwd/root 切换到普通账户 [root@localhost ~]# su c ...

  4. Factorial

    Factorial  计算阶乘 In mathematics, the factorial of a non-negative integer n, denoted by n!, is the pro ...

  5. html5自带表单验证-美化改造

    神奇的代码 暂且叫做html5.css /* === HTML5 validation styles === */ .myform select:required, .myform input:req ...

  6. 结构体 lock_sys

    typedef struct lock_sys_struct lock_sys_t; extern lock_sys_t* lock_sys; struct lock_sys_struct{ hash ...

  7. .woff 文件404,配置到web.config

    <staticContent>        <remove fileExtension=".woff" />        <mimeMap fil ...

  8. bzoj1293

    简易贪心+heap 注意要用链表 type link=^node;      node=record        loc:longint;        next:link;      end;   ...

  9. lnmp 安装环境之后discuz论坛排版乱的问题

    服务器系统类型:centos 6.5 环境:使用lnmp官方安装shell安装 (http://lnmp.org/install.html) 在部署dz之后,访问页面 出现排版乱,资源不能加载的问题: ...

  10. poj2752 水题

    又2b了一次…… var s:ansistring; ans,pre:..] of longint; i,k,tot:longint; procedure main; begin pre[]:=;k: ...