導航:首頁 > 編程語言 > javaascii碼轉十進制

javaascii碼轉十進制

發布時間:2021-12-07 12:04:49

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) );
//.................

閱讀全文

與javaascii碼轉十進制相關的資料

熱點內容
看電影時女生手總是環抱著 瀏覽:73
公主和妓女 瀏覽:730
iphone5如何刪除其他 瀏覽:532
win10停在關機界面 瀏覽:629
celluloid教程 瀏覽:210
大話西遊2在線看免費 瀏覽:10
什麼看電視沒有水印 瀏覽:538
手機通訊錄與qq好友 瀏覽:236
牛客網java輸入輸出 瀏覽:370
什麼app要qq登錄 瀏覽:534
oracle資料庫如何執行 瀏覽:80
有趣的家庭的網站有哪些 瀏覽:148
域名升級訪問通知 瀏覽:270
片名《鬼作秀之頭顱鬼屋》 瀏覽:936
電腦系統host文件 瀏覽:996
求一個在線播放的網站 瀏覽:596
四級丶四級電影﹥ 瀏覽:582
怎麼把cad的工具欄調出來 瀏覽:742
強奸了女僵屍的電影 瀏覽:15
能在線觀看最新網址 瀏覽:317

友情鏈接