寻求将10进制数转换成8421BCD码的方法

ppyy 2002-03-15 11:08:26
请告诉我计算方法
代码我自己写
谢谢
...全文
1643 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ppyy 2002-03-16
  • 打赏
  • 举报
回复
void Long2Bcd(BYTE *bcd, long lSource, long lByteLen)
{
int i;
long lNumber = lSource;
for(i = lByteLen-1; i >= 0; i--)
{
*(bcd+i) = (((lNumber%100) / 10) << 4) | ((lNumber%100) % 10);
lNumber /= 100;
}
}


这个是某一朋友给的函数
但是我调用也得不到正确结果
ppyy 2002-03-16
  • 打赏
  • 举报
回复
to chaisave(save):
你的程序根本不能运行
chaisave 2002-03-16
  • 打赏
  • 举报
回复
我用的是boost库,你可能没装(装一个吧)。
目的只是把int转为字符串,itoa也可以的说。
ppyy 2002-03-16
  • 打赏
  • 举报
回复
c:\project\test\test.cpp(1) : fatal error C1083: Cannot open include file: 'boost/lexical_cast.hpp': No such file or directory
chaisave 2002-03-16
  • 打赏
  • 举报
回复
这和编译器有关么?
我用的是MSVC6.0。
ppyy 2002-03-16
  • 打赏
  • 举报
回复
to chaisave(save) :你是UNIX下的C++吗?
我是用的VC编译器
chaisave 2002-03-16
  • 打赏
  • 举报
回复
#include <boost/lexical_cast.hpp>
#include <algorithm>
#include <string>
using namespace std;
using namespace boost;

char dest[3];
void ToBCD(char src) {
static size_t index = 0;
dest[index++] = src - '0';
}

int main()
{
string src = lexical_cast<string> (125); //125 for example.
for_each(src.begin(), src.end(), ToBCD);
}
//完了,dest就是那个BCD码了
//不能run,给个理由先!
//再说了,你不是只要算法么?
ywls 2002-03-16
  • 打赏
  • 举报
回复
#include<iostream.h>
class convert{
int a[16],b;
public:
convert(){
for(int i=0;i<16;i++)
a[i]=0;
};
void input();
void cal();
void output();
};
void convert::input(){
cout<<"Please input a number:";
cin>>b;
cout<<"OCT:"<<b<<endl;
}
void convert::cal(){
for(int i=15;i>=0;i--){
if(b==1){a[i]=1;break;}
a[i]=b%2;
b/=2;}
}
void convert::output(){
cout<<"BIN:";
for(int i=0;i<16;i++)
cout<<a[i];
}
void main(){
convert instance;
instance.input();
instance.cal();
instance.output();
}
ywls 2002-03-16
  • 打赏
  • 举报
回复
class convert{
int a[16],b;
public:
convert(){
for(int i=0;i<16;i++)
a[i]=0;
};
void input();
void cal();
void output();
};
void convert::input(){
cout<<"Please input a number:";
cin>>b;
cout<<"OCT:"<<b<<endl;
}
void convert::cal(){
for(int i=15;i>=0;i--){
if(b==1){a[i]=1;break;}
a[i]=b%2;
b/=2;}
}
void convert::output(){
cout<<"BIN:";
for(int i=0;i<16;i++)
cout<<a[i];
}
void main(){
convert instance;
instance.input();
instance.cal();
instance.output();
}
vcshcn 2002-03-15
  • 打赏
  • 举报
回复
两者相等,只不过占位不同,转什么呀
可以移位,在与或等
chaisave 2002-03-15
  • 打赏
  • 举报
回复
char src[LEN];
char dest[LEN];
void ToBCD(char src) {
static size_t index = 0;
dest[index++] = src - '0';
}
ppyy 2002-03-15
  • 打赏
  • 举报
回复
不是转换成16进制啊
而是转换成8421BCD码
是二进制的
ppyy 2002-03-15
  • 打赏
  • 举报
回复
把每个字母减去'0'???怎么减?

chaisave 2002-03-15
  • 打赏
  • 举报
回复
设10进制数为d,BCD码数为b
先把d转为字符串s,
再把每个字母减'0'就是BCD码
for_each(s.begin(), s.end(), ToBCD)
ziqiriying 2002-03-15
  • 打赏
  • 举报
回复
也就是转化成16进制数
使用堆栈
int a = 10;

while( a!= 0 )
{
int temp ;
temp = a%2;
a = a/2;
push( temp );
}

69,382

社区成员

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

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