zoukankan      html  css  js  c++  java
  • java byte[]与十六进制字符串相互转换

    案例1

    java byte[]与十六进制字符串相互转换

    import java.util.Arrays;
    
    public class ccc {
    
        public static void main(String[] args) {
            int[] array ={-6, 1, 18, 114, 54, 0, -11, 16, 5, 3, -23, -116, -13, -24, 121, 36};
            System.out.println(Arrays.toString(array));
        }
    }
    
    
    String a = "你好";
    System.out.println(Arrays.toString(a.getBytes(StandardCharsets.UTF_8)));
    byte[] bytes = {-28, -67, -96, -27, -91, -67};
    System.out.println(new String(bytes,StandardCharsets.UTF_8));
    

    运行结果:
    -28,-67,-96,-27,-91,-67,
    你好

    案例2

    
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.charset.Charset;
    
    public class mytest {
    
    	public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    byte[] bytes = new byte[]{-76, -1, 32, 30, 36};
    
    char[] chars = getChars(bytes);
    
    byte[] bytes2= getBytes(chars);
    
    char[] chars2 = getChars(bytes2);
    
    }
    
    	/** char转byte */
    	public static byte[] getBytes(char[] chars) {
    		
    		
    		
    		 int len = chars.length;
    		 byte[] bytes = new byte[len];
    		
    		 for(int i=0;i<len;i++){
    		 bytes[i]= (byte)(chars[i]);
    		 }
    		 return bytes;
    	}
    
    	/** byte转char */
    	public static char[] getChars(byte[] bytes) {
    
    		 int len = bytes.length;
    		 char[] chars = new char[len];
    		
    		 for(int i=0;i<len;i++){
    		 chars[i]= (char)(bytes[i] & 0xff);
    		 }
    		 return chars;
    
    	}
    }
    
    

    案例3

    package com.anjz.test;
     
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
     
    public class ByteArrayTest {
    	public static void main(String[] args) throws IOException {
    		String str = "你好";
    		ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes("utf-8"));
    		
    		byte[] bytes = new byte[str.getBytes("utf-8").length];
    		bis.read(bytes);
    		for(byte b :bytes){
    			System.out.print(b+",");
    		}
    	}
    }
    
    

    运行结果:
    -28,-67,-96,-27,-91,-67,

    案例4

    package com.anjz.test;
     
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
     
    public class ByteArrayTest {
    	public static void main(String[] args) throws IOException {
    		String str = "你好";
    		ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes("utf-8"));
    		
    		int temp = 0;
    		while((temp = bis.read())!=-1){
    			System.out.print(temp+",");
    		}
    	}
    }
    

    运行结果:
    228,189,160,229,165,189,

    案例3 与 案例4
    实验一中直接输出的byte数据,实验二直接输出的是int数据,但两个数据是不一样的,我们把两个结果的数据放到一块。

    -28,-67,-96,-27,-91,-67,

    228,189,160,229,165,189,

    发现一个规律:每列数据的绝对值加一起是个固定值256,这是一个巧合,还是一个规律?关于这个问题,首先我们看一下bis.read()的源码。

  • 相关阅读:
    Maven基本操作命令
    Android studio安装与配置
    java数据结构----图
    java数据结构----堆
    java数据结构----哈希表
    java数据结构----树
    java数据结构----链表
    java数据结构----队列,优先级队列
    java数据结构----栈
    java数据结构----数组篇
  • 原文地址:https://www.cnblogs.com/gqv2009/p/14601163.html
Copyright © 2011-2022 走看看