JAVA小数十六进制表示,在MASM中将一系列小数位转换为十六进制大数

您可以执行算术乘法10并添加带有十六进制字符串的数字 . 从那里你可以组成十进制到十六进制的字符串转换 .

C中的插图:

#include

#include

#include

typedef unsigned uint;

int chhex2val(char ch)

{

if (ch >= '0' && ch <= '9')

return ch - '0';

if (ch >= 'A' && ch <= 'F')

return ch - 'A' + 10;

if (ch >= 'a' && ch <= 'f')

return ch - 'a' + 10;

abort();

return 0;

}

char val2chhex(int v)

{

if (v >= 0 && v < 16)

return "0123456789ABCDEF"[v];

abort();

return '0';

}

// Multiplies a hex string like "17F" by 10 and

// returns a string with the product (e.g. "0EF6").

// The original string isn't modified.

char* mulh10(char* h)

{

size_t l = strlen(h);

char* p = malloc(l + 1 + 1);

size_t i;

uint c = 0;

if (p == NULL)

abort();

p[l + 1] = '\0';

for (i = 0; i < l; i++)

{

c += chhex2val(h[l - 1 - i]) * 10;

p[l - i] = val2chhex(c % 16);

c /= 16;

}

p[0] = val2chhex(c);

return p;

}

// Adds (arithmetically) to a hex string like "17F" a hex/dec digit, e.g. '9'.

// Returns the modified original string (e.g. "188").

char* addhd(char* h, char d)

{

size_t l = strlen(h);

size_t i;

uint c = chhex2val(d);

for (i = 0; c && i < l; i++)

{

c += chhex2val(h[l - 1 - i]);

h[l - 1 - i] = val2chhex(c % 16);

c /= 16;

}

return h;

}

int main(void)

{

char num[] = "17F";

printf("\"17F\" (hex) * 10 = \"%s\" (hex)\n", mulh10(num));

printf("\"17F\" (hex) + '9' = \"%s\" (hex)\n", addhd(num, '9'));

printf("\"65535\" (dec) = \"%s\" (hex)\n",

addhd(mulh10(addhd(mulh10(addhd(mulh10(addhd(mulh10(addhd(mulh10(

"0"), '6')), '5')), '5')), '3')), '5'));

return 0;

}

"17F" (hex) * 10 = "0EF6" (hex)

"17F" (hex) + '9' = "188" (hex)

"65535" (dec) = "00FFFF" (hex)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值