Thread: Function return a value

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cpjust View Post
    I'm pretty sure ntohl() and htonl() do exactly the same thing. There's no way for the function to know what endiness the number is already in, it just flips the bytes around and assumes you knew what the original endiness was...
    Well, hopefully the library knows what the host-order is; and network order is well defined. So the function "knows" either to flip (as above), or do nothing....

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tabstop View Post
    Well, hopefully the library knows what the host-order is; and network order is well defined. So the function "knows" either to flip (as above), or do nothing....
    How can it possibly know the difference between Big Endian and Little Endian? Just by looking at a number there's no way to know.
    Is 0xFE1265CD02 Big Endian or Little Endian? There's no special bit (like a signed number) that says this is Big Endian...
    Code:
    #include <iostream>
    #include <winsock2.h>
    
    using namespace std;
    
    int main()
    {
    	unsigned long num = 1234567890;
    	cout << num << endl;
    	num = htonl( num );
    	cout << num << endl;
    	num = htonl( num );
    	cout << num << endl;
    	return 0;
    }
    Output:
    Code:
    1234567890
    3523384905
    1234567890
    Last edited by cpjust; 07-16-2008 at 09:34 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cpjust View Post
    How can it possibly know the difference between Big Endian and Little Endian? Just by looking at a number there's no way to know.
    Is 0xFE1265CD02 Big Endian or Little Endian? There's no special bit (like a signed number) that says this is Big Endian...
    It knows the difference, because it knows the machine. If the machine is a little-endian machine, the number is little-endian. If the machine is big-endian, the number is big-endian.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tabstop View Post
    It knows the difference, because it knows the machine. If the machine is a little-endian machine, the number is little-endian. If the machine is big-endian, the number is big-endian.
    Yes, but on a Little Endian machine, both ntohl() and htonl() swap bytes around. On a Big Endian machine, both ntohl() and htonl() do nothing.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cpjust View Post
    Yes, but on a Little Endian machine, both ntohl() and htonl() swap bytes around. On a Big Endian machine, both ntohl() and htonl() do nothing.
    Right -- ntohl and htonl both do the same thing as each other (assuming consistent bytes) -- but they don't necessarily do the same thing as OP's code. Looking back, your point was probably that ntohl and htonl do the same thing regardless, so there's no need to "choose". I'm pretty sure my point was that yes, sometimes you have to type "ntohl" and sometimes you have to type "htonl", but (1) why reinvent the wheel and (2) it's nice if you can tell just by looking why you're flipping something around.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Yeah, having two names is probably just for convenience. It makes it easier to read code. It seems like they could have just had one function, but maybe coming up with a good name would be a problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM