zoukankan      html  css  js  c++  java
  • javase整数与字节数组转换+整数转为16进制

    1、 将整数转换成字节数组及 把字节数组转成整数

    2、把int形转换为16进制的存储形式


     

     1 import org.junit.Test;
     2 
     3 public class Converter {
     4     
     5     @Test
     6     public void test3(){
     7         int i = -15 ; 
     8         System.out.println(toHexString(i));
     9     }
    10     
    11     @Test
    12     public void test2(){
    13         byte b = (byte)0xff ;
    14         System.out.println(b);
    15     }
    16     
    17     @Test
    18     public void test1(){
    19         int  i = -1 ;
    20         byte[] arr = int2ByteArray(i);
    21         outArr(arr);
    22         System.out.println(byteArr2Int(arr));
    23         
    24     }
    25     /**
    26      * 将整数转换成字节数组
    27      */
    28     public byte[] int2ByteArray(int i){
    29         byte[] arr = new byte[4];
    30         arr[0] = (byte)(i >> 24);
    31         arr[1] = (byte)(i >> 16);
    32         arr[2] = (byte)(i >> 8);
    33         arr[3] = (byte)i;
    34         return arr ;
    35     }
    36     
    37     /**
    38      * 将字节数组转成整数
    39      */
    40      //第一种方法
    41     public int byteArr2Int(byte[] arr){
    42         int i0 = arr[0] << 24 ;
    43         int i1 = (arr[1] << 16) & 0x00ffffff ;
    44         int i2 = (arr[2] << 8) & 0x0000ffff ;
    45         int i3 = arr[3] & 0x000000ff ;
    46         return i0 | i1 | i2 | i3 ;
    47     }
    48     
    49     /**
    50      * 
    51      */
    52      //第二种方法
    53     public int byteArr2Int0(byte[] arr){
    54         int i0 = (arr[0] & 0xff) << 24 ;
    55         int i1 = (arr[1] & 0xff) << 16 ;
    56         int i2 = (arr[2] & 0xff) << 8 ;
    57         int i3 = arr[3] & 0xff ;
    58         return i0 | i1 | i2 | i3 ;
    59     }
    60     
    61     /**
    62      * 输出数组
    63      */
    64     private void outArr(byte[] arr){
    65         for(int i = 0 ; i < arr.length ; i ++){
    66             System.out.print(arr[i] + " ");
    67         }
    68         System.out.println();
    69     }
    70     
    71     /**
    72      * 把int形转换为16进制的存储形式
    73      */
    74     public String toHexString(int x){
    75         char[] arr = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'} ;
    76         StringBuilder builder = new StringBuilder();
    77         int y = x ;
    78         if(x < 0 ){
    79             y = -x ;
    80             builder.append("-0x");
    81         }
    82         else{
    83             builder.append("0x");
    84         }
    85         for(int i = 7 ; i >= 0 ; i -- ){
    86             int temp = (y >> (i * 4) & 0xf);//0-15,把移位后前面无用的的二进制数都转为0.
    87             builder.append(arr[temp]);
    88         }
    89         return builder.toString();
    90     }
    91 }
  • 相关阅读:
    有用的Python模块
    Python中for循环搭配else的陷阱
    MySQL实用操作
    Pycharm常用快捷键
    MySQL基础
    HTML基础
    MySQL基础
    HTTP连接管理
    TCP连接的建立和终止
    TCP数据流
  • 原文地址:https://www.cnblogs.com/yihaifutai/p/6704946.html
Copyright © 2011-2022 走看看