簡體   English   中英

如何從十進制轉換為ASCII字符

[英]how to convert from decimal to ASCII characters

我試圖編寫一個函數,將字符串中的最后兩個十六進制轉換為ASCII字符。 如“ ab3A”應打印“ ab:”

這是我編寫的代碼,它將后兩位轉換為十進制,但無法將十進制轉換為ASCII字符。 我試圖使用.toString()來完成它,但是沒有成功。

 private static String unmangle(String word)
 {      
    String newTemp = word.substring(word.indexOf('%')+1);
    int hex = hexToInt(newTemp);
    String strI = Integer.toString(hex);

    System.out.println(strI);

    word=word.replace("%", "");
    word=word.replace("+", " ");
    return word = word.replace(newTemp, "")+ strI;


}

您非常接近:您所需Integer.toString的只是Integer.toString而不是調用Integer.toString

private static String unmangle(String word)
{      
    String newTemp = word.substring(word.indexOf('%')+1);
    char hex = (char)hexToInt(newTemp);

    word=word.replace("%", "");
    word=word.replace("+", " ");
    return word = word.replace(newTemp, "")+ hex;
}

首先,您應該知道如何將十六進制轉換為ASCII。

String newTemp=word.substring(word.length-1,word.length);
char a=newTemp.substring(0,0);
char b=newTemp.substring(1,1);
int c=0,d=0;
//And you should convert to int.
if(a='A'|'a')
c=10;//the same to d.
//and 
c=c*16+d;
String strI = Integer.toString(c);
return word.substring(0,word.length-2)+strI;

對不起,我的英語不好。 這就是我解決這個問題的方法strI = Integer.toString(hex);

這句話是錯誤的。它使十六進制成為字符串。例如,如果hex = 97,StrI =“ 97”而不是“ a”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM