Ⅰ java中 怎麼把 ascii碼轉換為 十六進制
使用這個方法可以傳進去的16進制的數字組成的字元串轉化為utf-8格式的字元串
public static String toStringHex1(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;
}
追問
我要ASCII格式的字元串,有嗎,謝了。
追答
那就修改s = new String(baKeyword, "ASCII")這一行就行了,後面的表示要轉化的編碼格式可以選很多種,以下是你要的代碼
public static String toStringHex1(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, "ASCII");
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
Ⅱ java 字元串轉化為十進制
不知道你准確的用意.
如果只是想把字元串轉換成十進制的,你可以把字元串先轉換為位元組數組,再把位元組轉成十進制的就行了
for(byte b:"mann".getBytes()){
System.out.print(b);
}
Ⅲ Java中,如何把ascii碼轉換成字元
如果是單個字元的話,直接轉換就可以
intd=97;
chare=(char)d;
如果是一串ascii碼的話,最好存在byte[]中
byte[]b={97,98,99};
System.out.println(newString(b));
Ⅳ java怎麼把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<s.length();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)
{
Ⅳ java的string中有什麼方法可以將二進制轉為十進制或ASCII碼
http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/index.html
static String toBinaryString(int i)
以二進制(基數 2)無符號整數形式返回一個整數參數的字元串表示形式。
static String toHexString(int i)
以十六進制的無符號整數形式返回一個整數參數的字元串表示形式。
static String toOctalString(int i)
以八進制(基數 8)無符號整數形式返回一個整數參數的字元串表示形式。
static String toString(int i)
返回一個表示指定整數的 String 對象。
static String toString(int i, int radix)
用第二個參數指定的基數返回第一個參數的字元串表示形式。
Ⅵ JAVA字元串轉16進制ascii碼
String s = "abcd";
byte[] b = s.getBytes();
int[] in = new int[b.length];
for (int i = 0; i < in.length; i++) {
in[i] = b[i]&0xff;
}
for (int j = 0; j < in.length; j++) {
System.out.println(Integer.toString(in[j], 0x10));
}
Ⅶ java字元串ascii轉換為數字
你的問題描述和你的提問題目貌似不一樣的哦。雖然我很不明白你的疑惑,但是我根據你的題目猜想就是把任意的字元串轉換為ascii碼,對嗎?如果是這樣的,只需要把字元串的每個字元都轉為int型,就可以得到你想要的ascii碼了。
代碼可以參考如下:
public
class
StringToAscii
{
public
static
void
main(String[]
args)
{
String
s
=
"abc123"
;
int[]
arr
=
new
int[s.length()];
for
(int
i
=
0
;
i
<
s.length()
;
i
++
)
{
arr[i]
=
s.charAt(i);
System.out.print(arr[i]
+
","
);
}
}
}
Ⅷ 字元和ASCII碼相互轉換(java編程)
使用強制轉換,'1'--->(byte)'1'即是ascii,要十六進制,就用String.format("%02X",(byte)'1');
System.out.println("'1'ASCII="+((byte)'1'));
System.out.printf("'1'ASCII hex=%02X\n",(byte)'1');
//............
System.out.println("40->"+((char)40) );
System.out.println("59->"+((char)59) );
//..............
System.out.println("0x40->"+((char)0x40) );
//.................