English 中文(简体)
c) 部分 如何划分 按数字分类
原标题:c# How to divide Byte by digits
  • 时间:2024-03-25 02:17:11
  •  标签:
  • c#
  • modbus

我正在开发一套方案拟定语言的Modbus通信模块。

如果在与《议定书》的沟通过程中出现例外情况,

The error code is returned, which is the Hex value,

假设错误代码格式为0x84,例如

0x80 -> Header

0x04 -> Body

It s in the same format as

因此,我想把0x84与0x80和0x04分开。

我是否可以提到任何材料?

问题回答

范围

void Main()
{
    string x = "0x80";
    x.FromHex().Dump();
    128.ToHex().Dump();
}

public static class ext
{
    public static int FromHex(this string value) => 
     (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                ? Int32.Parse(value.Substring(2), NumberStyles.HexNumber)
                : Int32.Parse(value, NumberStyles.HexNumber);
    public static string ToHex(this int value) => String.Format("0x{0:X}", value);
}

成果

128
0x80
``

如果我正确阅读这一问题,你就重新询问如何做手法。 你们没有高超的价值观。 dec只是你以书面形式代表人数的许多方法之一。 仅凭一线,即8点。 您再次询问如何区分低级和4级以上轨道。 你们可以做这样的事,用手法。 你们可以使用双向行动,掩盖头四个或最底层四个轨道,例如:

byte n1 = 0x84;
byte m1 = 0x0F;
byte m2 = 0xF0;
byte n2 = (byte)(n1 & m1);
byte n3 = (byte)(n1 & m2);

Console.WriteLine($"{Convert.ToString(n1, 2),8} 0x{n1:X2}");
Console.WriteLine($"{Convert.ToString(m1, 2),8} 0x{m1:X2}");
Console.WriteLine($"{Convert.ToString(m2, 2),8} 0x{m2:X2}");
Console.WriteLine($"{Convert.ToString(n2, 2),8} 0x{n2:X2}");
Console.WriteLine($"{Convert.ToString(n3, 2),8} 0x{n3:X2}");

这方面的产出如下:

10000100 0x84
00001111 0x0F
11110000 0xF0
00000100 0x04
10000000 0x80

我不想解释一下借方工作的细节。 如果你不理解,你就应当接受“蓝.”逻辑,然后是双tw逻辑。

如果你确实有包含“0x84”的<条码>,则你可以简单地将这一条与实际的<条码>逐项/代码”区分开来,然后如上:

var s = "0x84";
var b = Convert.ToByte(s, 16);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签