C语言实现二进制与十进制转换

来源:互联网 发布:单片机交通灯设计报告 编辑:程序博客网 时间:2024/05/16 23:56

/*************************************************************************    > File Name: test.cpp    > Author: Damon    > Mail: thydamon@gmail.com     > Created Time: Fri 05 Jun 2015 02:28:03 AM PDT ************************************************************************/#include <stdlib.h>#include <stdio.h>#include <math.h>void rever(char *pIn, char *pOut){char *pTin  = pIn;char *pTout = pOut;int i = 0, j = 0;while (pTin[i] != '*'){i++;}while (i--){pOut[j] = pIn[i];j++;}pOut[j] = '*';}void putArr(char *nIn){int i = 0;while (nIn[i] != '*'){printf("%c ", nIn[i]);i++;}printf("\n");}void TentoTw(const int nIn, char *nOut){int  nTmp  = nIn;char *pTmp = nOut;int  i = 0;while (nTmp){// printf("%d ", nTmp/2);pTmp[i] = nTmp%2 + 48;// printf("%d\n", *pTmp);// pTmp++;i++;nTmp = nTmp/2;}pTmp[i] = '*';}void TwtoTen(char *pIn){char *pTin = pIn;int  i = 0, num = 0, j = 0;while (pTin[j] != '*')j++;//printf("%d\n",j);while (pTin[i] != '*'){printf("%c ",pTin[i]);num = num+(pTin[i] - 48)*pow(2,j-i-1);i++;}printf("\n");printf("%d\n",num);}int main(int argc, char* argv[]){int  in = 0;char str[32] = {0};char fstr[32] = {0};printf("please input a integer:");scanf("%d", &in);TentoTw(in, str);putArr(str);rever(str,fstr);putArr(fstr);TwtoTen(fstr);return 0;}


g++ test.cpp -o test -lm 编译即可。

0 0
原创粉丝点击