LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2005, 10:42 AM   #16
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30

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 ?
 
Old 07-07-2005, 02:22 PM   #17
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Which manner ? 32 bits or byte ordering ?
 
Old 07-07-2005, 02:41 PM   #18
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30
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 ?

cheers.
 
Old 07-07-2005, 03:00 PM   #19
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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).
 
Old 07-07-2005, 03:07 PM   #20
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30
And this needs to be implemented by the programmer,rather then beng carried out automatically by the socket API then?

The book I'm reading isn't clear on this point.
 
Old 07-08-2005, 02:22 PM   #21
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Ahh yes. ya know the strange thing is that i dident get an compiler error or warning ??

maybe i did it right in the real code ill have to double check.
 
Old 07-08-2005, 02:46 PM   #22
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
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:
Code:
int32_t a = 2354234;
byteswritten = send(sock, &a, sizeof(a), 0);
/* hopefully byteswritten == sizeof(a) == 4 */
and read it on the other end like
Code:
int32_t a;
bytesread = recv(sock, &a, sizeof(a), 0);
/* hopefully bytesread == sizeof(a) == 4 */
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.
 
Old 07-08-2005, 04:38 PM   #23
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30
I understand.
Thnks 4 your patients.
 
Old 07-08-2005, 07:29 PM   #24
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
yea i was about to post something similar that there are functions to convert
your data or ascii into ither format.

Im reading something similar on C and sockets

fun stuff too bad i dont understand some of the more complicated struct stuff here.
 
Old 07-09-2005, 03:52 PM   #25
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30
..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.
 
Old 07-09-2005, 04:40 PM   #26
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Although converting to network order is The Right Thing to do, another way to hack an int is (assuming 32-bit ints):

Code:
union {
    int i;
    char[4];
}
Load the int and read the chars in the desired order, 0-3 or 3-0, as the mood takes you.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Distro Live CD for Video & Audio Encoding UltimateLinux Linux - Distributions 1 08-26-2005 12:22 PM
Ordering Linux CDs LinuxNewUser Linux - Newbie 13 04-25-2005 12:54 AM
how to convert long integer value to byte array appas Programming 11 11-23-2004 01:56 PM
backup byte-for-byte axion0917 Linux - Software 2 12-11-2003 05:01 PM
DVD rip & encoding to avi tsitras Linux - Software 0 05-07-2003 08:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration