UTF转换为字节

时间:2018-05-23 18:16:47

标签: c# unicode byte

此代码编译视觉工作室2017但不编译Visual Studio 2015.我想在Visual Studio中编译。为什么不编译?显示错误,如错误CS0103名称' b00001111'在当前上下文中不存在

  public IEnumerable<byte> AsUtf8()
  {
    //up to 7 bits
    if (Value <= 0x007F)
    {
        yield return (byte) Value;
        yield break;
    }

    //up to 11 bits
    if (Value <= 0x07FF)
    {
        yield return (byte) (0b11000000 | (0b00011111 & (Value >> 6))); //tag + upper 5 bits
        yield return (byte) (0b10000000 | (0b00111111 & Value)); //tag + lower 6 bits
        yield break;
    }

    //up to 16 bits
    if (Value <= 0x0FFFF)
    {
        yield return (byte) (0b11100000 | (0b00001111 & (Value >> 12))); //tag + upper 4 bits
        yield return (byte) (0b10000000 | (0b00111111 & (Value >> 6))); //tag + next 6 bits
        yield return (byte) (0b10000000 | (0b00111111 & Value)); //tag + last 6 bits
        yield break;
    }

    //up to 21 bits
    if (Value <= 0x1FFFFF)
    {
        yield return (byte) (0b11110000 | (0b00000111 & (Value >> 18))); //tag + upper 3 bits
        yield return (byte) (0b10000000 | (0b00111111 & (Value >> 12))); //tag + next 6 bits
        yield return (byte) (0b10000000 | (0b00111111 & (Value >> 6))); //tag + next 6 bits
        yield return (byte) (0b10000000 | (0b00111111 & Value)); //tag + last 6 bits
        yield break;
    }

    throw new UnsupportedCodepointException();
}

0 个答案:

没有答案
相关问题