VC 数字转化为十六进制字符串

来源:互联网 发布:快递不送货上门 知乎 编辑:程序博客网 时间:2024/04/30 22:50

//#include "stdafx.h"
#include "string"

using namespace std;


#pragma pack(push,__mytest__,1)

union TEST_union8
{
unsigned char c[8];
   
    __int32 x32;
__int64 x64;
double d;


};


#pragma pack(pop,__mytest__)
char   digit[ 17 ] = "0123456789ABCDEF";
string int_to_str16( int x )
{
TEST_union8 u;


int i,j;
char buf[9];
buf[8]=0;
    u.x32 =x ; j=0;
for (i=3;i>=0;i--)
{
   int c =   u.c[i];
   buf[j++] = digit[ c>>4 ];   
   buf[j++] = digit[ c & 0xf ];      
}
return buf;
}
string int64_to_str16( __int64 x )
{
TEST_union8 u;
int i,j;
char buf[17];
buf[16]=0;
    u.x64 =x ; j=0;
for (i=7;i>=0;i--)
{
   int c =   u.c[i];
   buf[j++] = digit[ c>>4 ];   
   buf[j++] = digit[ c & 0xf ];      
}
return buf;
}
string double_to_str16( double x )
{
TEST_union8 u;
int i,j;
char buf[17];
buf[16]=0;
    u.d =x ; j=0;
for (i=7;i>=0;i--)
{
   int c =   u.c[i];
   buf[j++] = digit[ c>>4 ];   
   buf[j++] = digit[ c & 0xf ];      
}
return buf;
}
int main()
{
     
    
printf("/n%s/n", int_to_str16(65).c_str());
    __int64 x64 = 12345678901234567890;
    printf("/n%s/n", int64_to_str16(x64).c_str());
    printf("/n%s/n", double_to_str16( 1.5).c_str());

getchar();

return 0;
}