当我有“ 0xd8 0xff 0xe0”之类的字符串时,我会做

Text.Split(' ').Select(part => byte.Parse(part, System.Globalization.NumberStyles.HexNumber)).ToArray();

但是,如果我有像“ 0xd8ffe0”之类的字符串,我不知道该怎么办?

我也可以提出建议如何将字节数组作为一个字符串编写。

有帮助吗?

解决方案

在开始解析之前,您需要擦洗字符串。首先,卸下领先的0x,然后在列举字符串时跳过任何空间。但是使用LINQ可能不是最好的方法。首先,代码不会很可读,如果您要调试,则很难逐步完成。但是,您可以做一些技巧来快速进行十六进制/字节转换。例如,不要使用字节。parse,而是使用数组索引来“查找”相应的值。

不久前,我实现了一个六边形编码类,该类从编码基类衍生而来,例如asciiencoding和utf8encoding等。使用它非常简单。它也非常优化,这可能非常重要,具体取决于数据的大小。

var enc = new HexEncoding();
byte[] bytes = enc.GetBytes(str); // convert hex string to byte[]
str = enc.GetString(bytes);       // convert byte[] to hex string

这是完整的课程,我知道帖子有点大,但我已经删除了文档评论。

public sealed class HexEncoding : Encoding
{

    public static readonly HexEncoding Hex = new HexEncoding( );

    private static readonly char[] HexAlphabet;
    private static readonly byte[] HexValues;

    static HexEncoding( )
    {

        HexAlphabet = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        HexValues = new byte[255];
        for ( int i = 0 ; i < HexValues.Length ; i++ ) {
            char c = (char)i;
            if ( "0123456789abcdefABCDEF".IndexOf( c ) > -1 ) {
                HexValues[i] = System.Convert.ToByte( c.ToString( ), 16 );
            }   // if
        }   // for

    }

    public override string EncodingName
    {
        get
        {
            return "Hex";
        }
    }

    public override bool IsSingleByte
    {
        get
        {
            return true;
        }
    }

    public override int GetByteCount( char[] chars, int index, int count )
    {
        return count / 2;
    }

    public override int GetBytes( char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex )
    {

        int ci = charIndex;
        int bi = byteIndex;

        while ( ci < ( charIndex + charCount ) ) {

            char c1 = chars[ci++];
            char c2 = chars[ci++];

            byte b1 = HexValues[(int)c1];
            byte b2 = HexValues[(int)c2];

            bytes[bi++] = (byte)( b1 << 4 | b2 );

        }   // while

        return charCount / 2;

    }

    public override int GetCharCount( byte[] bytes, int index, int count )
    {
        return count * 2;
    }

    public override int GetChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex )
    {

        int ci = charIndex;
        int bi = byteIndex;

        while ( bi < ( byteIndex + byteCount ) ) {

            int b1 = bytes[bi] >> 4;
            int b2 = bytes[bi++] & 0xF;

            char c1 = HexAlphabet[b1];
            char c2 = HexAlphabet[b2];

            chars[ci++] = c1;
            chars[ci++] = c2;

        }   // while

        return byteCount * 2;

    }

    public override int GetMaxByteCount( int charCount )
    {
        return charCount / 2;
    }

    public override int GetMaxCharCount( int byteCount )
    {
        return byteCount * 2;
    }

}   // class

其他提示

  1. 十六进制 Stringbyte[]:

    byte[] bytes = new byte[value.Length / 2];
    for (int i = 0; i < value.Length; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(value.Substring(i, 2), 16);
    }
    

    如果你有 "0x" 一开始您应该跳过两个字节。

  2. byte[] 或任何 IEnumerable<Byte> - >十六进制 String:

    return sequence.Aggregate(string.Empty,
                              (result, value) =>  result + 
                                                  string.Format(CultureInfo.InvariantCulture, "{0:x2}", value));
    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top