ビットコンベクター:文字列からバイト配列を取得します
-
03-10-2019 - |
質問
「0xd8 0xff 0xe0」のような文字列があるとき
Text.Split(' ').Select(part => byte.Parse(part, System.Globalization.NumberStyles.HexNumber)).ToArray();
しかし、「0xd8ffe0」のような文字列を取得した場合、どうしたらいいかわかりませんか?
また、バイト配列を1つの文字列として書き込む方法を推奨することができます。
解決
文字列を解析する前に、文字列をスクラブする必要があります。まず、先頭の0xを削除してから、文字列を列挙しているときにスペースをスキップするだけです。しかし、これにLINQを使用することは、おそらく最良のアプローチではありません。 1つは、コードがあまり読みやすくないため、デバッグしている場合はステップスルーするのは難しいでしょう。しかし、ヘックス/バイト変換を非常に高速にするためにできるいくつかのトリックがあります。たとえば、byte.parseを使用しないでください。代わりに、配列インデックスを使用して対応する値を「検索」します。
しばらく前に、私はasciienencodingやutf8encodingなどと同じようにエンコードベースクラスに由来するヘキセンコードクラスを実装しました。それを使用することは非常に簡単です。それはかなり最適化されており、データのサイズに応じて非常に重要です。
var enc = new HexEncoding();
byte[] bytes = enc.GetBytes(str); // convert hex string to byte[]
str = enc.GetString(bytes); // convert byte[] to hex string
これが完全なクラスです。投稿にはちょっと大きいことは知っていますが、Docのコメントを削除しました。
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
他のヒント
ヘックス
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>
- >ヘックスString
:return sequence.Aggregate(string.Empty, (result, value) => result + string.Format(CultureInfo.InvariantCulture, "{0:x2}", value));
所属していません StackOverflow