奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

JAVA認證

JAVA十六進制與字符串的轉換方法

時間:2024-09-12 13:35:51 JAVA認證 我要投稿
  • 相關推薦

JAVA十六進制與字符串的轉換方法

  在java程序中,十六進制與字符串是怎么轉換的呢?下面yjbys小編就為大家分享JAVA十六進制與字符串的轉換方法,一起來看看吧!

  第一種方法:

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

  toHexString

  public static String toHexString(int i)以十六進制的無符號整數形式返回一個整數參數的字符串表示形式。

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

  0123456789abcdef

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

  Integer.toHexString(n).toUpperCase()

  參數:

  i - 要轉換成字符串的整數。

  返回:

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

  // 轉化字符串為十六進制編碼

  public static String toHexString(String s)

  {

  String str="";

  for (int i=0;i

  {

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

  String s4 = Integer.toHexString(ch);

  str = str + s4;

  }

  return str;

  }

  // 轉化十六進制編碼為字符串

  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 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進制數字字符集

  */

  private static String hexString="0123456789ABCDEF";

  /*

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

  */

  public static String encode(String str)

  {

  //根據默認編碼獲取字節數組

  byte[] bytes=str.getBytes();

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

  //將字節數組中每個字節拆解成2位16進制整數

  for(int i=0;i

  {

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

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

  }

  return sb.toString();

  }

  /*

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

  */

  public static String decode(String bytes)

  {

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

  //將每2位16進制整數組裝成一個字節

  for(int i=0;i

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

  return new String(baos.toByteArray());

  }


更多相關文章推薦:

1.java習題及答案

2.Java培訓去哪里好?要多久?

3.2016年Java筆試題及答案

4.java考試習題及答案

5.JAVA軟件工程師的工資待遇怎么樣

6.JAVA+HTML5的優勢有哪些

7.Java 開發環境配置

8.2016最新Java認證筆試題及答案

9.SUN認證Java2程序員考試題及答案

10.2016最新java考試題庫及答案

  第二種方法:

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

  代碼如下:

  package com.nantian.iclient.atm.sdb;

  public class Util {

  public Util() {

  }

  /**

  * 將指定byte數組以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字符合成一個字節;

  * 如:"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,以每兩個字符分割轉換為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;

  }

  }


更多相關文章推薦:

1.java習題及答案

2.Java培訓去哪里好?要多久?

3.2016年Java筆試題及答案

4.java考試習題及答案

5.JAVA軟件工程師的工資待遇怎么樣

6.JAVA+HTML5的優勢有哪些

7.Java 開發環境配置

8.2016最新Java認證筆試題及答案

9.SUN認證Java2程序員考試題及答案

10.2016最新java考試題庫及答案

【JAVA十六進制與字符串的轉換方法】相關文章:

關于Java的字符轉換10-14

有關JavaScript 字符串與數組轉換函數06-25

關于JAVA字符串的拼接與性能08-06

Json對象與Json字符串4種轉換方式10-17

sun認證java關于字符串處理技巧08-26

word文件的轉換方法08-09

Word轉換PPT常用的方法10-12

int和String互相轉換的方法07-07

Word轉換成PPT的方法11-07

拉丁舞重心轉換方法09-12

主站蜘蛛池模板: 商洛市| 江川县| 永年县| 平和县| 长宁区| 东乌珠穆沁旗| 永福县| 尤溪县| 昭通市| 区。| 土默特右旗| 长春市| 五莲县| 金昌市| 泸定县| 子洲县| 邯郸市| 靖远县| 页游| 海安县| 乐安县| 监利县| 云南省| 洛隆县| 凤翔县| 寿阳县| 泽库县| 新宁县| 醴陵市| 鱼台县| 桃源县| 平陆县| 壤塘县| 东方市| 石门县| 于都县| 睢宁县| 翼城县| 鹿泉市| 延安市| 宜章县|