c语言long的作用,用C语言实现函数,功能:把十进制数(long型)以十六进制形

2

#include

#include

#include

#include

using namespace std;

void ConvertLongToHexString(long Input,char* output)

{

char *dest=output;

assert(output!=NULL);

while(Input)

{

if(Input%16>10)

{

*dest++='A'+Input%16-10;

}

else

{

*dest++=Input%16+'0';

}

Input=Input/16;

}

*dest++='x';

*dest++='0';

*dest='\0';

int i=0,j=strlen(output)-1;

while(i

{

char temp;

temp=output[i];

output[i]=output[j];

output[j]=temp;

i++;

j--;

}

}

int main()

{

char *output=(char *)malloc(1);

ConvertLongToHexString(10000000,output);

cout<

return 0;

}

发表于 2015-04-24 21:01:29

回复(0)

1

void ConvertLongToHexString(long Input,char* output) {

static char HexStr[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

unsigned char * p = (unsigned char *)(&Input);

for ( int i=0;i

output[i*2]   = HexStr[p[i]>>4];

output[i*2+1] = HexStr[(p[i]<<4)>>4];

}

}

发表于 2015-05-13 15:17:54

回复(1)

1

1.我的思路是: 把这个long型(32bit)的数据每次右移4位 ,这4位数据和

1111进行相与,结果放入字符数组的末尾(也可以用栈来实现)

int _tmain(int argc, _TCHAR* argv[])

{

char out[11] = {0};

ConvertLongToHexString(1120, out);

printf("%s",out);

system("pause");

return 0;

}

void ConvertLongToHexString(long Input,char* output)

{

long in = Input;

for(int i = 0; i < 8; i++)

{

int temp = in & 0x0000000F;

output[9-i] = (temp < 10) ? ('0' +temp) : ('A' + temp -10);

in = in >> 4; //右移4位

}

output[10] = '\0';

output[0] = '0';

output[1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值