zoukankan      html  css  js  c++  java
  • [转]JAVA字节数据与JAVA类型的转换

    本文转自:http://blog.csdn.net/eddle/article/details/6892271

    一、JAVA进制类型转换

    十进制转成十六进制:

    Integer.toHexString(int i)

    十进制转成八进制

    Integer.toOctalString(int i) 

    十进制转成二进制

    Integer.toBinaryString(int i)

    十六进制转成十进制

    Integer.valueOf("FFFF",16).toString()

    八进制转成十进制

    Integer.valueOf("876",8).toString()

    二进制转十进制

    Integer.valueOf("0101",2).toString()

    有什么方法可以直接将2,8,16进制直接转换为10进制的吗?

    java.lang.Integer

    parseInt(String s, int radix)

    使用第二个参数指定的基数,将字符串参数解析为有符号的整数。

    examples from jdk:

    parseInt("0", 10) returns 0

    parseInt("473", 10) returns 473

    parseInt("-0", 10) returns 0

    parseInt("-FF", 16) returns -255

    parseInt("1100110", 2) returns 102

    parseInt("2147483647", 10) returns 2147483647

    parseInt("-2147483648", 10) returns -2147483648

    parseInt("2147483648", 10) throws a NumberFormatException

    parseInt("99", 8) throws a NumberFormatException

    parseInt("Kona", 10) throws a NumberFormatException

    parseInt("Kona", 27) returns 411787

    进制转换如何写(二,八,十六)不用算法

    Integer.toBinaryString

    Integer.toOctalString

    Integer.toHexString

    二、字节数组与JAVA类型转换

    //long类型转成byte数组

      public static byte[] longToByte(long number) {

            long temp = number;

            byte[] b = new byte[8];

            for (int i = 0; i < b.length; i++) {

                b[i] = new Long(temp & 0xff).byteValue();//

    将最低位保存在最低位

                temp = temp >> 8; // 向右移8

            }

            return b;

        }

        //byte数组转成long

        public static long byteToLong(byte[] b) {

            long s = 0;

            long s0 = b[0] & 0xff;// 最低位

            long s1 = b[1] & 0xff;

            long s2 = b[2] & 0xff;

            long s3 = b[3] & 0xff;

            long s4 = b[4] & 0xff;// 最低位

            long s5 = b[5] & 0xff;

            long s6 = b[6] & 0xff;

            long s7 = b[7] & 0xff;

            // s0不变

            s1 <<= 8;

            s2 <<= 16;

            s3 <<= 24;

            s4 <<= 8 * 4;

            s5 <<= 8 * 5;

            s6 <<= 8 * 6;

            s7 <<= 8 * 7;

            s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;

            return s;

        }

    /**

         * 注释:int到字节数组的转换!

         *

         * @param number

         * @return

         */

        public static byte[] intToByte(int number) {

            int temp = number;

            byte[] b = new byte[4];

            for (int i = 0; i < b.length; i++) {

                b[i] = new Integer(temp & 0xff).byteValue();//

    将最低位保存在最低位

                temp = temp >> 8; // 向右移8

            }

            return b;

        }

        /**

         * 注释:字节数组到int的转换!

         *

         * @param b

         * @return

         */

        public static int byteToInt(byte[] b) {

            int s = 0;

            int s0 = b[0] & 0xff;// 最低位

            int s1 = b[1] & 0xff;

            int s2 = b[2] & 0xff;

            int s3 = b[3] & 0xff;

            s3 <<= 24;

            s2 <<= 16;

            s1 <<= 8;

            s = s0 | s1 | s2 | s3;

            return s;

        }

        /**

         * 注释:short到字节数组的转换!

         *

         * @param s

         * @return

         */

        public static byte[] shortToByte(short number) {

            int temp = number;

            byte[] b = new byte[2];

            for (int i = 0; i < b.length; i++) {

                b[i] = new Integer(temp & 0xff).byteValue();//

    将最低位保存在最低位

                temp = temp >> 8; // 向右移8

            }

            return b;

        }

        /**

         * 注释:字节数组到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;

        }

  • 相关阅读:
    tushare学习
    TVP-VAR模型
    时间序列分析
    python tusahre使用
    金融大讲堂-笔记
    多元GARCH模型
    方差与协方差
    代码生成器,项目更新第一版,mybatis-plus
    Springboot手动搭建项目——配置mybatis tk框架
    java基础,集合,HashMap,源码解析
  • 原文地址:https://www.cnblogs.com/freeliver54/p/2219854.html
Copyright © 2011-2022 走看看