2015-04-26 208 views
-1

我有以下问题:转换int数组为字符串JAVA

我有2 int阵列 - 煤焦ř我如何转换这个数组stringchar?在阵列

实际值是:[-59, -103]

ř - >[-59, -103] - >ř 谢谢。

编辑:

String specialChar = "ř"; 
    System.out.println(specialChar); 
    byte[] tmp = specialChar.getBytes(); 
    System.out.println(Arrays.toString(tmp)); //[-59, -103] 
    int[] byteIntArray = new int[2]; 
    byteIntArray[0] = (int) tmp[0]; 
    byteIntArray[1] = (int) tmp[1]; 
    System.out.println(Arrays.toString(byteIntArray)); //[-59, -103] 
    //now i want convert byteIntArray to string 
+0

你有一个'INT []'或'的byte [] '? –

+0

@BrettOkken我编辑过帖子,看看实际的代码示例.. – slearace

回答

0

怎么样?

byte[] byteArray = new byte[2]; 
byteArray[0] = (byte)byteIntArray[0]; 
byteArray[1] = (byte)byteIntArray[1]; 
String specialChar = new String(byteArray); 

注意String.getBytes()使用本地平台编码字符串转换成字节数组。所以得到的字节数组取决于您的个人系统设置。

如果你希望你的字节数组对其他系统兼容,可以使用标准编码,如 “UTF-8”,而不是:

byte[] tmp = specialChar.getBytes("UTF-8"); // String -> bytes 
String s = new String(tmp, "UTF-8");  // bytes -> String 
+0

不错,谢谢。 – slearace