Beispiel #1
0
signed short NShtonlInt16(signed short s) {
	int i = 0x12345678;
	unsigned char *l = (unsigned char*) &i;
	if (*l == 0x78) {
		s = BigLittleSwap16(s);
	}
	return s;
}
Beispiel #2
0
/*************************************************
* Function: HtoNs
* Description: 模拟htons函数,本机字节序转网络字节序
* Author: cxy 
* Returns: 
* Parameter: 
* History:
*************************************************/
u16 AC_HtoNs(u16 h)
{

       // 若本机为大端,与网络字节序同,直接返回

       // 若本机为小端,转换成大端再返回

       return AC_checkCPUendian() ? h : BigLittleSwap16(h);

}
Beispiel #3
0
uint16 t_ntohs(uint16 n)
{
	return checkCPUendian() ? n : BigLittleSwap16(n);
}
Beispiel #4
0
uint16 t_htons(uint16 h)
{
	return checkCPUendian() ? h : BigLittleSwap16(h);
}
Beispiel #5
0
int main()
{
	printf("%d\n", check_cpu_endian());
	printf("%d\n", check_cpu_endian2());
	printf("%d\n", check_cpu_endian3());
	printf("%d\n", is_bigendian());
	

	printf("sizeof(short int)=%lu\n", sizeof(short int));
	printf("sizeof(int)=%lu\n", sizeof(int));
	printf("sizeof(long int)=%lu\n", sizeof(long int));
	printf("sizeof(long long int)=%lu\n", sizeof(long long int));

	printf("~0=0x%X\n", (uint8_t)~0);
	printf("~0 << 8=0x%X\n", (uint8_t)~0 << 8);
	printf("~0 << 56=0x%lX\n", (uint64_t)((uint8_t)~0) << 56);

	int aa = -3;
	unsigned int bb = aa;
	int cc = bb;
	printf("int %d; uint %u int %d\n", aa, bb, cc);
	/*
	uint32_t dd = UINT32_MAX;
	int32_t ee = dd;
	printf("uintmax %u int %d\n", dd, ee);
	*/

	puts("32-------------------");
	int n1 = 0x12345678;
	unsigned char* pc = (unsigned char*)(&n1);
	printf("cpu order:\n");
	print_endian(pc, 32);

	n1 = htonl(n1);
	printf("htonl order:\n");
	print_endian(pc, 32);

	n1 = BigLittleSwap32(n1);
	printf("BigLittleSwap32 order:\n");
	print_endian(pc, 32);

	puts("64-------------------");

	uint64_t n2 = 0x1A2B3C4D12345678;
	pc = (unsigned char*)(&n2);
	printf("cpu order:\n");
	print_endian(pc, 64);

	n2 = BigLittleSwap64(n2);
	printf("BigLittleSwap64 order:\n");
	print_endian(pc, 64);

	n2 = __bswap_64(n2);
	printf("__bswap_64 order:\n");
	print_endian(pc, 64);

	n2 = LITTLE_ENDIAN_CHANGE(n2, 64);
	printf("LITTLE_ENDIAN_CHANGE64 order:\n");
	print_endian(pc, 64);

	n2 = BIG_ENDIAN_CHANGE(n2, 64);
	printf("BIG_ENDIAN_CHANGE64 order:\n");
	print_endian(pc, 64);


	puts("16-------------------");

	uint16_t n3 = 0x1234;
	pc = (unsigned char*)(&n3);
	printf("cpu order:\n");
	print_endian(pc, 16);

	n3 = BigLittleSwap16(n3);
	printf("BigLittleSwap16 order:\n");
	print_endian(pc, 16);


	n3 = little_endian_chg16(n3);
	printf("little_endian_chg16 order:\n");
	print_endian(pc, 16);

	n3 = LITTLE_ENDIAN_CHANGE(n3, 16);
	printf("LITTLE_ENDIAN_CHANGE16 order:\n");
	print_endian(pc, 16);

	n3 = BIG_ENDIAN_CHANGE(n3, 16);
	printf("BIG_ENDIAN_CHANGE16 order:\n");
	print_endian(pc, 16);


	n3 = stream_ltt_bit16(pc, 16);
	printf("stream_ltt_bit16 order:\n");
	print_endian(pc, 16);

	n3 = stream_big_bit16(pc, 16);
	printf("stream_big_bit16 order:\n");
	print_endian(pc, 16);

	puts("16-------------------");
	uint16_t tn = 0x1234;
	print_endian((unsigned char*)(&tn), 16);

	uint64_t n4 = stream_ltt_bit64((unsigned char*)(&tn), 16);
	pc = (unsigned char*)(&n4);
	printf("stream_ltt_bit64 order:\n");
	print_endian(pc, 64);


	n4 = stream_big_bit64((unsigned char*)(&tn), 16);
	pc = (unsigned char*)(&n4);
	printf("stream_big_bit64 order:\n");
	print_endian(pc, 64);


	puts("32-------------------");	
	char buff[4] = {'\x12', '\x34', '\x56', '\x78'};
	print_endian((unsigned char *)buff, 32);
	const char *p = buff;
	printf("p=%p\n", p);
	int m0 = stream_ltt_bit32(&p, 4);
	printf("p=%p m=0x%X\n", p, m0);

	char buff2[4];
	char *p2 = buff2;
	printf("p=%p\n", p2);
	p2 = bit32_ltt_stream(m0, p2, 4);
	print_endian((unsigned char *)buff2, 32);
	printf("p=%p\n", p2);
	


	return 0;

}
Beispiel #6
0
uint CMessage::GetSerial() const
{
	return BigLittleSwap16(byteToInt(m_Serial,2));
}