c语言socket发送int数据转char数据

2013-04-11 09:25:45


#include<stdio.h>
#include<string.h>
int main()
{
	int count = 4123131;
	char countBuff[1];
	memset( countBuff, 0, 1 );
	memcpy( countBuff, &count, sizeof(count) );

	int recvCount;
	memcpy( &recvCount, countBuff, sizeof(recvCount) );

	printf("%d", recvCount);
}

Another Version

#include<stdio.h>
#include<string.h>
int main()
{
	int count = 4123131;
	char countBuff;
	memset( &countBuff, 0, 1 );
	memcpy( &countBuff, &count, sizeof(count) );

	int recvCount;
	memcpy( &recvCount, &countBuff, sizeof(recvCount) );

	printf("%d", recvCount);
}