-2

I have the following code in 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();

}

I'm trying to convert it to C#, and failing, because MemoryStream is the only option, and I don't have a buffer.

This is what I have now:

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

What else can I do to make it work like the way in Java?.. Thanks.

4

1 回答 1

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

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

于 2013-08-25T19:51:16.680 回答