国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

JAVA 十六進制與字符串的轉(zhuǎn)換淺談

這篇具有很好參考價值的文章主要介紹了JAVA 十六進制與字符串的轉(zhuǎn)換淺談。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

java16進制轉(zhuǎn)字符串,編程語言,java,java,開發(fā)語言,算法

?

筆者前幾日在開服過程中需要將字符串轉(zhuǎn)化成為16進制的字符串,在網(wǎng)上找到了一些方法嘗試之后,均發(fā)現(xiàn)存在一個問題-->字符串轉(zhuǎn)為16進制后再轉(zhuǎn)回來,英文正常,中文出現(xiàn)亂碼

筆者前幾日在開服過程中需要將字符串轉(zhuǎn)化成為16進制的字符串,在網(wǎng)上找到了一些方法嘗試之后,均發(fā)現(xiàn)存在一個問題-->字符串轉(zhuǎn)為16進制后再轉(zhuǎn)回來,英文正常,中文出現(xiàn)亂碼

經(jīng)過考慮決定通過以下方式進行解決: 

  1)在將字符串轉(zhuǎn)為16進制之前先進行一次轉(zhuǎn)化,先將其轉(zhuǎn)化成為Unicode編碼(相當于把中文用英文字符代替),在轉(zhuǎn)化成為16進制

  2)相反的,在十六進制轉(zhuǎn)換為字符串后的得到的是Unicode編碼,此時再將Unicode編碼解碼即可獲取原始字符串

代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

/**

?* 字符串轉(zhuǎn)換unicode

?*/

public static String string2Unicode(String string) {

  StringBuffer unicode = new StringBuffer();

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

    // 取出每一個字符

?char c = string.charAt(i);

?// 轉(zhuǎn)換為unicode

?unicode.append("\\u" + Integer.toHexString(c));

  }

  return unicode.toString();

}

*字符串轉(zhuǎn)為16進制

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

?* 字符串轉(zhuǎn)化成為16進制字符串

?* @param s

?* @return

?*/

public static String strTo16(String s) {

?String str = "";

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

??int ch = (int) s.charAt(i);

??String s4 = Integer.toHexString(ch);

??str = str + s4;

?}

?return str;

}

*16進制轉(zhuǎn)為字符串

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

/**

?* 16進制轉(zhuǎn)換成為string類型字符串

?* @param s

?* @return

?*/

public static String hexStringToString(String s) {

?if (s == null || s.equals("")) {

??return null;

?}

?s = s.replace(" ", "");

?byte[] baKeyword = new byte[s.length() / 2];

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

??try {

???baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));

??} catch (Exception e) {

???e.printStackTrace();

??}

?}

?try {

??s = new String(baKeyword, "UTF-8");

??new String();

?} catch (Exception e1) {

??e1.printStackTrace();

?}

?return s;

}

*Unicode轉(zhuǎn)為字符串

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

?* unicode 轉(zhuǎn)字符串

?*/

public static String unicode2String(String unicode) {

?StringBuffer string = new StringBuffer();

?String[] hex = unicode.split("\\\\u");

?for (int i = 1; i < hex.length; i++) {

??// 轉(zhuǎn)換出每一個代碼點

??int data = Integer.parseInt(hex[i], 16);

??// 追加成string

??string.append((char) data);

?}

?return string.toString();

}

此方法雖然解決了轉(zhuǎn)化過程中中文亂碼的問題,但是過于復雜,筆者后來又發(fā)現(xiàn)一種新的轉(zhuǎn)化方式,可直接轉(zhuǎn)化,中文不亂碼,

代碼如下:

*字符串轉(zhuǎn)16進制

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/**

?* 字符串轉(zhuǎn)換成為16進制(無需Unicode編碼)

?* @param str

?* @return

?*/

public static String str2HexStr(String str) {

?char[] chars = "0123456789ABCDEF".toCharArray();

?StringBuilder sb = new StringBuilder("");

?byte[] bs = str.getBytes();

?int bit;

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

??bit = (bs[i] & 0x0f0) >> 4;

??sb.append(chars[bit]);

??bit = bs[i] & 0x0f;

??sb.append(chars[bit]);

??// sb.append(' ');

?}

?return sb.toString().trim();

}

*16進制轉(zhuǎn)為字符串

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/**

?* 16進制直接轉(zhuǎn)換成為字符串(無需Unicode解碼)

?* @param hexStr

?* @return

?*/

public static String hexStr2Str(String hexStr) {

?String str = "0123456789ABCDEF";

?char[] hexs = hexStr.toCharArray();

?byte[] bytes = new byte[hexStr.length() / 2];

?int n;

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

??n = str.indexOf(hexs[2 * i]) * 16;

??n += str.indexOf(hexs[2 * i + 1]);

??bytes[i] = (byte) (n & 0xff);

?}

?return new String(bytes);

}

下面是補充

java字符串和十六進制字符串互轉(zhuǎn)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

public class HexStringUtils {

?private static final char[] DIGITS_HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',

???'E', 'F' };

?protected static char[] encodeHex(byte[] data) {

??int l = data.length;

??char[] out = new char[l << 1];

??for (int i = 0, j = 0; i < l; i++) {

???out[j++] = DIGITS_HEX[(0xF0 & data[i]) >>> 4];

???out[j++] = DIGITS_HEX[0x0F & data[i]];

??}

??return out;

?}

?protected static byte[] decodeHex(char[] data) {

??int len = data.length;

??if ((len & 0x01) != 0) {

???throw new RuntimeException("字符個數(shù)應該為偶數(shù)");

??}

??byte[] out = new byte[len >> 1];

??for (int i = 0, j = 0; j < len; i++) {

???int f = toDigit(data[j], j) << 4;

???j++;

???f |= toDigit(data[j], j);

???j++;

???out[i] = (byte) (f & 0xFF);

??}

??return out;

?}

?protected static int toDigit(char ch, int index) {

??int digit = Character.digit(ch, 16);

??if (digit == -1) {

???throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);

??}

??return digit;

?}

?public static String toHex(String str) {

??return new String(encodeHex(str.getBytes()));

?}

?public static String fromHex(String hex) {

??return new String(decodeHex(hex.toCharArray()));

?}

?public static void main(String[] args) {

??String s = "abc你好";

??String hex = toHex(s);

??String decode = fromHex(hex);

??System.out.println("原字符串:" + s);

??System.out.println("十六進制字符串:" + hex);

??System.out.println("還原:" + decode);

?}

}

toHexString

public static String toHexString(int i)以十六進制的無符號整數(shù)形式返回一個整數(shù)參數(shù)的字符串表示形式。
如果參數(shù)為負,那么無符號整數(shù)值為參數(shù)加上 232;否則等于該參數(shù)。將該值轉(zhuǎn)換為十六進制(基數(shù) 16)的無前導 0 的 ASCII 數(shù)字字符串。如果無符號數(shù)的大小值為零,則用一個零字符 '0' ('\u0030') 表示它;否則,無符號數(shù)大小的表示形式中的第一個字符將不是零字符。用以下字符作為十六進制數(shù)字:

0123456789abcdef

這些字符的范圍是從 '\u0030' 到 '\u0039' 和從 '\u0061' 到 '\u0066'。如果希望得到大寫字母,可以在結果上調(diào)用 String.toUpperCase() 方法:

Integer.toHexString(n).toUpperCase()

參數(shù):
i - 要轉(zhuǎn)換成字符串的整數(shù)。

返回:

用十六進制(基數(shù) 16)參數(shù)表示的無符號整數(shù)值的字符串表示形式。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

// 轉(zhuǎn)化字符串為十六進制編碼

public static String toHexString(String s)

{

String str="";

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

{

int ch = (int)s.charAt(i);

String s4 = Integer.toHexString(ch);

str = str + s4;

}

return str;

}

// 轉(zhuǎn)化十六進制編碼為字符串

public static String toStringHex(String s)

{

byte[] baKeyword = new byte[s.length()/2];

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

{

try

{

baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));

}

catch(Exception e)

{

e.printStackTrace();

}

}

try

{

s = new String(baKeyword, "utf-8");//UTF-16le:Not

}

catch (Exception e1)

{

e1.printStackTrace();

}

return s;

}

// 轉(zhuǎn)化十六進制編碼為字符串

public static String toStringHex(String s)

{

byte[] baKeyword = new byte[s.length()/2];

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

{

try

{

baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));

}

catch(Exception e)

{

e.printStackTrace();

}

}

try

{

s = new String(baKeyword, "utf-8");//UTF-16le:Not

}

catch (Exception e1)

{

e1.printStackTrace();

}

return s;

}

public static void main(String[] args) {

System.out.println(encode("中文"));

System.out.println(decode(encode("中文")));

}

/*

* 16進制數(shù)字字符集

*/

private static String hexString="0123456789ABCDEF";

/*

* 將字符串編碼成16進制數(shù)字,適用于所有字符(包括中文)

*/

public static String encode(String str)

{

//根據(jù)默認編碼獲取字節(jié)數(shù)組

byte[] bytes=str.getBytes();

StringBuilder sb=new StringBuilder(bytes.length*2);

//將字節(jié)數(shù)組中每個字節(jié)拆解成2位16進制整數(shù)

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

{

sb.append(hexString.charAt((bytes[i]&0xf0)>>4));

sb.append(hexString.charAt((bytes[i]&0x0f)>>0));

}

return sb.toString();

}

/*

* 將16進制數(shù)字解碼成字符串,適用于所有字符(包括中文)

*/

public static String decode(String bytes)

{

ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);

//將每2位16進制整數(shù)組裝成一個字節(jié)

for(int i=0;i<bytes.length();i+=2)

baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));

return new String(baos.toByteArray());

}

第二種方法:

將指定byte數(shù)組以16進制的形式打印到控制臺

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

package com.nantian.iclient.atm.sdb;

public class Util {

public Util() {

}

/**

* 將指定byte數(shù)組以16進制的形式打印到控制臺

* @param hint String

* @param b byte[]

* @return void

*/

public static void printHexString(String hint, byte[] b) {

System.out.print(hint);

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

String hex = Integer.toHexString(b[i] & 0xFF);

if (hex.length() == 1) {

hex = '0' + hex;

}

System.out.print(hex.toUpperCase() + " ");

}

System.out.println("");

}

/**

*

* @param b byte[]

* @return String

*/

public static String Bytes2HexString(byte[] b) {

String ret = "";

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

String hex = Integer.toHexString(b[i] & 0xFF);

if (hex.length() == 1) {

hex = '0' + hex;

}

ret += hex.toUpperCase();

}

return ret;

}

/**

* 將兩個ASCII字符合成一個字節(jié);

* 如:"EF"--> 0xEF

* @param src0 byte

* @param src1 byte

* @return byte

*/

public static byte uniteBytes(byte src0, byte src1) {

byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();

_b0 = (byte)(_b0 << 4);

byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();

byte ret = (byte)(_b0 ^ _b1);

return ret;

}

/**

* 將指定字符串src,以每兩個字符分割轉(zhuǎn)換為16進制形式

* 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9}

* @param src String

* @return byte[]

*/

public static byte[] HexString2Bytes(String src){

byte[] ret = new byte[8];

byte[] tmp = src.getBytes();

for(int i=0; i<8; i++){

ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);

}

return ret;

}

}

以上就是JAVA 十六進制與字符串的轉(zhuǎn)換的詳細內(nèi)容,希望可以幫到你。

轉(zhuǎn)自:微點閱讀? ?https://www.weidianyuedu.com文章來源地址http://www.zghlxwxcb.cn/news/detail-731215.html

到了這里,關于JAVA 十六進制與字符串的轉(zhuǎn)換淺談的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 字符串string轉(zhuǎn)換長整型int(八進制、十進制、十六進制)

    strtoul() 是C語言標準庫中用于將字符串轉(zhuǎn)換成無符號長整數(shù)的函數(shù)。它的完整原型如下: 其中, nptr 代表要被轉(zhuǎn)換成無符號長整數(shù)的字符串; endptr 是一個指向字符指針的指針,該指針指向已經(jīng)轉(zhuǎn)換了的最后一個字符的下一個位置; base 指定要采用的進制,范圍是2 ~ 36。 該函

    2024年02月07日
    瀏覽(31)
  • python中str、bytes、十六進制字符串的相互轉(zhuǎn)換

    python中str、bytes、十六進制字符串的介紹 str是python的內(nèi)置函數(shù),str字符串在python中使用 \\\'\\\' 或者 \\\"\\\" 括起來的字符串,比如: bytes也是python的內(nèi)置函數(shù),bytes字符串通常以b開頭,比如: 十六進制字符串是指字符串中每個字符表示一個十六進制數(shù),兩個字符組成一個字節(jié),比如

    2024年02月21日
    瀏覽(33)
  • 51單片機LCD1602液晶屏顯示字符,字符串,(有)無符號整數(shù),十六進制數(shù),二進制數(shù)等

    51單片機LCD1602液晶屏顯示字符,字符串,(有)無符號整數(shù),十六進制數(shù),二進制數(shù)等

    LCD1602液晶顯示器是廣泛使用的一種字符型液晶顯示模塊。液晶顯示模塊具有體積小、功耗低、顯示內(nèi)容豐富、超薄輕巧等優(yōu)點,在嵌入式應用系統(tǒng)中得到越來越廣泛的應用,這講中向大家介紹的LCD1602?液晶顯示模塊(其內(nèi)部控制器為HD44780?芯片),它可以顯示兩行,每行16?個字

    2024年02月04日
    瀏覽(33)
  • 十六進制轉(zhuǎn)換到十進制(java)

    一、前言 ?首先我們要知道十六進制的轉(zhuǎn)換以及十以上的進制的轉(zhuǎn)換與十以下的進制轉(zhuǎn)換是不一樣的,它們在一位上會用超過9的數(shù)字,這個我們在利用以前的方法就行不通了,我接下來就向大家分享一種方法。(注:在Java中10---15分別用A B C D E F表示,不分大小寫) 二、正文

    2024年02月11日
    瀏覽(32)
  • Java中十六進制與十進制之間互相轉(zhuǎn)換

    提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔 提示:這里可以添加本文要記錄的大概內(nèi)容: 提示:以下是本篇文章正文內(nèi)容,下面案例可供參考 話不多說,直接上代碼 如將十進制數(shù)字 108 轉(zhuǎn)換為十六進制 代碼示例如下: 運行結果: 這樣將一個十

    2024年02月12日
    瀏覽(28)
  • 【計算機網(wǎng)絡】TCP中payload的解析,在python中的體現(xiàn)?字節(jié)、字符?為啥字節(jié)用十六進制表示?

    【計算機網(wǎng)絡】TCP中payload的解析,在python中的體現(xiàn)?字節(jié)、字符?為啥字節(jié)用十六進制表示?

    問題來源 最近要根據(jù)一份國家標準協(xié)議進行解碼,但是對于進制,字節(jié),字符,編碼,轉(zhuǎn)碼有比較多的問題。其中我想實現(xiàn)的主要需求如下: 有客戶端與服務端,進行tcp通信,客戶端要發(fā)送一個請求,tcp中payload請求大概是這樣,有很多個十六進制表示的字節(jié)組成 根據(jù)協(xié)議的

    2024年02月11日
    瀏覽(26)
  • Qt將十二位整形十進制轉(zhuǎn)換成十六進制,在轉(zhuǎn)為ascii字符,并下發(fā)串口。在接受端完整還原這個十二位的十進制數(shù)。

    可以按照以下步驟進行操作: 將十進制數(shù)123456789012轉(zhuǎn)換成十六進制字符串: 其中, %1 表示替換第1個參數(shù), 0 表示輸出的最小位數(shù)為0, 16 表示輸出的進制為16, ull 表示無符號長長整型。 2. 將十六進制字符串轉(zhuǎn)換成ASCII字符:

    2024年02月05日
    瀏覽(29)
  • JS十六進制,CRC冗余,小程序發(fā)送藍牙數(shù)據(jù),十六進制GBK編碼轉(zhuǎn)換等

    小程序問題:https://kf.qq.com/faq/170705YVZFZZ170705eyI7Rr.html 調(diào)用: 注意:這里的true和false代表是否大端小端轉(zhuǎn)換 調(diào)用: 調(diào)用: 調(diào)用: 調(diào)用: 調(diào)用: 此代碼寫到小程序utils目錄下的utuils.js文件中 調(diào)用:頁面最上邊先引入,然后再使用 調(diào)用: 這里發(fā)送buffer1給小程序公用api就可 調(diào)

    2024年02月16日
    瀏覽(32)
  • python實現(xiàn)十六進制轉(zhuǎn)十進制

    python實現(xiàn)十六進制轉(zhuǎn)十進制

    先來看下十六進制的定義以及表示方式,以下是百度百科上的解釋: 十六進制 (簡寫為 hex 或下標16)是一種基數(shù)為16的計數(shù)系統(tǒng),是一種逢16進1的進位制。通常用數(shù)字0、1、2、3、4、5、6、7、8、9和字母A、B、C、D、E、F(a、b、c、d、e、f)表示,其中:A~F表示10~15,這些稱作

    2023年04月19日
    瀏覽(27)
  • 【FPGA仿真】Matlab生成二進制、十六進制的txt數(shù)據(jù)以及Vivado讀取二進制、十六進制數(shù)據(jù)并將結果以txt格式保存

    在使用Vivado軟件進行Verilog程序仿真時可能需要對模塊輸入仿真的數(shù)據(jù),因此我們需要一個產(chǎn)生數(shù)據(jù)的方法(二進制或者十六進制的數(shù)據(jù)),Matlab軟件是一個很好的工具,當然你也可以使用VS等工具。 以下分別給出了使用Matlab模擬產(chǎn)生二進制和十六進制數(shù)據(jù)的例子,例子僅供參

    2024年02月01日
    瀏覽(145)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包