Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there not a socket ntol for signed long?

Tags:

c

sockets

If I want to transport a signed long (32 bit integer) across the network what do I do? If I send as a signed long then at other end I won't know (potentially) how remote machine represents signed numbers? So what do I do?

like image 576
Angus Comber Avatar asked Jan 27 '26 01:01

Angus Comber


2 Answers

Just use ntohl and htonl and cast or implicitly convert back and forth between uint32_t and int32_t.

like image 64
R.. GitHub STOP HELPING ICE Avatar answered Jan 28 '26 16:01

R.. GitHub STOP HELPING ICE


Because ntohl() operates on byte-order, so it doesn't care much about the sign, all it cares for is how many bits are there. To answer the question in title.

To answer the question "what do I do?" beyond stating that you're asking question — you convert it to unsigned and then convert back on the other sign, because besides not knowing whether it's signed or not, the arbitrary remote also doesn't know what this number is about and your particular remote may be more knowledgeable of the matter.

like image 23
Michael Krelin - hacker Avatar answered Jan 28 '26 17:01

Michael Krelin - hacker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!