English 中文(简体)
• 如何在C#中找到高度和低度的ger,把它当作一个果园送往一个 com港?
原标题:Getting upper and lower byte of an integer in C# and putting it as a char array to send to a com port, how?

在C,我将这样做。

序号=3510;

char = 编号和编号;> 8;

果园数量和数量;及

二级医院

二级医院

上限和下限

在C#中,我这样做:

            int number = Convert.ToInt16("3510");
            byte upper = byte(number >> 8);
            byte lower = byte(number & 8);
            char upperc = Convert.ToChar(upper);
            char lowerc = Convert.ToChar(lower);
            data = "GETDM" + upperc + lowerc;
            comport.Write(data);

However in the debugger number = 3510, upper = 13 and lower = 0 this makes no sense, if I change the code to >> 6 upper = 54 which is absolutely strange.

基本上,我只想从16个比数中提取上下游,并在“GETDM”之后将其运出港口。

我如何能够这样做? 这一点在C中非常简单,但在C#中,我完全 st。

最佳回答

你的面罩是不正确的,你应当掩盖255(0xff)而不是8。 如果你只想保留第8类底线,则你需要一个只包含第8类底线的面罩,即第255条。

请注意,如果你试图将数项分成两部分,那实际上应当是简短的或短暂的,可以开始,而不是<条码>int<>t(four bytes)。

ushort number = Convert.ToUInt16("3510");
byte upper = (byte) (number >> 8);
byte lower = (byte) (number & 0xff);

注:I ve 使用了ushort 这里不使用<代码>byte作为双向计算法,更便于考虑何时不需要对展期表示担忧。 由于向<条码>下限转换到<条码>的工作方式缩小,因此实际上不会发生这种情况,但这会影响你应当思考的那种情况。

问题回答

页: 1

byte lower = Convert.ToByte(number & 0x00FF);

实例:

ushort number = Convert.ToUInt16("3510");
byte upper = Convert.ToByte(number >> 8);
byte lower = Convert.ToByte(number & 0x00FF);
char upperc = Convert.ToChar(upper);
char lowerc = Convert.ToChar(lower);
data = "GETDM" + upperc + lowerc;

即使所接受的回答符合问题,但我认为,由于问题无所作为,而且并非只是头盔,而且在搜索结果方面令人误解,而正如我们所知,C#中的Int32有32个仲裁点,因此有4个仲裁方。 我将在此举一个例子,在Int32使用的情况下将是有益的。 关于Int32,我们有:

  1. LowWordLowByte
  2. LowWordHighByte
  3. HighWordLowByte
  4. HighWordHighByte.

因此,我制定了以下方法,将Int32的数值转换成几端的海星体,其中每个星号都由一个白天空间与他人分离。 当你传送数据,要求接收人更快地进行处理时,他只能使用Split(“”),并且获得作为单列载体的 by。

public static String IntToLittleEndianWhitespacedHexString(int pValue, uint pSize)
{
    String result = String.Empty;

    pSize = pSize < 4 ? pSize : 4;

    byte tmpByte = 0x00;
    for (int i = 0; i < pSize; i++)
    {
        tmpByte = (byte)((pValue >> i * 8) & 0xFF);
        result += tmpByte.ToString("X2") + " ";
    }

    return result.TrimEnd(   );
}

使用:

String value1 = ByteArrayUtils.IntToLittleEndianWhitespacedHexString(0x927C, 4);
String value2 = ByteArrayUtils.IntToLittleEndianWhitespacedHexString(0x3FFFF, 4);
String value3 = ByteArrayUtils.IntToLittleEndianWhitespacedHexString(0x927C, 2);
String value4 = ByteArrayUtils.IntToLittleEndianWhitespacedHexString(0x3FFFF, 1);

结果是:

  1. 7C 92 00 00
  2. FF FF 03 00
  3. 7C 92
  4. FF.

如果难以理解我所制定的方法,那么以下办法可能更可理解:

public static String IntToLittleEndianWhitespacedHexString(int pValue)
{
    String result = String.Empty;
    byte lowWordLowByte = (byte)(pValue & 0xFF);
    byte lowWordHighByte = (byte)((pValue >> 8) & 0xFF);
    byte highWordLowByte = (byte)((pValue >> 16) & 0xFF);
    byte highWordHighByte = (byte)((pValue >> 24) & 0xFF);

    result = lowWordLowByte.ToString("X2") + " " +
            lowWordHighByte.ToString("X2") + " " +
            highWordLowByte.ToString("X2") + " " +
            highWordHighByte.ToString("X2");

    return result;
}

评注:

  1. Of course insteand of uint pSize there can be an enum specifying Byte, Word, DoubleWord
  2. Instead of converting to hex string and creating the little endian string, you can convert to chars and do whatever you want to do.

希望这将有助于人!

如果是:

byte lower = (byte) ( number & 0xFF );

更具创造性

[System.Runtime.InteropServices.StructLayout( System.Runtime.InteropServices.LayoutKind.Explicit )]
public struct IntToBytes {
    [System.Runtime.InteropServices.FieldOffset(0)]
    public int Int32;
    [System.Runtime.InteropServices.FieldOffset(0)]
    public byte First;
    [System.Runtime.InteropServices.FieldOffset(1)]
    public byte Second;
    [System.Runtime.InteropServices.FieldOffset(2)]
    public byte Third;
    [System.Runtime.InteropServices.FieldOffset(3)]
    public byte Fourth;
}




相关问题
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. ...

热门标签