如何将十六进制转换为0x ...字节格式(C#)

时间:2015-11-12 09:29:37

标签: c# .net arrays hex byte

我想将int转换为hex字符串(此部分正常),然后将hex字符串转换为{{1格式 byte ,以便将其用于数组。目前我已经有了这段代码:

0x(theHexString)

例如,int值为18,因此Hex字符串为1D(太棒了!)。但问题是我不知道为了获得0x1D字节我是否还要做其他事情......

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

尝试使用带有数字基础的Convert.ToInt32方法的重载:

int int32Value = Convert.ToInt32("1D", fromBase: 16);

byte byteValue = Convert.ToByte(int32Value);

我不确定是否理解了这个问题。

如果你的意思是你想要将十六进制值转换为它的C#表示(" 0xVALUE"),那么只需在生成的十六进制字符串的开头添加这些字符:

"1D".Insert(0, "0x");

无论如何,我做了这个功能,可以帮助你:

/// <summary>
/// Specifies language style to represent an Hexadecimal value
/// </summary>
public enum HexadecimalStyle : int
{

    /// <summary>
    /// C# Hexadecimal syntax.
    /// </summary>
    CSharp = 0,

    /// <summary>
    /// Visual Basic.Net Hexadecimal syntax.
    /// </summary>
    VbNet = 1

}


/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Converts an Hexadecimal value to its corresponding representation in the specified language syntax.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <example> This is a code example.
/// <code>
/// Dim value As String = HexadecimalConvert(HexadecimalStyle.CSharp, "48 65 6C 6C 6F 20 57")
/// Dim value As String = HexadecimalConvert(HexadecimalStyle.VbNet, "48 65 6C 6C 6F 20 57")
/// </code>
/// </example>
/// ----------------------------------------------------------------------------------------------------
/// <param name="style">
/// The <see cref="CryptoUtil.HexadecimalStyle"/> to represent the Hexadecimal value.
/// </param>
/// 
/// <param name="value">
/// The Hexadecimal value.
/// </param>
/// 
/// <param name="separator">
/// The string used to separate Hexadecimal sequences.
/// </param>
/// ----------------------------------------------------------------------------------------------------
/// <returns>
/// The resulting value.
/// </returns>
/// ----------------------------------------------------------------------------------------------------
/// <exception cref="InvalidEnumArgumentException">
/// style
/// </exception>
/// ----------------------------------------------------------------------------------------------------
[DebuggerStepThrough()]
public string HexadecimalConvert(HexadecimalStyle style, string value, string separator = "")
{

    string styleFormat = "";

    switch (style) {

        case CryptoUtil.HexadecimalStyle.CSharp:
            styleFormat = "0x{0}";

            break;
        case CryptoUtil.HexadecimalStyle.VbNet:
            styleFormat = "&H{0}";

            break;
        default:

            throw new InvalidEnumArgumentException(argumentName: "style", invalidValue: style, enumClass: typeof(HexadecimalStyle));
    }

    if (!string.IsNullOrEmpty(separator)) {
        value = value.Replace(separator, "");
    }


    if ((value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) || (value.StartsWith("&H", StringComparison.OrdinalIgnoreCase))) {
        value = value.Substring(2);

    }

    return string.Format(styleFormat, Convert.ToString(value).ToUpper);

}