Thread: Big endian/Little endian conversion

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    Big endian/Little endian conversion

    In writing my first network app, I have hit a wall. That wall's name is Big endian.

    I need a conversion routine to convert back and forth. Is there something builtin to MSVC6? If not, is there a magical bitwise shift routine I can use.

    Also, I need a way of detecting when it needs to be converted, as my client will attatch to various hosts. Some Big endian and some Little endian.
    Best Regards,

    Bonkey

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Try

    htonl() - little to big

    ntohl() - Big to little.......

    There's also a nifty ASM opcode called BSWAP which does this

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I can't find those functions in MSVC6.

    According to msdn CSocket handles this for you. News to me, since I derived from CSocket and am still having the problem.
    Best Regards,

    Bonkey

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Try winsock2.h

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I am not having any luck. Here is my code:


    Code:
    void CTelnetSocket::OnReceive( int nErrorCode )
    {
    	char Buf[251];
    
    	Receive(Buf,250);
    
    	char *string=new char[255];
    
    	ltoa((ntohl(atol(Buf))),string,10);
    	ltoa((htonl(atol(Buf))),string,10);
    
    	strcpy(m_bBuf,string);
    	
    	delete [] string;
    
    	if(m_bBuf == "")
    		return;
    
    	ReadData=true;
     }
    I keep getting NULL values in c.
    Last edited by bonkey; 09-25-2002 at 11:06 AM.
    Best Regards,

    Bonkey

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Make sure you import the ws_2 library file.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Big Endian & Little Endian
    By swaugh in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 11:25 PM
  3. Big Endian Little Endian Complex- Converting Characters
    By bd02eagle in forum C Programming
    Replies: 3
    Last Post: 07-11-2006, 01:01 AM
  4. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM
  5. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM