admin 管理员组文章数量: 1184232
2024年12月21日发(作者:会用python的工资)
.
package 2decimal;
import p;
publicclass MainEntry {
publicstaticvoid main(String[] args) {
// 初始化16进制数据键值对
HashMap
HashMap
HashMap
HashMap
for(int i = 0; i < 10; i++) {
(i, (char) ('0'+i));
((char) ('0'+i), i);
}
for(int i = 0; i < 6; i++) {
(10+i, (char) ('A'+i));
((char) ('A'+i), 10+i);
}
// 调用功能接口
hexToDecimal("ECABFFFED1234D7897685DC", hmI2C,
hmC2I);
}
/**
* 实现功能函数
*/
privatestaticvoid hexToDecimal(String
hexString,
HashMap
HashMap
String result = "0"; // 初始化结果值
for(int i = 0; i < ()-1 ; i++)
{
1页
.
if (i == 0) {
result =
additional((((i))).
toString(),
"0", hmI2C, hmC2I);
result =
decimalStrMultiple16(result, hmI2C, hmC2I);
} else {
result = additional(result,
(((i))).toString(),
hmI2C, hmC2I);
result =
decimalStrMultiple16(result, hmI2C, hmC2I);
}
}
result = additional(result,
(((
()-1))).toString(),
hmI2C, hmC2I);
n("result = "+result);
}
/**
* 乘法:
* 被乘数
*/
privatestatic String
decimalStrMultiple16(String decimalString,
HashMap
HashMap
StringBuffer sb = new StringBuffer();
carryAndUnit cu = null;
for (int i = ()-1; i >=0;
2页
.
i--) {
if (i == ()-1) {
cu =
multipleOne((i),
0, hmI2C, hmC2I);
} else {
cu =
multipleOne((i),
cu.c, hmI2C, hmC2I);
}
(0, cu.u);
}
if (cu.c > 0) {
(0, cu.c);
}
return ng();
}
/**
* 一位乘法:
* 被乘数
* 进位
* 直接乘了16,是因为16进制转换为10进制数
*/
privatestatic carryAndUnit multipleOne(char a,
int c,
HashMap
HashMap
int multiplicand = (a);
int multiplier = 16;
int carry = c;
int result = multiplicand*multiplier+carry;
carryAndUnit cu =
newcarryAndUnit(result/10,
3页
.
(result%10));
return cu;
}
/**
* 加法:
* 被加数
* 加数
*/
privatestatic String additional(String
decimalResult, String addend,
HashMap
HashMap
StringBuffer sb = new StringBuffer();
carryAndUnit cu = null;
if (() == 1) {
for(int i = ()-1 ; i >= 0;
i--) {
if (i == ()-1) {
cu =
additionalOne((i),
(0), 0,
hmI2C, hmC2I);
} else {
cu =
additionalOne((i),
'0', cu.c, hmI2C, hmC2I);
}
(0, cu.u);
}
if(cu.c > 0) {
(0, cu.c);
}
} elseif (() == 2) {
4页
.
for(int i = ()-1 ; i >= 0;
i--) {
if (i == ()-1) {
cu =
additionalOne((i),
(1), 0,
hmI2C, hmC2I);
} else {
cu =
additionalOne((i),
'0', cu.c, hmI2C, hmC2I);
}
(0, cu.u);
}
if(cu.c > 0) {
(0, cu.c);
}
String tempStr = ng();
(0, ());
(0,
(()-1));
for(int i = ()-2 ; i >= 0; i--)
{
if (i == ()-2) {
cu =
additionalOne((i),
(0), 0,
hmI2C, hmC2I);
} else {
cu =
additionalOne((i),
'0', cu.c, hmI2C, hmC2I);
}
(0, cu.u);
}
5页
.
if (() == 1) {
(0, (0));
}
if(cu.c > 0) {
(0, cu.c);
}
} else {
n("addend
error");
}
return ng();
}
/**
* 一位加法:
* 被加数
* 加数
* 之前的进位
*/
privatestatic carryAndUnit additionalOne(char
a, char b, int c,
HashMap
HashMap
int augend = (a);
int addend = (b);
int carry = c;
int result = augend+addend+carry;
carryAndUnit cu =
newcarryAndUnit(result/10,
(result%10));
return cu;
}
/**
6页
.
* 进位和最后一位值数据模型
*/
privatestaticclass carryAndUnit {
publicintc;
publiccharu;
public carryAndUnit(int c, char u){
this.c = c;
this.u
}
}
}
7页
= u;
版权声明:本文标题:(16进制数据转换成10进制)HexToDecimal 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1734880506a1603769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论