ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ok... it was my rusty maths that was giving me the wrong output.
I was calculating my remainder wrong.
The algorithm that jlliagre gave me initially does indeed give me the desired result.
Can someone explain why an integer would be encoded in this manner please ?
I understand the need for a standard network byte odering;and that diffent computer architectures use different byte ordering etc,
I feel I am missing the point with something basic however.
Using my first example.Why when transmitting the value 17,998,720 byte ordering aside , is this number not just transmitted as 17 99 87 20 ?
why re-encode to 1 18 163 128 ?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Computers are storing information in binary (0 and 1), when a number is stored or transmitted, bits, not digits are involved. To represent a 32 bit integer, it is easier to show it using bytes (8 bits numbers) which span from 0 to 255, the numbers 1 18 163 128 represent the four bytes that when combined make 17998720.
This is far more efficient than storing them in decimal, like you suggest (60% better per digit actually).
And this needs to be implemented by the programmer,rather then beng carried out automatically by the socket API then?
Which thing are you asking about? There are some convenience functions ntohl, htonl, ntohs, and htons to convert to and from big endian order which is standard for sending between hosts. Other than that, there's no reason that you'd have to read each byte of your integer that's related particularly to sockets...
That integers are stored as sequences of bits and operated on with twos complement arithmetic is a fundamental property of your processor. You can transmit numbers as ASCII if you want, and it'll be slow and (imo) harder to parse. Easier to look at in a packet capturer though. See atoi(), strtol(), and sprintf() for converting to and from ASCII.
I guess that's all a bit much detail. An important point is that you can just dump the internal representation of an integer out a socket like this:
If one end of the connection is a PC (little endian) and the other is a mac (big endian) then a will come over as a different number. oops. A solution is to always transmit in network (big endian) order. So really the sender always should do
Code:
a = htonl(a);
before sending and the receiver should always do
Code:
a = ntohl(a);
after receiving. Now the endianness of the two hosts' processors doesn't matter. That's how you transmit numbers over a network without converting to ascii. No fancy math necessary.
..Yeah it is fun ...especially wen u have a "eureka" momment and understand something that u were stuck on.
I think i could implement to algorithum in C now if I needed to but I have no idea how the thing was derived.I guess as there is a suite of programs that do the conversion for me I shall not loose sleep for now.
i'm a programming noob and am in the middle of developing a simple irc client,(re-inventing the wheel I know) as an educational excercise in C/Linux.
Thnks again to those who have contributed to making this an enlightening thread.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.