「0xd80xff0xe0」のような文字列がある場合は
Text.Split(' ').Select(part => byte.Parse(part, System.Globalization.NumberStyles.HexNumber)).ToArray();
しかし、「0xd8ffe0」のような文字列を取得した場合、どうしたらよいかわかりません。
また、バイト配列を1つの文字列として記述する方法を推奨することもできます。
「0xd80xff0xe0」のような文字列がある場合は
Text.Split(' ').Select(part => byte.Parse(part, System.Globalization.NumberStyles.HexNumber)).ToArray();
しかし、「0xd8ffe0」のような文字列を取得した場合、どうしたらよいかわかりません。
また、バイト配列を1つの文字列として記述する方法を推奨することもできます。
文字列の解析を開始する前に、文字列をスクラブする必要があります。まず、先頭の0xを削除してから、文字列を列挙するときにスペースをスキップします。ただし、これにLINQを使用することは、おそらく最善のアプローチではありません。1つは、コードがあまり読みにくく、デバッグしている場合にステップスルーするのが難しいことです。しかしまた、16進数/バイト変換を非常に高速にするために実行できるいくつかのトリックがあります。たとえば、Byte.Parseを使用せずに、代わりに配列インデックスを使用して、対応する値を「検索」します。
しばらく前に、ASCIIEncodingやUTF8EncodingなどのようにEncoding基本クラスから派生するHexEncodingクラスを実装しました。これを使用するのは非常に簡単です。これもかなり最適化されており、データのサイズによっては非常に重要になる可能性があります。
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
16進String
数byte[]
:
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>
->16進数String
:
return sequence.Aggregate(string.Empty,
(result, value) => result +
string.Format(CultureInfo.InvariantCulture, "{0:x2}", value));