Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将十六进制转换为0x。。。字节格式(C#)_C#_.net_Arrays_Hex_Byte - Fatal编程技术网

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

如何将十六进制转换为0x。。。字节格式(C#),c#,.net,arrays,hex,byte,C#,.net,Arrays,Hex,Byte,我想将int转换成hex字符串(这部分没问题),然后将hex字符串转换成字节格式0x(hex字符串),以便将其用于数组。目前,我得到了以下代码: // 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.ToSt

我想将
int
转换成
hex
字符串(这部分没问题),然后将
hex
字符串转换成
字节
格式
0x(hex字符串)
,以便将其用于数组。目前,我得到了以下代码:

// 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字节


谢谢你的帮助

尝试使用重载的
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);

}

1D
不是
18
。它的
29
,因为
D
是13。18也将是
12
您确定这是您真正需要的吗
TotalEnghtbyte
byte
这是一种数字数据类型,您正在尝试将字符串存储在那里。我想是的。我用这个来创建xbee框架。在xbee帧中,第3字节是请求的长度字节。因此,我用第一行计算总长度,然后将其转换为十六进制(lenght=18->1D),最后我尝试将这个十六进制字符串转换为字节值(hex:1D->byte:0x1D),下面是答案@m.kazemAkhgary是,对不起,我错了。这是因为我在lenght值上加了11(在我的例子中是18)->11+18=29->1D:),所以如果我只是在字符串的开头加上0x,然后把它转换成byte,我会有:(byte)0xthehextstring?应该是我需要的@不,字节是字节类型,是数字数据类型,不是字符串。它将具有“0x”。您需要将其转换为字符串以将字符附加到字符串的开头。好的,但在我的字节数组中,数据的定义如下:byte[]requestInByte={0x7E,0x00,0x1D,…}这就是为什么我不理解如何将字节与0x。。而且不仅7E,00,1D,…0xNumber是一个文字C#数字数据类型,不是字符串,你不能做你想做的事。所以简单地使用byte mybyte=7E或mybyte=0x7E是一样的?