hexa到dec转换 [英] hexa to dec conversion

查看:101
本文介绍了hexa到dec转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
  int dechex=0;
  int hexdec;
  printf("Please enter your input in Hexadecimal form: ");

  hexdec = getchar();

  while(hexdec != '\n')
  {
    if('0' <= hexdec && hexdec <= '9')
    {
      dechex = dechex * 16;
      dechex = dechex + (hexdec - '0');
      break;
    }
    else if('A' <= hexdec && hexdec <= 'F')
    {
      dechex = dechex * 16;
      dechex = dechex + (hexdec - 'A')+10;
      break;
    }
    else if('a' <= hexdec && hexdec <= 'f')
    {
      dechex = dechex * 16;
      dechex = dechex + (hexdec - 'a')+10;
      break;
    }
    else
    {
      dechex=0;
    }

    hexdec = getchar();
  }
  
  printf("\nYour input number in decimal form is %d",dechex);
}





请帮帮我们我们错了o / p ....



please help me guys am getting wrong o/p ....

thanku but output is giving
input-5 ;output-5
input-fff;output-15
input-aa;output-10
why this happens







请帮助大家




please help guys

推荐答案

您的 dechex 变量未初始化。因此它在执行程序时可能包含任何起始值。



因此将其初始化为零:

Your dechex variable is not initialised. So it may contain any start value when executing your program.

So initialise it with zero:
void main()
{
  int dechex = 0;
  // ...
}


使用stdlib的 strtol [ ^ ]功能...

1.获取你的十六进制字符串

2.在十六进制字符串的begginnig添加'0x'

3.调用strtol
Use stdlib's strtol[^] function...
1. Get your hex-string
2. Add '0x' at the begginnig of the hex-string
3. Call strtol


只是我们解决了我的问题通过删除代码中的中断自我.... lolll
just f guys i solved it my self by removing breaks in code....lolll


这篇关于hexa到dec转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆