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

本文关键字:格式 字节 0x 十六进制 转换 | 更新日期: 2023-09-27 18:21:34

我想将int字符串转换为hex字符串(此部分可以),然后将hex字符串转换为byte格式0x(theHexString)以便将其用于数组。目前我已经得到了代码:

// Calculate the int that will be convert to Hex string
int totalLenght=11+request.Length;  
// Try to convert it into byte
byte totalLenghtByte=Convert.ToByte( totalLenght.ToString("X"));  
// put it into an array of bytes
xbeeFrame[2] =(totalLenghtByte); 

例如,int值为18,因此Hex字符串为1D(很棒!)。但问题是,我不知道是否必须做其他事情才能获得0x1D字节。。。

谢谢你的帮助!

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

尝试使用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);
}