c語言算法(把16進制轉換成10進制)..覺得好難阿.\help me!!!!分不夠再加.....

piziliu2003 2005-01-14 04:08:06
做一個.c語言算法函數把16進制轉換成10進制.(十六制不限制長度.的喔)最好能提供用tc 2.0編譯的源代碼.
謝謝.大蝦們.
...全文
741 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
r_swordsman 2005-01-18
  • 打赏
  • 举报
回复
回复人: piziliu2003(眼淚咖啡) ( ) 信誉:100 2005-01-17 20:02:00 得分: 0


. r_swordsman 分是少了點.想要分就說一聲.我馬上再開一個貼.. 真是太感謝了...


说实话~~~~~分是少了那么一点点~~~~~~~~


呵呵~~~~~~`开玩笑啦~~~~能帮助你我也好高兴咖~~~~~~`
r_swordsman 2005-01-17
  • 打赏
  • 举报
回复
#include "stdafx.h"

class fc
{
public:
int a; ////////////// <---------- 注意PUBLIC~~~要PRIVATE的话~~你要提供设置和访问的方法
public:
fc(){ a = 10; }
friend int cg(fc *); // <<<-----------友元函数的声明
fc operator +(fc);
};

int cg(fc *f) // <<<-----------友元函数的定义
{
return f->a += 10;
}
fc fc::operator +(fc f)
{
fc t;
t.a = a + f.a; //////// +!!!!!!!
return t;
}
void main()
{
fc f;// fc::a = 0;
fc t = f + f;
int r = cg(&f);// r = fc::a = 10;
r = cg(&f);// r = fc::a = 20;
}


///////
你自己设计类的时候要考虑怎么低LONG和高LONG相加~~~然后底LONG向高LONG的进位~~~~~~~`
r_swordsman 2005-01-17
  • 打赏
  • 举报
回复
TO : r_swordsman
重载远算符能不能給各例子參考呢?
===========================

比如一个DDLONG类
DDLONG DDLONG::operator +(DDLONG d) ////// 上一个错误~~~不是FC是DDLONG~~贴的时候没改过来
{
DDLONG t;
....
return t;
}

表达式:
A=B+C
其中
A是反回直
B就是当前对象
C就是参数
r_swordsman 2005-01-17
  • 打赏
  • 举报
回复
TO : r_swordsman
重载远算符能不能給各例子參考呢?
===========================

比如一个DDLONG类
DDLONG fc::operator +(DDLONG d)
{
DDLONG t;
....
return t;
}

表达式:
A=B+C
其中
A是反回直
B就是当前对象
C就是参数
piziliu2003 2005-01-17
  • 打赏
  • 举报
回复
. r_swordsman 分是少了點.想要分就說一聲.我馬上再開一個貼.. 真是太感謝了...
piziliu2003 2005-01-17
  • 打赏
  • 举报
回复
Thanks. r_swordsman(取什么样的昵称才有个性又能显示出来而且不能太长?)
aladar(深蓝)
pcboyxhy(-273.15℃
huangyang88(中国-必胜)
r_swordsman 2005-01-15
  • 打赏
  • 举报
回复
把16进制字符串转换成long类型~~~
long l = strtol("00ABCEDff", NULL, 16); // l = 0x0abcedff
aladar 2005-01-15
  • 打赏
  • 举报
回复
长度由MAX_LEN定,只要十进制结果小于3*MAX_LEN十进制位就行。
没装tc2, 应该可以的,我假设这里的int都是16位的,楼主编译一下试试

没做严格测试。。。只是试了几个数都可以
C2A56E3B541B69E6=14025737815608486374

#include <stdio.h>

#define MAX_LEN 10

void mul16(int* num)
{
int i;
for(i=0; i<MAX_LEN; i++)
num[i]*=16;

for(i=0; i<MAX_LEN; i++) {
if(num[i]>=1000 && i!=MAX_LEN-1) { //后面半句防溢出
num[i+1]+=num[i]/1000;
num[i]%=1000;
}
}
}

void add(int* big_num, int num)
{
int i;
big_num[0]+=num;

for(i=0; i<MAX_LEN; i++) {
if(big_num[i]>=1000 && i!=MAX_LEN-1) {
big_num[i+1]+=big_num[i]/1000;
big_num[i]%=1000;
}
}
}

void htoi(char* hex, int hlen, int* dec)
{
int i;
for(i=0; i<MAX_LEN; i++)
dec[i]=0;

for(i=0; i<hlen; i++) {
if(hex[i]>='a' && hex[i]<='f')
hex[i]+='A'-'a'; //转换大小写
}

for(i=0; i<hlen; i++) {
mul16(dec);
if(hex[i]>='A' && hex[i]<='F')
add(dec, hex[i]-'A'+10);
else
add(dec, hex[i]-'0');
}
}

void print(int* num)
{
int i;

for(i=0;i<MAX_LEN;i++)
printf("%03d", num[MAX_LEN-i-1]);

printf("\n");
}

int main()
{
int num[MAX_LEN]={0};
char buf[20]="C2A56E3B541B69E6";
htoi(buf, 16, num);
print(num);
return 0;
}
aladar 2005-01-15
  • 打赏
  • 举报
回复
我来写个不限长度的,稍等
pcboyxhy 2005-01-15
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

long htoi(char *str);

int main(int argc, char *argv[])
{
char s[100];
cin>>s;
cout<<endl<<htoi(s);
system("PAUSE");
return 0;
}

long htoi(char *str)
{
long i=0, j, dec=0, t;
j=strlen(str);
if(*str=='0') i=2;
while(i<j)
{
dec<<=4;
t=*(str+i++);
if(t<58) t-=48;
if(t>64&&t<71) t-=55;
if(t>96&&t<103) t-=87;
dec|=t;
}
return dec;
}


piziliu2003 2005-01-15
  • 打赏
  • 举报
回复
TO : r_swordsman
重载远算符能不能給各例子參考呢?
piziliu2003 2005-01-15
  • 打赏
  • 举报
回复
同意. r_swordsman(取什么样的昵称才有个性又能显示出来而且不能太长?)
thanks.!!
r_swordsman 2005-01-15
  • 打赏
  • 举报
回复
to:: r_swordsman(取什么样的昵称才有个性又能显示出来而且不能太长?) ( ) 信誉:
to: pcboyxhy(-273.15℃)
to: huangyang88(中国-必胜)
關鍵是Long型的當到0~4294967295就會溢出的喔.也就是算法只能到 8位16進制.喔..
5555555555555555555555555....
看來還是要想其它辦法... 難道真的要用數組做運算麼..!!!(如somedummy(某人马甲) 所說..
請大蝦幫忙..

=============
一个LONG存储不了~~~
可以用2个LONG~~~
一个做高位~~~一个做低位~~~
还不够~~用~~~4个~~~~
要做+减运算么????????自己重载远算符吧~~~~~~

piziliu2003 2005-01-15
  • 打赏
  • 举报
回复
to:: r_swordsman(取什么样的昵称才有个性又能显示出来而且不能太长?) ( ) 信誉:
to: pcboyxhy(-273.15℃)
to: huangyang88(中国-必胜)
關鍵是Long型的當到0~4294967295就會溢出的喔.也就是算法只能到 8位16進制.喔..
5555555555555555555555555....
看來還是要想其它辦法... 難道真的要用數組做運算麼..!!!(如somedummy(某人马甲) 所說..
請大蝦幫忙..
FePwaw 2005-01-14
  • 打赏
  • 举报
回复
C++中最简单的方法,强,笑出眼泪了。
drizzlecrj 2005-01-14
  • 打赏
  • 举报
回复
//C++中最简单的方法
//史上最短程序
# include<iostream.h>
void main()
{
int a;
cin>>hex>>a;
cout<<dec<<a<<endl;
}
piziliu2003 2005-01-14
  • 打赏
  • 举报
回复
請問個位大蝦還有甚麼好的想法...好的source code.!!小第感激不盡
somedummy 2005-01-14
  • 打赏
  • 举报
回复
12位的话,随便搞一下就OK了,全部采用数组存放,然后模拟整数运算,轻松搞定(和大数组保存阶乘类似),最多中间从字符串转换到整数,然后再从整数数组输出
lovefly_fanny 2005-01-14
  • 打赏
  • 举报
回复
学习~~
csrwgs 2005-01-14
  • 打赏
  • 举报
回复
#include"stdlib"

char *itoa(int value, char *string, int radix);
int atoi(const char *s);
加载更多回复(5)

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧