欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談java二進(jìn)制、十進(jìn)制、十六進(jìn)制、字符串之間的相互轉(zhuǎn)換

 更新時間:2020年11月09日 17:55:30   投稿:jingxian  
下面小編就為大家?guī)硪黄獪\談二進(jìn)制、十進(jìn)制、十六進(jìn)制、字符串之間的相互轉(zhuǎn)換。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考,一起跟隨小編過來看看吧

1. 字節(jié)轉(zhuǎn)10進(jìn)制

直接使用(int)類型轉(zhuǎn)換。

/*
  * 字節(jié)轉(zhuǎn)10進(jìn)制
  */
 public static int byte2Int(byte b){
  int r = (int) b;
  return r;
 }

2. 10進(jìn)制轉(zhuǎn)字節(jié)

直接使用(byte)類型轉(zhuǎn)換。

/*
  * 10進(jìn)制轉(zhuǎn)字節(jié)
  */
 public static byte int2Byte(int i){
  byte r = (byte) i;
  return r;
 }

3. 字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串

對每一個字節(jié),先和0xFF做與運算,然后使用Integer.toHexString()函數(shù),如果結(jié)果只有1位,需要在前面加0。

/*
  * 字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串
  */
 public static String bytes2HexString(byte[] b) {
  String r = "";
  
  for (int i = 0; i < b.length; i++) {
   String hex = Integer.toHexString(b[i] & 0xFF);
   if (hex.length() == 1) {
    hex = '0' + hex;
   }
   r += hex.toUpperCase();
  }
  
  return r;
 }

4. 16進(jìn)制字符串轉(zhuǎn)字節(jié)數(shù)組

這個比較復(fù)雜,每一個16進(jìn)制字符是4bit,一個字節(jié)是8bit,所以兩個16進(jìn)制字符轉(zhuǎn)換成1個字節(jié),對于第1個字符,轉(zhuǎn)換成byte以后左移4位,然后和第2個字符的byte做或運算,這樣就把兩個字符轉(zhuǎn)換為1個字節(jié)。

/*
  * 字符轉(zhuǎn)換為字節(jié)
  */
 private static byte charToByte(char c) {
  return (byte) "0123456789ABCDEF".indexOf(c);
  }
 
 /*
  * 16進(jìn)制字符串轉(zhuǎn)字節(jié)數(shù)組
  */
 public static byte[] hexString2Bytes(String hex) {
   
   if ((hex == null) || (hex.equals(""))){
    return null;
   }
   else if (hex.length()%2 != 0){
    return null;
   }
   else{    
    hex = hex.toUpperCase();
    int len = hex.length()/2;
    byte[] b = new byte[len];
    char[] hc = hex.toCharArray();
    for (int i=0; i<len; i++){
     int p=2*i;
     b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1]));
    }
    return b;
   }   
   
 }

5. 字節(jié)數(shù)組轉(zhuǎn)字符串

直接使用new String()。

/*
  * 字節(jié)數(shù)組轉(zhuǎn)字符串
  */
 public static String bytes2String(byte[] b) throws Exception {
  String r = new String (b,"UTF-8");  
  return r;
 }

6. 字符串轉(zhuǎn)字節(jié)數(shù)組

直接使用getBytes()。

/*
  * 字符串轉(zhuǎn)字節(jié)數(shù)組
  */
 public static byte[] string2Bytes(String s){
  byte[] r = s.getBytes();
  return r;
 }

7. 16進(jìn)制字符串轉(zhuǎn)字符串

先轉(zhuǎn)換成byte[],再轉(zhuǎn)換成字符串。

/*
  * 16進(jìn)制字符串轉(zhuǎn)字符串
  */
 public static String hex2String(String hex) throws Exception{
  String r = bytes2String(hexString2Bytes(hex));  
  return r;
 }

8. 字符串轉(zhuǎn)16進(jìn)制字符串

先轉(zhuǎn)換為byte[],再轉(zhuǎn)換為16進(jìn)制字符串。

/*
  * 字符串轉(zhuǎn)16進(jìn)制字符串
  */
 public static String string2HexString(String s) throws Exception{
  String r = bytes2HexString(string2Bytes(s));  
  return r;
 }

main函數(shù):

public static void main(String[] args) throws Exception{  
  byte b1 = (byte) 45;
  System.out.println("1.字節(jié)轉(zhuǎn)10進(jìn)制:" + byte2Int(b1));
  
  int i = 89;
  System.out.println("2.10進(jìn)制轉(zhuǎn)字節(jié):" + int2Byte(i));
  
  byte[] b2 = new byte[]{(byte)0xFF, (byte)0x5F, (byte)0x6, (byte)0x5A};
  System.out.println("3.字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串:" + bytes2HexString(b2));
  
  String s1 = new String("1DA47C");
  System.out.println("4.16進(jìn)制字符串轉(zhuǎn)字節(jié)數(shù)組:" + Arrays.toString(hexString2Bytes(s1)));
  
  System.out.println("5.字節(jié)數(shù)組轉(zhuǎn)字符串:" + bytes2String(b2));
  
  System.out.println("6.字符串轉(zhuǎn)字節(jié)數(shù)組:" + Arrays.toString(string2Bytes(s1)));
  
  System.out.println("7.16進(jìn)制字符串轉(zhuǎn)字符串:" + hex2String(s1));
  
  String s2 = new String("Hello!");
  System.out.println("8.字符串轉(zhuǎn)16進(jìn)制字符串:" + string2HexString(s2));
 }

運行結(jié)果:

1.字節(jié)轉(zhuǎn)10進(jìn)制:45
2.10進(jìn)制轉(zhuǎn)字節(jié):89
3.字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串:FF5F065A
4.16進(jìn)制字符串轉(zhuǎn)字節(jié)數(shù)組:[29, -92, 124]
5.字節(jié)數(shù)組轉(zhuǎn)字符串:?_Z
6.字符串轉(zhuǎn)字節(jié)數(shù)組:[49, 68, 65, 52, 55, 67]
7.16進(jìn)制字符串轉(zhuǎn)字符串:?|
8.字符串轉(zhuǎn)16進(jìn)制字符串:48656C6C6F21

以上這篇淺談二進(jìn)制、十進(jìn)制、十六進(jìn)制、字符串之間的相互轉(zhuǎn)換就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringMVC返回的ResponseEntity出現(xiàn)亂碼及解決

    SpringMVC返回的ResponseEntity出現(xiàn)亂碼及解決

    這篇文章主要介紹了SpringMVC返回的ResponseEntity出現(xiàn)亂碼及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 鎖超時發(fā)現(xiàn)parallelStream并行流線程上下文坑解決

    鎖超時發(fā)現(xiàn)parallelStream并行流線程上下文坑解決

    這篇文章主要為大家介紹了鎖超時發(fā)現(xiàn)parallelStream并行流線程上下文坑解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 微服務(wù)?Spring?Boot?整合?Redis?BitMap?實現(xiàn)?簽到與統(tǒng)計功能

    微服務(wù)?Spring?Boot?整合?Redis?BitMap?實現(xiàn)?簽到與統(tǒng)計功能

    這篇文章主要介紹了微服務(wù)?Spring?Boot?整合?Redis?BitMap?實現(xiàn)?簽到與統(tǒng)計功能,文章簡單介紹了Redis BitMap 基本用法結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • Java Socket編程實例(二)- UDP基本使用

    Java Socket編程實例(二)- UDP基本使用

    這篇文章主要講解Java Socket編程中UDP的基本使用,希望能給大家做一個參考。
    2016-06-06
  • Java實現(xiàn)驗證碼具體代碼(圖片、漢字)

    Java實現(xiàn)驗證碼具體代碼(圖片、漢字)

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)驗證碼具體代碼,包括圖片驗證碼、漢字驗證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Java創(chuàng)建線程的方式解析

    Java創(chuàng)建線程的方式解析

    這篇文章主要介紹了Java創(chuàng)建線程的方式解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助
    2022-07-07
  • Mybatis超詳細(xì)講解構(gòu)建SQL方法

    Mybatis超詳細(xì)講解構(gòu)建SQL方法

    這篇文章主要為大家詳細(xì)介紹了Mybatis構(gòu)建SQL,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-07-07
  • 使用eclipse快速新建spirngboot項目的方法

    使用eclipse快速新建spirngboot項目的方法

    本篇文章主要介紹了使用eclipse快速新建spirngboot項目的方法,具有一定的參考價值,有興趣的可以了解一下
    2017-04-04
  • 學(xué)Java做項目需要學(xué)習(xí)的一些技能

    學(xué)Java做項目需要學(xué)習(xí)的一些技能

    這篇文章主要介紹了學(xué)Java做項目需要學(xué)習(xí)的一些技能,例如JavaSE、Servlet、JSP等,總結(jié)了他們中需要學(xué)習(xí)的東西,都是一些經(jīng)驗總結(jié),需要的朋友可以參考下
    2014-07-07
  • 如何設(shè)置springboot啟動端口

    如何設(shè)置springboot啟動端口

    spring boot是個好東西,可以不用容器直接在main方法中啟動,而且無需配置文件,方便快速搭建環(huán)境。下面給大家介紹springboot啟動端口的設(shè)置方法和spring boot創(chuàng)建應(yīng)用端口沖突8080 問題,感興趣的朋友一起看看吧
    2017-08-08

最新評論