`

short,int,long与byte数组之间的转换

    博客分类:
  • java
 
阅读更多

引用 http://www.javaresearch.org/article/83622.htm

 

 

  1. package com.test;
  2. import java.nio.ByteBuffer;
  3. public class ByteUtil {
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main(String[] args) {
  8.         test2();
  9.     }
  10.     public static void test2()
  11.     {
  12.         short s = -20;
  13.         byte[] b = new byte[2];
  14.         putReverseBytesShort(b, s, 0);
  15.         ByteBuffer buf = ByteBuffer.allocate(2);
  16.         buf.put(b);
  17.         buf.flip();
  18.         System.out.println(getReverseBytesShort(b, 0));
  19.         System.out.println(Short.reverseBytes(buf.getShort()));
  20.         System.out.println("***************************");
  21.         int i = -40;
  22.         b = new byte[4];
  23.         putReverseBytesInt(b, i, 0);
  24.         buf = ByteBuffer.allocate(4);
  25.         buf.put(b);
  26.         buf.flip();
  27.         System.out.println(getReverseBytesInt(b, 0));
  28.         System.out.println(Integer.reverseBytes(buf.getInt()));
  29.         System.out.println("***************************");
  30.         long l = -50;
  31.         b = new byte[8];
  32.         putReverseBytesLong(b, l, 0);
  33.         buf = ByteBuffer.allocate(8);
  34.         buf.put(b);
  35.         buf.flip();
  36.         System.out.println(getReverseBytesLong(b, 0));
  37.         System.out.println(Long.reverseBytes(buf.getLong()));
  38.         System.out.println("***************************");
  39.     }
  40.     public static void test1()
  41.     {
  42.         short s = -20;
  43.         byte[] b = new byte[2];
  44.         putShort(b, s, 0);
  45.         ByteBuffer buf = ByteBuffer.allocate(2);
  46.         buf.put(b);
  47.         buf.flip();
  48.         System.out.println(getShort(b, 0));
  49.         System.out.println(buf.getShort());
  50.         System.out.println("***************************");
  51.         int i = -40;
  52.         b = new byte[4];
  53.         putInt(b, i, 0);
  54.         buf = ByteBuffer.allocate(4);
  55.         buf.put(b);
  56.         buf.flip();
  57.         System.out.println(getInt(b, 0));
  58.         System.out.println(buf.getInt());
  59.         System.out.println("***************************");
  60.         long l = -50;
  61.         b = new byte[8];
  62.         putLong(b, l, 0);
  63.         buf = ByteBuffer.allocate(8);
  64.         buf.put(b);
  65.         buf.flip();
  66.         System.out.println(getLong(b, 0));
  67.         System.out.println(buf.getLong());
  68.         System.out.println("***************************");
  69.     }
  70.     public static void putShort(byte b[], short s, int index) {
  71.         b[index] = (byte) (s >> 8);
  72.         b[index + 1] = (byte) (s >> 0);
  73.     }
  74.     public static void putReverseBytesShort(byte b[], short s, int index) {
  75.         b[index] = (byte) (s >> 0);
  76.         b[index + 1] = (byte) (s >> 8);
  77.     }
  78.     public static short getShort(byte[] b, int index) {
  79.         return (short) (((b[index] << 8) | b[index + 1] & 0xff));
  80.     }
  81.     public static short getReverseBytesShort(byte[] b, int index) {
  82.         return (short) (((b[index+1] << 8) | b[index] & 0xff));
  83.     }
  84.     // ///////////////////////////////////////////////////////
  85.     public static void putInt(byte[] bb, int x, int index) {
  86.         bb[index + 0] = (byte) (x >> 24);
  87.         bb[index + 1] = (byte) (x >> 16);
  88.         bb[index + 2] = (byte) (x >> 8);
  89.         bb[index + 3] = (byte) (x >> 0);
  90.     }
  91.     public static void putReverseBytesInt(byte[] bb, int x, int index) {
  92.         bb[index + 3] = (byte) (x >> 24);
  93.         bb[index + 2] = (byte) (x >> 16);
  94.         bb[index + 1] = (byte) (x >> 8);
  95.         bb[index + 0] = (byte) (x >> 0);
  96.     }
  97.     public static int getInt(byte[] bb, int index) {
  98.         return (int) ((((bb[index + 0] & 0xff) << 24)
  99.                 | ((bb[index + 1] & 0xff) << 16)
  100.                 | ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));
  101.     }
  102.     public static int getReverseBytesInt(byte[] bb, int index) {
  103.         return (int) ((((bb[index + 3] & 0xff) << 24)
  104.                 | ((bb[index + 2] & 0xff) << 16)
  105.                 | ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0)));
  106.     }
  107.     // /////////////////////////////////////////////////////////
  108.     public static void putLong(byte[] bb, long x, int index) {
  109.         bb[index + 0] = (byte) (x >> 56);
  110.         bb[index + 1] = (byte) (x >> 48);
  111.         bb[index + 2] = (byte) (x >> 40);
  112.         bb[index + 3] = (byte) (x >> 32);
  113.         bb[index + 4] = (byte) (x >> 24);
  114.         bb[index + 5] = (byte) (x >> 16);
  115.         bb[index + 6] = (byte) (x >> 8);
  116.         bb[index + 7] = (byte) (x >> 0);
  117.     }
  118.     public static void putReverseBytesLong(byte[] bb, long x, int index) {
  119.         bb[index + 7] = (byte) (x >> 56);
  120.         bb[index + 6] = (byte) (x >> 48);
  121.         bb[index + 5] = (byte) (x >> 40);
  122.         bb[index + 4] = (byte) (x >> 32);
  123.         bb[index + 3] = (byte) (x >> 24);
  124.         bb[index + 2] = (byte) (x >> 16);
  125.         bb[index + 1] = (byte) (x >> 8);
  126.         bb[index + 0] = (byte) (x >> 0);
  127.     }
  128.     public static long getLong(byte[] bb, int index) {
  129.         return ((((long) bb[index + 0] & 0xff) << 56)
  130.                 | (((long) bb[index + 1] & 0xff) << 48)
  131.                 | (((long) bb[index + 2] & 0xff) << 40)
  132.                 | (((long) bb[index + 3] & 0xff) << 32)
  133.                 | (((long) bb[index + 4] & 0xff) << 24)
  134.                 | (((long) bb[index + 5] & 0xff) << 16)
  135.                 | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
  136.     }
  137.     public static long getReverseBytesLong(byte[] bb, int index) {
  138.         return ((((long) bb[index + 7] & 0xff) << 56)
  139.                 | (((long) bb[index + 6] & 0xff) << 48)
  140.                 | (((long) bb[index + 5] & 0xff) << 40)
  141.                 | (((long) bb[index + 4] & 0xff) << 32)
  142.                 | (((long) bb[index + 3] & 0xff) << 24)
  143.                 | (((long) bb[index + 2] & 0xff) << 16)
  144.                 | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));
  145.     }

}

分享到:
评论

相关推荐

    java数据类型转byte数组

    ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...

    java实现的字节数组转换成基本类型,基本类型转换成byte[]

    char short int long float double 转换成byte数组

    java byte数组与int,long,short,byte的转换实现方法

    下面小编就为大家带来一篇java byte数组与int,long,short,byte的转换实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    java_android_类型转换工具类的标准代码

    byte数组转换为int 保留几位小数 null转String String转Byte String转Boolean String转Int String转Short String转Double Int转String Double转Long Double转Int Long转Double Long转Int String转Long Long转String

    java 举例分析 equals hashcode 基本类型与基本对象的比较 shot与Short int与Integer long与Long

    举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character

    ByteConvert_arduino:Arduino的库,可将变量转换为字节并返回

    字节转换什么事啊您是否曾经想过通过I2C,SPI,串行或其他协议或总线传输int , short , long , double或任何其他数字类型,但是您已将变量转换为字符串以能够按char进行传输。 该库使您可以将任何数值转换为字节...

    数据类型转换

    float、double等类型与byte数组相互转换,根据数据类型的位数不同,对应的数组大小也不同、例如int32位,4个字节,则需要大小为4的byte数组来进行转换,详情请看代码,里面有转换的实现和详细的注释

    Java中各个数据类型之间的转换

    byte→short(char)→int→long→float→double 也就是说byte类型的变量可以自动转换为short类型,示例代码: byte b = 10; short sh = b; 这里在赋值时,JVM首先将b的值转换为short类型,然后再赋值给sh。 在类型...

    谈谈Java中整数类型(short int long)的存储方式

    在java中的整数类型有四种,分别是byte short in long,本文重点给大家介绍java中的整数类型(short int long),由于byte只是一个字节0或1,在此就不多说了,对java中的整数类型感兴趣的朋友一起学习吧

    java数据类型的转换简单数据类型之间的转换 (2). 字符串与其它数据类型的转换 (3). 其它实用数据类型转换

    一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double型之间的转换,整数和String类型之间的转换,以及处理、显示时间方面的问...byte、short、int、long;浮点型float、double。其

    switch语句能否作用在byte上,能否作用在long上,能否作用在String上

     在switch(expr1)中,expr1只能是一个整数表达式或者枚举常量(更大字体),整数表达式可以是int基本类型或Integer包装类型,由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也...

    C/C++与Java各数据类型所占字节数的详细比较

     [signed] short [int] 2Byte  unsigned short [int] 2 Byte  long [int] 4 Byte  unsigned long [int] 4 Byte Visual C++ 6.0:  [signed] int 4Byte unsigned int 4Byte  [signed] short [in

    C#自动类型转换与强制类型转换的讲解

     byte→short(char)→int→long→float→double 也就是说byte类型的变量可以自动转换为short类型,示例代码: byte b = 10; short sh = b; 在类型转换时可以跳跃。示例代码: byte b1 = 100; int n = b1; ...

    JavaXML解析器FastXml.zip

    当需要把某个属性值或者文本块内容转换为int、short、long时,不需要先把byte数组转化成string,然后再转化成int、short、long。FastXml直接把byte数组转换成对应的类型。 如果xml文档中有些标签内容你并...

    Java数组详解(Java基础)

    (1)基本类型 byte short int long float double char boolean (2)引用类型 类 数组 接口 枚举 注解 变量:内存中的一块存储空间, 存储的就是常量。 特点:一个变量只能存储一个数据,不能存储多个。 需求:...

    对于JPEG图片的数字水印提取程序

    int HufBlock(BYTE dchufindex,BYTE achufindex); int DecodeElement(); BYTE ReadByte(); int WaterMarkExtractDecode(); void IQtIZzMCUComponent(short flag); void IZzMCUComponent(short flag); void ...

    Java基础测试

    byte short int long 的关系

    DELPHI6帮助文件-中英文对照

    基本整数类型包括Shortint、Smallint、Longint、Int64、Byte、Word、Longword等,如下: 类型 范围 格式 Shortint -128..127 含符号的8位 Smallint -32768..32767 含符号的16位 Longint -2147483648...

    linux下 将uyvy格式图片转换成bmp格式

    在linux下,将uyvy格式的图片转换成bmp格式 默认宽度是720 头文件忘记传了:uyvytobmp.h #ifndef __UYVY_TO_BMP__ #define __UYVY_TO_BMP__ typedef unsigned int DWORD; typedef unsigned short int WORD; typedef...

    类型转换java

    键盘输入1个字符串,如果是整数数字串,如”4353”, 分别按八进制和十六进制输出,以及按byte,short,int,long,char输出;每个数占10位长.每行输出3个数; 如果是带小数数字串,如”243.58”,分别按short,int,long,float...

Global site tag (gtag.js) - Google Analytics