• 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()的源码。

  • 相关阅读:
    IOS实现自动循环滚动广告--ScrollView的优化和封装
    iOS开发知识点总结
    iOS项目常用效果方法注意点集锦
    项目功能大全,让你的项目一天搞定
    Xcode调试技巧(断点和重构)
    Swift学习笔记-ARC
    常用的属性修饰
    __ block
    小笔记
    java+tomcat安装
  • 原文地址:https://www.cnblogs.com/gqv2009/p/14601163.html
Copyright © 2020-2023  润新知