Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Host byte order in a computer system is either little endian or big endian depen

ID: 3550528 • Letter: H

Question

Host byte order in a computer system is either little endian or big endian depending on the computer architecture. For example in Intel architecture computers the host byte order is little endian , while in Motorola computers , the host byte order is big endian. Network byte order on the other hand is always big endian. This is necessary to make sure the systems from different manufacturers are compatible on the network. uint32_t fromHosttoNetwork(uint32_t hostlong)

uint32_t fromNetworktoHost(uint32_t netlong)
are two functions that are desired to convert from host byte order to network byte order or vice versa.

The fromHosttoNetwork () function converts the unsigned integer hostlong from host byte order to network byte order.

The fromNetworktoHost () function converts the unsigned integer netlong from network byte order to
host byte order.

Write a function fromHosttoNetwork in C that will accept an integer variable and covert it from host byte order to network byte order.
Write a function fromNetworktoHost in C that will accept an integer in network byte order and return an integer in host byte order.

Explanation / Answer

#define INTEL /* to identify the architecture, comment this line if MOTOROLA architecture */


uint32_t fromNetworktoHost(uint32_t netlong)

{

#if defined INTEL

/* network is big endian and host(intel) is little endian */

uint32_t return_value;

uint32_t temp;

uint32_t i=0;

temp = netlong;

/*store the bits in the reverse order */

for(i=0;i<32;i++)

{

return_value = temp & 0xFFFFFFFF;

return_value = return_value > 1;

temp = netlong < 1;

}

return (returnvalue);

#else

/* network is big endian and host(motorola) also big endian noting to do */

return(netlong);

#endif

}


uint32_t fromHosttoNetwork(uint32_t hostlong)

{

/* will be same as the above function */

#if defined INTEL

/* network is big endian and host(intel) is little endian */

uint32_t return_value;

uint32_t temp;

uint32_t i=0;

temp = netlong;

/*store the bits in the reverse order */

for(i=0;i<32;i++)

{

return_value = temp & 0xFFFFFFFF;

return_value = return_value > 1;

temp = netlong < 1;

}

return (returnvalue);

#else

/* network is big endian and host(motorola) also big endian noting to do */

return(netlong);

#endif

}