VFPX / Win32API

Provides documentation for calling hundreds of Win32API functions from VFP
124 stars 47 forks source link

htons() issue with new versions of Windows #9

Closed newdatasystems closed 8 months ago

newdatasystems commented 8 months ago

Note sure where to put this information. I don't want to fork just to edit the .md so I'm gonna make it an ticket.

In the "Hints" section for HTONS, it says that newer versions of windows set the 16th bit. But that's not quite right. For different numbers it sets more bits.

587 becomes 150274 instead of 19202

10 0100 1011 0000 0010 = 150274
00 0100 1011 0000 0010 = 19202

2525 becomes 646409 instead of 56585

1001 1101 1101 0000 1001 = 646409
0000 1101 1101 0000 1001 = 56585

20000 becomes 5120078 instead of 8270

0100 1110 0010 0000 0100 1110 = 5120078
0000 0000 0010 0000 0100 1110 = 8270

On Windows Server 2022 and Windows 11 htons() is returning a result with more than 16-bits. (To fill in the extra bits, it's cycling through the significant bits of the number again. So it's simply nonsense.)

The fix is to ignore everything after the 15th bit. e.g.

=num2word(BITAND(htons(587),65535))
DougHennig commented 8 months ago

Thanks.