简体   繁体   English

Delphi中的“ Word”是否等效于Java中的“ Char”变量?

[英]Is “Word” from Delphi an equivalent variable to “Char” on Java?

In Delphi, a Word is a 16 bits unsigned variable (0..65535) 在Delphi中,Word是16位无符号变量(0..65535)

http://www.delphibasics.co.uk/RTL.asp?Name=Word http://www.delphibasics.co.uk/RTL.asp?Name=Word

The char variable in Java is a 16 bit Unicode variable (0..65535) Java中的char变量是16位Unicode变量(0..65535)

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

I have the following code on Delphi 我在Delphi上有以下代码

procedure TForm1.ReadHoldRegisters(var lst: TList<byte>; deviceaddr: byte;
  RegisterAddress, RegisterLength: word);
begin

  lst.Add(deviceaddr);
  lst.Add(3);
  lst.Add(RegisterAddress div 256);
  lst.Add(RegisterAddress mod 256);
  Add_CRC16(lst);
end;

procedure TForm1.Add_CRC16(var Data: TList<byte>);
var
  CRC16Lo, CRC16Hi, CL, CH, SaveHi, SaveLo: byte;
  Flag: Integer;
  b: byte;
begin
  CRC16Lo := $FF;
  CRC16Hi := $FF;
  CL := $1;
  CH := $A0;

  for b in Data do
  begin
    CRC16Lo := CRC16Lo Xor b;
    For Flag := 0 To 7 Do
    Begin
      SaveHi := CRC16Hi;
      SaveLo := CRC16Lo;
      CRC16Hi := CRC16Hi Div 2;
      CRC16Lo := CRC16Lo Div 2;

      If ((SaveLo And $1) = $1) Then
      Begin
        CRC16Hi := CRC16Hi Xor CH;
        CRC16Lo := CRC16Lo Xor CL;
      End;



    End;
  end;
  Data.Add(CRC16Lo);
  Data.Add(CRC16Hi);
end;

And it's equivalent on Java 它在Java上等效

    public void ReadHoldRegisters(List<Byte> lst, byte deviceaddr, char RegisterAddress, char RegisterLength)
        {

            lst.add(deviceaddr);
            lst.add((byte) 3);
            lst.add((byte) (RegisterAddress/256));
            lst.add((byte) (RegisterAddress%256));

            Add_CRC16(lst);

        }

    private void Add_CRC16(List<Byte> Data)
    {
        char  SaveLo, SaveHi;
        int flag;


        char CRC16Lo =  0xFF;
        char CRC16Hi =  0xFF;
        char CL      =  0x1;
        char CH      =  0xA0;


        // início do for in
        for (Byte b : Data)
        {
            CRC16Lo = (char) ((CRC16Lo) ^ b);
            for(flag=0;flag<7;flag++)
            {
                SaveHi = CRC16Hi;
                SaveLo = CRC16Lo;
                CRC16Hi = (char) (CRC16Hi/2);
                CRC16Lo = (char) (CRC16Lo/2);
/*
                if((SaveHi & 0x1) == 0x1 )
                {
                    CRC16Lo = ((char) (CRC16Lo|0x80));
                }
*/
                if((SaveLo & 0x1) == 0x1 )
                {
                    CRC16Hi = ((char) (CRC16Hi^CH));
                    CRC16Lo = ((char) (CRC16Lo^CL));

                }
            }
        }
        // fim do for in

        CRC16Hi = (char) (CRC16Hi & 0xffff);
        CRC16Lo = (char) (CRC16Lo & 0xffff);

        Data.add((byte) CRC16Lo);
        Data.add((byte) CRC16Hi);

    }

When I print the values by doing 当我通过执行打印值时

for (Byte b : lst)
S = S + String.format("%1X ", b);

I get 01 03 00 20 F0 on Delphi and 1 3 0 D8 D9 on Java. 我在Delphi上得到01 03 00 20 F0 ,在Java上得到1 3 0 D8 D9

Assuming that char and word are equivalent variables, what's going wrong with RegisterAddress and RegisterLength my code? 假设char和word是等效变量,我的代码RegisterAddress和RegisterLength怎么了?

确定使用WideChar(经典的Delphi)还是仅使用Char(Delphi 2009起)更合适?

As you said, a Delphi Word is an unsigned 16 bit type. 如您所说,Delphi Word是一种无符号的16位类型。 The Java char likewise is an unsigned 16 bit type. Java char同样是无符号的16位类型。

However, you mention Word in the question, but it doesn't appear in the code. 但是,您在问题中提到Word ,但是它没有出现在代码中。 Well, you use Word in ReadHoldRegisters but nowhere in Add_CRC16 is the Word type used. 那么,您使用WordReadHoldRegisters但无处Add_CRC16Word使用的类型。 So, you aren't using Word . 因此,您没有使用Word You are using Byte . 您正在使用Byte So, the two variants of Add_CRC16 differ significantly because of that. 因此, Add_CRC16的两个变体Add_CRC16而显着不同。

I've not looked at the rest of the code, but the mismatch between 8 bit Byte in Delphi and 16 bit char in Java is the most glaring issue. 我没有看其余的代码,但是最明显的问题是Delphi中的8位Byte与Java中的16位char之间的不匹配。 Start by fixing that problem, and see what comes next. 首先解决该问题,然后查看下一步。

As a final aside, I recommend that you avoid the Delphi Basics website, especially for reference material. 最后,建议您不要使用Delphi Basics网站,特别是对于参考资料。 You are much better served by the official documentation. 官方文档为您提供了更好的服务。 The relevant documentation for this question is Delphi Data Types . 此问题的相关文档是Delphi数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM