标签:数据类型转换 格式化输出
void Test()
{
//原则
//字节相同,只作二进制拷贝
//2字节->4字节,根据符号性先将2字节的数据转换成对应的4字节
//4字节->2字节,直接拷贝低位
int i = 0;
{
//INT16 -> UINT16
//UINT16 -> UINT16
}
{
//INT32 -> UINT32
INT32 n1 = 0xffffffff;
UINT32 u1 = n1;//0xffffffff
++i;
}
{
//UINT32 -> INT32
UINT32 u1 = 0xffffffff;
INT32 n1 = u1;//0xffffffff
++i;
}
{
//INT16 -> INT32
INT16 n1 = 0xffff;
INT32 n2 = n1; //0xffffffff, 2字节扩展到4字节,符号保持不变
++i;
}
{
//INT16 -> UINT32
INT16 n1 = 0xffff;
UINT32 u1 = n1; //0xffffffff, INT16->INT32->UINT32
u1 = (UINT32)(n1&0x0000ffff); //0xffff
n1 = 0x1;
u1 = n1;//0x1
++i;
}
{
//UINT16 -> UINT32
UINT16 u1 = 0xffff;
UINT32 u2 = u1;//0x0000ffff,高位补0
++i;
}
{
//UINT16 -> INT32
UINT16 u1 = 0xffff;
INT32 n1 = u1;//0xffff, UINT16 -> UINT32 -> INT32
++i;
}
{
//INT32 -> INT16
INT32 n1 = 0xf0001000;
INT16 n2 = n1; //0x1000,直接截断,取低2字节
//INT32 -> UINT16
n1 = 0xf1234567;
UINT16 u1 = n1; //0x4567,直接截断,取低2字节
++i;
}
{
//UINT32 -> INT16
UINT32 u1 = 0xf123f456;
INT16 n1 = u1; //0x4567,直接截断,取低2字节
//UINT32 -> UINT16
UINT16 u2 = u1; //0x4567,直接截断,取低2字节
++i;
}
}
void Test2()
{
// UINT64 u1 = 0x1200003456000078;
// UINT32 u2= UINT32(u1&(UINT64)0x00000000ffffffff);
// UINT32 u3= UINT32(u1>>32);
// printf("UINT64 is: %0.16llx\n", u1);
// printf("UINT32 low: %0.8x\n", u2);
// printf("UINT32 hi: %0.8x\n", u3);
//%d需要一个INT32(如果不足4字节,转成4字节),转成INT32,而不关心变量本身的数据类型
//%u需要一个UIN32(如果不足4字节,转成4字节),转成UINT32,而不关心变量本身的数据类型
{
//%d/%u + INT32
INT32 n1 = 0xffffffff;
TEA_LOG(LOG_DEBUG, _T("INT32 %d, %u"), n1, n1);
//INT32 -1, 4294967295
}
{
//%d/%u + UINT32
UINT32 u1 = 0xffffffff;
TEA_LOG(LOG_DEBUG, _T("UINT32 %d, %u"), u1, u1);
//UINT32 -1, 4294967295
}
{
//%d/%u + INT16
INT16 n1 = 0xffff;
TEA_LOG(LOG_DEBUG, _T("INT16 %d, %u"), n1, n1);
//INT16 -1, 4294967295
//INT16 -> INT32 -> UINT32
}
{
//%d/%u + UINT16
UINT16 u1 = 0xffffffff;
TEA_LOG(LOG_DEBUG, _T("UINT16 %d, %u"), u1, u1);
//UINT16 65535, 65535
}
}
本文出自 “波波软件QQ:3083279843” 博客,请务必保留此出处http://boyka3721.blog.51cto.com/10568852/1851082
标签:数据类型转换 格式化输出
原文地址:http://boyka3721.blog.51cto.com/10568852/1851082