How can I copy data from an int (int port1 = 52010) to a pair of unsigned chars (unsigned char port2[2]? I don’t know how to deal with the division.
How can I copy data from an int (int port1 = 52010) to a
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You typically use masking and shifting.
Big-endian:
little-endian:
For things like port numbers as used in IP networking, you typically always go to the so-called “network byte order” (aka “big-endian”), and there’s a special macro for doing this:
Note that this keeps the port number as a
unsigned short, while swapping the bytes if necessary.