2013-08-25 76 views
-2

我在Java中下面的代碼:ByteArrayOutputStream在C#

public static byte[] hex(String hex) { 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
int nexti = 0; 
int nextb = 0; 
boolean highoc = true; 
outer: 
while (true) 
{ 
    int number = -1; 
    while (number == -1) { 
    if (nexti == hex.length()) { 
     break outer; 
    } 
    char chr = hex.charAt(nexti); 
    if ((chr >= '0') && (chr <= '9')) 
     number = chr - '0'; 
    else if ((chr >= 'a') && (chr <= 'f')) 
     number = chr - 'a' + 10; 
    else if ((chr >= 'A') && (chr <= 'F')) 
     number = chr - 'A' + 10; 
    else { 
     number = -1; 
    } 
    nexti++; 
    } 
    if (highoc) { 
    nextb = number << 4; 
    highoc = false; 
    } else { 
    nextb |= number; 
    highoc = true; 
    baos.write(nextb); 
    } 
} 
label161: return baos.toByteArray(); 

}

我試圖將其轉換爲C#和失敗,因爲MemoryStream的是唯一的選擇,而我不沒有緩衝區。

這是我現在有:

public static byte[] fromString(string hex) 
    { 
     MemoryStream baos = new MemoryStream(); 
     int nexti = 0; 
     int nextb = 0; 
     bool highoc = true; 
     for (; ;) 
     { 
      int number = -1; 
      while (number == -1) 
      { 
       if (nexti == hex.Length) 
       { 
        goto END; 
       } 
       char chr = hex.ToCharArray()[nexti]; 
       if (chr >= '0' && chr <= '9') 
       { 
        number = chr - '0'; 
       } 
       else if (chr >= 'a' && chr <= 'f') 
       { 
        number = chr - 'a' + 10; 
       } 
       else if (chr >= 'A' && chr <= 'F') 
       { 
        number = chr - 'A' + 10; 
       } 
       else 
       { 
        number = -1; 
       } 
       nexti++; 
      } 
      if (highoc) 
      { 
       nextb = number << 4; 
       highoc = false; 
      } 
      else 
      { 
       nextb |= number; 
       highoc = true; 
       baos.Write(nextb); 
      } 
     } 
    END: 
     return baos.toByteArray(); 
    } 

還有什麼我可以做,使之象Java的方式..謝謝合作?

+0

'while(true)'是少數不需要改變的事情之一。 –

+1

不清楚有什麼問題。發佈編譯器或異常消息或聲明期望與實際結果。 –

+0

只是一個問題:之後你對字節數組做了什麼?將它轉換爲數字? – oddparity

回答

1

這是類似的東西

public static byte[] StringToByteArrayFastest(string hex) { 
     if (hex.Length % 2 == 1) 
      throw new Exception("The binary key cannot have an odd number of digits"); 

     byte[] arr = new byte[hex.Length >> 1]; 

     for (int i = 0; i <hex.Length>> 1; ++i) 
     { 
      arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1]))); 
     } 

     return arr; 
    } 

public static int GetHexVal(char hex) { 
    int val = (int)hex; 
    //For uppercase A-F letters: 
    return val - (val < 58 ? 48 : 55); 
    //For lowercase a-f letters: 
    //return val - (val < 58 ? 48 : 87); 
    //Or the two combined, but a bit slower: 
    //return val - (val < 58 ? 48 : (val < 97 ? 55 : 87)); 
} 





public static byte[] StringToByteArray(String hex) 
{ 
    int NumberChars = hex.Length; 
    byte[] bytes = new byte[NumberChars/2]; 
    for (int i = 0; i < NumberChars; i += 2) 
    bytes[i/2] = Convert.ToByte(hex.Substring(i, 2), 16); 
    return bytes; 
} 





private byte[] HexStringToByteArray(string hexString) 
{ 
    int hexStringLength = hexString.Length; 
    byte[] b = new byte[hexStringLength/2]; 
    for (int i = 0; i < hexStringLength; i += 2) 
    { 
     int topChar = (hexString[i] > 0x40 ? hexString[i] - 0x37 : hexString[i] - 0x30) << 4; 
     int bottomChar = hexString[i + 1] > 0x40 ? hexString[i + 1] - 0x37 : hexString[i + 1] - 0x30; 
     b[i/2] = Convert.ToByte(topChar + bottomChar); 
    } 
    return b; 
} 

這裏有更多的人。 How do you convert Byte Array to Hexadecimal String, and vice versa?

+0

'hex.Length >> 1'和'i << 1'完全沒有用處和晦澀難懂。只需分開並乘以2。 –

相關問題