2009-11-06 222 views
0

有小問題,非常感謝幫助:)字節數組字符串

我應該字節數組轉換爲字符串,並得到這個輸出字符串:「[0,0,0,0]」 之後另一種方法應該將字符串作爲輸入並從第一個字符串中檢索字節數組。

我得到的錯誤,我有number.format異常,所以我想我應該以其他方式convertToString方法。 這是我到目前爲止有:

import java.io.ByteArrayOutputStream; 
import java.util.StringTokenizer; 


public class byteToString { 

    public String convertToString(){ 
     byte[] byteArray = new byte[] {91,79,44,32,79,44,32,79,44,32,79,93}; 
     String holder = new String(byteArray); 
     return holder; 

    } 
    /*was told to use this code to convert back*/ 
     private static byte[] toByteArray(String myString){ 
     myString = myString.substring(0, myString.length()- 1).substring(1); 
     ByteArrayOutputStream myStream = new ByteArrayOutputStream(); 
     for (StringTokenizer myTok = new StringTokenizer(myString, ","); myTok.hasMoreTokens();){ 
      myStream.write(Byte.parseByte(myTok.nextToken().trim())); 
     } 
     return myStream.toByteArray(); 

    } 
    public static void main(String[] args){ 
     String myString = new byteToString().convertToString(); 

     toByteArray(myString); 


    } 
} 

提前謝謝! :)

+0

參見'Arrays.toString(字節[])'http://java.sun.com/javase/6/docs/api/ java/util/Arrays.html#toString(byte []) – McDowell 2009-11-06 10:09:35

回答

3

new byte[] {91,79,44,32,79,44,32,79,44,32,79,93}實際上是[O, O, O, O]陣列的不是 ES!代替使用new byte[] {91,48,44,32,48,44,32,48,44,32,48,93}

而且要注意的是,你可以使用:

myString = myString.substring(1, myString.length() - 1);

代替:

myString = myString.substring(0, myString.length()- 1).substring(1);

它更有效率。

0
public class AAA { 

    public static final byte[] TEST_DATA = {91, 79, 44, 32, 79, 44, 32, 79, 44, 32, 79, 93}; 

    public String convertToString(byte[] array) { 
     return Arrays.toString(array); 
    } 

    private static byte[] toByteArray(String myString) { 
     ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 
     Pattern pattern = Pattern.compile("\\D*(\\d+)"); 
     Matcher matcher = pattern.matcher(myString); 
     while (matcher.find()) { 
      bOut.write((byte)Integer.parseInt(matcher.group(1))); 
     } 
     return bOut.toByteArray(); 
    } 

    public static void main(String[] args) { 
     String myString = new AAA().convertToString(TEST_DATA); 
     byte[] bytes = toByteArray(myString); 
     System.out.println("Test " + (Arrays.equals(bytes, TEST_DATA) ? "passed" : "failed")); 
    } 
} 
+0

優雅的解決方案,thanx!但由於某種原因,我需要使用我編寫的toByteArray方法中的代碼.... – Angela 2009-11-06 10:27:06

0

爲ByteArray的字符串簡單的解決方案是下面

public class ByteArrayToString { 
      public static void main(String args[]) { 
       byte[] random = new byte[] { 34, 65, 54 , 76, 66, 65, 66, 70, -10 }; 
       String utf = new String(random, "UTF-8"); 
       String cp1252 = new String(random, "Cp1252"); 
       String windows1252 = new String(random, "Windows-1252"); 
       System.out.println("String created from byte `enter code here`array in UTF-8 encoding : "+ utf); 
       System.out.println("byte array to String in Cp1252 encoding : " + cp1252); 
       System.out.println("byte array to String in Windows-1252 encoding : "+ windows1252); 
      } 
     }