【问题标题】:How to convert an hex to the 0x... byte format (C#)如何将十六进制转换为 0x... 字节格式 (C#)
【发布时间】:2016-02-13 14:00:49
【问题描述】:

我想将int转换成hex字符串(这部分没问题)然后将hex字符串转换成byte格式@987654326 @ 以便在数组中使用它。目前我有那个代码:

// 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,因此十六进制字符串是 1D(太棒了!)。但问题是我不知道我是否必须做其他事情才能获得 0x1D 字节......

感谢您的帮助!

【问题讨论】:

  • 1D 不是18。它的29 因为D 是13。18 也是12
  • 您确定这是您真正需要的吗? totalLenghtBytebyte,这是数字数据类型,您正在尝试将字符串存储在那里。
  • 是的,我想。我用它来创建一个 xbee 框架。在 xbee 帧中,第 3 个字节是请求的长度字节。所以我用第一行计算总长度,然后将其转换为十六进制(长度 = 18 -> 1D),最后我试图将此十六进制字符串转换为字节值(十六进制:1D ->字节:0x1D)
  • @M.kazemAkhgary 是的,对不起我的错误。这是因为我将 11 添加到我的长度值(在我的示例中为 18)-> 11+18 = 29 -> 1D :)

标签: c# .net arrays hex byte


【解决方案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);

}

【讨论】:

  • 所以如果我只是在字符串的开头添加 0x 然后将其转换为 byte ,我将拥有: (byte) 0xtheHexString ?应该是我需要的!
  • @Nethim 不,字节是字节类型,数字数据类型,而不是字符串。它不会有“0x”。您需要将其转换为字符串以将字符附加到字符串的开头。
  • 好的,但是在我的字节数组中,数据是这样定义的: byte[] requestInByte={0x7E, 0x00 , 0x1D , ... } 这就是为什么我不明白如何使用 0x 字节.. 不仅是 7E , 00, 1D ,...
  • 0xNumber 是文字 C# 数字数据类型,不是字符串,不能为所欲为。
  • 那么简单地使用 byte mybyte= 7E 或 mybyte=0x7E 是一样的吗?
猜你喜欢
  • 1970-01-01
  • 2017-08-14
  • 2015-04-18
  • 2010-10-15
  • 2021-05-19
  • 2022-07-08
  • 1970-01-01
  • 2017-08-23
  • 2022-11-29
相关资源
最近更新 更多