byteordr.c

来自「算断裂的」· C语言 代码 · 共 73 行

C
73
字号
/*
 * Example: Use the standard library version instead, if you have one.
 */

/* switch the order of the bytes in a long integer */
long ntohl(i_in)
long i_in;
{
	long i_out;
	register unsigned char *inptr, *outptr;

	inptr = (unsigned char *) &i_in;
	outptr = (unsigned char *) &i_out;

	outptr[3] = inptr[0];
	outptr[2] = inptr[1];
	outptr[1] = inptr[2];
	outptr[0] = inptr[3];

	return(i_out);
}

/* switch the order of the bytes in a long integer */
long htonl(i_in)
long i_in;
{
	long i_out;
	register unsigned char *inptr, *outptr;

	inptr = (unsigned char *) &i_in;
	outptr = (unsigned char *) &i_out;

	outptr[3] = inptr[0];
	outptr[2] = inptr[1];
	outptr[1] = inptr[2];
	outptr[0] = inptr[3];

	return(i_out);
}


/* switch the order of the bytes in a short integer */
short ntohs(i_in)
short i_in;
{
	short i_out;
	register unsigned char *inptr, *outptr;

	inptr = (unsigned char *) &i_in;
	outptr = (unsigned char *) &i_out;

	outptr[1] = inptr[0];
	outptr[0] = inptr[1];

	return(i_out);
}

/* switch the order of the bytes in a short integer */
short htons(i_in)
short i_in;
{
	short i_out;
	register unsigned char *inptr, *outptr;

	inptr = (unsigned char *) &i_in;
	outptr = (unsigned char *) &i_out;

	outptr[1] = inptr[0];
	outptr[0] = inptr[1];

	return(i_out);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?