23

14文字の文字列があります。これは7バイトの16進表現です。バイナリに変換したい。小さいConvert.ToString(Convert.ToInt32(hexstring, 16), 2);文字列の場合は機能しますが、14文字の場合は結果が大きすぎるため機能しません。どうすればこれを管理できますか?変換の出力は、長さが56文字のバイナリ文字列である必要があることに注意してください(先行ゼロを保持する必要があります)。(たとえば、(byte)0x01の変換では、「1」ではなく「00000001」が生成されます)

4

8 に答える 8

38

各16進数を4つの2進数に変換できます。

string binarystring = String.Join(String.Empty,
  hexstring.Select(
    c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')
  )
);

using System.Linq;これを機能させるには、ファイルの先頭が必要です。

于 2011-07-07T21:28:11.283 に答える
12
Convert.ToString(Convert.ToInt64(hexstring, 16), 2);

多分?または

Convert.ToString(Convert.ToInt64(hexstring, 16), 2).PadLeft(56, '0');
于 2011-07-07T21:23:36.007 に答える
12

単純なアプローチを取り、独自のマッピングを定義してみませんか?

private static readonly Dictionary<char, string> hexCharacterToBinary = new Dictionary<char, string> {
    { '0', "0000" },
    { '1', "0001" },
    { '2', "0010" },
    { '3', "0011" },
    { '4', "0100" },
    { '5', "0101" },
    { '6', "0110" },
    { '7', "0111" },
    { '8', "1000" },
    { '9', "1001" },
    { 'a', "1010" },
    { 'b', "1011" },
    { 'c', "1100" },
    { 'd', "1101" },
    { 'e', "1110" },
    { 'f', "1111" }
};

public string HexStringToBinary(string hex) {
    StringBuilder result = new StringBuilder();
    foreach (char c in hex) {
        // This will crash for non-hex characters. You might want to handle that differently.
        result.Append(hexCharacterToBinary[char.ToLower(c)]);
    }
    return result.ToString();
}

これにより、先行ゼロが維持されることに注意してください。したがって"aa"、に変換さ"10101010"れます"00000aa"が、に変換され"0000000000000000000010101010"ます。

于 2011-07-07T21:57:28.207 に答える
4

私のC++の背景の答え:

private Byte[] HexToBin(string pHexString)
{
    if (String.IsNullOrEmpty(pHexString))
        return new Byte[0];

    if (pHexString.Length % 2 != 0)
        throw new Exception("Hexstring must have an even length");

    Byte[] bin = new Byte[pHexString.Length / 2];
    int o = 0;
    int i = 0;
    for (; i < pHexString.Length; i += 2, o++)
    {
        switch (pHexString[i])
        {
            case '0': bin[o] = 0x00; break;
            case '1': bin[o] = 0x10; break;
            case '2': bin[o] = 0x20; break;
            case '3': bin[o] = 0x30; break;
            case '4': bin[o] = 0x40; break;
            case '5': bin[o] = 0x50; break;
            case '6': bin[o] = 0x60; break;
            case '7': bin[o] = 0x70; break;
            case '8': bin[o] = 0x80; break;
            case '9': bin[o] = 0x90; break;
            case 'A':
            case 'a': bin[o] = 0xa0; break;
            case 'B':
            case 'b': bin[o] = 0xb0; break;
            case 'C':
            case 'c': bin[o] = 0xc0; break;
            case 'D':
            case 'd': bin[o] = 0xd0; break;
            case 'E':
            case 'e': bin[o] = 0xe0; break;
            case 'F':
            case 'f': bin[o] = 0xf0; break;
            default: throw new Exception("Invalid character found during hex decode");
        }

        switch (pHexString[i+1])
        {
            case '0': bin[o] |= 0x00; break;
            case '1': bin[o] |= 0x01; break;
            case '2': bin[o] |= 0x02; break;
            case '3': bin[o] |= 0x03; break;
            case '4': bin[o] |= 0x04; break;
            case '5': bin[o] |= 0x05; break;
            case '6': bin[o] |= 0x06; break;
            case '7': bin[o] |= 0x07; break;
            case '8': bin[o] |= 0x08; break;
            case '9': bin[o] |= 0x09; break;
            case 'A':
            case 'a': bin[o] |= 0x0a; break;
            case 'B':
            case 'b': bin[o] |= 0x0b; break;
            case 'C':
            case 'c': bin[o] |= 0x0c; break;
            case 'D':
            case 'd': bin[o] |= 0x0d; break;
            case 'E':
            case 'e': bin[o] |= 0x0e; break;
            case 'F':
            case 'f': bin[o] |= 0x0f; break;
            default: throw new Exception("Invalid character found during hex decode");
        }
    }
    return bin;
}
于 2013-09-04T09:58:24.760 に答える
4

このコードを使用して、16進文字列からバイト配列を取得できます

 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;
    }
于 2018-03-28T21:49:44.683 に答える
0

あなたはこれを行うことができます。

私はそれをUtilMathというクラスに入れました。これは良いアイデアです。別のプログラムで使用した場合は、クラスを再度使用できるからです。そしてその名前が示すように、これは私のすべての数学関数に当てはまります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Math.Util
{
    class UtilMath
    {

        public static string hex2binary(string hexvalue)
        {
            // Convert.ToUInt32 this is an unsigned int
            // so no negative numbers but it gives you one more bit
            // it much of a muchness 
            // Uint MAX is 4,294,967,295 and MIN is 0
            // this padds to 4 bits so 0x5 = "0101"
            return String.Join(String.Empty, hexvalue.Select(c => Convert.ToString(Convert.ToUInt32(c.ToString(), 16), 2).PadLeft(4, '0')));
        }
    }
}

今それを使用する前にあなたはそれを含める必要があります、

using Math.Util

その後、それを使用する必要がある場合は、行くことによってそれを呼び出すことができます

UtilMath.hex2binary("FF");

または

String hexString = "FF";
UtilMath.hex2binary(hexString);

お役に立てれば。

于 2012-05-09T03:01:01.237 に答える
0

一度に1文字ずつ変換するとどうなりますか?私はこれをテストすることはできませんが、アイデアはうまくいくはずです。

//Convert.ToString(Convert.ToInt32(hexstring, 16), 2)

StringBuilder sb = new StringBuilder();
foreach( char c in hexstring.ToCharArray() ){
  sb.Append( Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2);
}
于 2011-07-07T21:28:39.007 に答える
0

Convert.FromHexStringは.Net5で導入されました

 var bytes = Convert.FromHexString("13AF3F")
于 2021-12-18T05:37:00.400 に答える