ByteArrayOutputStream in C#

本文关键字:in ByteArrayOutputStream | 更新日期: 2023-09-27 18:28:53

我在Java中有以下代码:

 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中那样工作?。。谢谢

ByteArrayOutputStream in C#

这里有一些类似的

   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;
}

这里还有很多。如何将字节数组转换为十六进制字符串,反之亦然?