C语言 如何将十六进制转换为十进制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31079978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:01:12  来源:igfitidea点击:

How to convert Hexadecimal to Decimal?

ccygwinhexdecimal

提问by H2019

I have different hexadecimal data are coming and stored into an integer type register.

我有不同的十六进制数据即将到来并存储到一个整数类型寄存器中。

When I use fprint I can see the following:

当我使用 fprint 时,我可以看到以下内容:

0x3076
0x307c
.
.
.

However, I would like to show a Decimal version of above-mention Hex data as follows.

但是,我想显示上述十六进制数据的十进制版本,如下所示。

12406
12412
.
.
.

In theory, let's say for the first value you do the following to convert it to decimal.

理论上,假设第一个值您执行以下操作将其转换为十进制。

(6* 16^0)+(7 * 16^1)+(0*16^2)+(3*16^3)=6 + 112+ 0 + 12288 = 12406

So IF I have the character-based version of "0x3076" and also, IF I am able to get each single characters 6 - 7 - 0 - 3, I can calculate the decimal amount!

因此,如果我有“0x3076”的基于字符的版本,而且,如果我能够获得每个单个字符 6 - 7 - 0 - 3,我可以计算十进制数量!

So, I decided to divide "3076" by 1000. I was expected to get 3, but I got TWO characters instead! however, if I was able to get 3 for the remainder of "307c" I am not able to get "C". If it was decimal this might work not hex!

所以,我决定将“3076”除以 1000。我原本应该得到 3,但我得到了两个字符!但是,如果我能够为“307c”的其余部分获得 3,我将无法获得“C”。如果它是十进制的,这可能不是十六进制的!

Also, I tried "strtol" command. As I use Cygwin to compile my code, I am not sure where the error is! What did I do wrong?

另外,我尝试了“ strtol”命令。当我使用 Cygwin 编译我的代码时,我不确定错误在哪里!我做错了什么?

So, I just need to find a way to get EACH SINGLE character out of the HEX data!

所以,我只需要找到一种方法来从 HEX 数据中获取每个单个字符!

Any idea please?

请问有什么想法吗?

P.S.

聚苯乙烯

Here is my code in order to help you to give me an idea.

这是我的代码,以帮助您给我一个想法。

   int get_readings(int source, int phase, int max_tries)
{
    uint8_t buf[MAX_IEC1107_MSG_BODY];
    uint8_t inbuf[MAX_IEC1107_MSG_BODY];
    int inlen;
    uint8_t *s;
    int32_t value;
    int status;
    double voltage;
    double current;
    double active_power;
    double reactive_power;
    double apparent_power;
    double power_factor;
    double frequency;

    s = buf;
    *s++ = HOST_CMD_GET_READINGS_PHASE_1 + phase;
    *s++ = 0x00;
    if (max_tries != 1)
        meter_set_max_tries(&emeter[source].meter_msg, max_tries);
    if (meter_exchange(&emeter[source].meter_msg, buf, 2, inbuf, &inlen)
        &&
        inbuf[1] != 0xFF)
    {
        emeter[source].no_response = FALSE;
        s = inbuf;
        /* Get current readings */
        value = (s[3] << 8) | s[2];

        fprint(stderr, "value:" %p\n, value); // this give me HEX code

. . .

. . .

回答by dbush

When reading in a string representing a number in hexadecimal, use strtol()to convert it to a long. Then if you want to print the number in decimal, use printf()with a %dformat specifier.

读入表示十六进制数字的字符串时,使用strtol()将其转换为 long。然后,如果你要打印十进制数,使用printf()%d格式说明。

char num[]="0x3076";
long n = strtol(num, NULL, 16);
printf("n=%ld\n", n);  // prints 12406

Once you read in the strings as longs using strtoland operate on the longs, the math should work itself out.

一旦你读入字符串作为 longs 使用strtol并在 longs 上操作,数学应该自己解决。

回答by BLUEPIXY

unsigned n;
sscanf("0x3076", "%x", &n);
printf("%u\n", n);

回答by H2019

Only with the following line I could solve the issue!

只有使用以下行我才能解决问题!

fprintf(stderr, "Voltage: %.2f\n", value/100.00);

value was shown as HEX as I used to use %p! Then I changed to x! and as I need to have 124.06 not 12406, in other words 2 decimal points, I added .2f!

值显示为十六进制,因为我以前使用 %p! 然后我改成了x!由于我需要 124.06 而不是 12406,换句话说就是 2 个小数点,我添加了 .2f!

Thank you all!

谢谢你们!

回答by Youness Nachid-Idrissi

1) for printing only:

1) 仅用于打印:

#include <stdio.h>
#include <stdlib.h>

int main(){
  int a=0;
  scanf("%x",&a);
  printf("%d\n",a);
  system("pause");
  return 0;
}

2) for calculation use:

2) 计算用途:

#include<stdio.h>
#include<stdlib.h>
int CharToDec(char c){
  if(c>='0' && c<='9') return c-'0';
  if(c>='a' && c<='f') return c-'a'+10;
  if(c>='A' && c<='F') return c-'A'+10;
  return 0;
}
int main(int argc, char *argv[]){
  char H[10];
  char *P;
  int p=1,d=0;
  printf("Entrez Hexadecimale nombre:");
  scanf("%s",H);
  for(P=H;*P;P++);
  for(P--;P>=H;P--){
    d = d + (CharToDec(*P) * p);
    p =  p*16;
  }
  printf("%d\n",d);
system("pause");
return 0
}

回答by Chris Dodd

You are confusing "integer" and "decimal" -- an "integer" is a number stored in a register or variable, while a "decimal" is a string of characters that represent a number in base 10.

您混淆了“整数”和“十进制”——“整数”是存储在寄存器或变量中的数字,而“十进制”是表示以 10 为基数的数字的字符串。

So converting from hexadecimal to decimal is a two-step process. You first convert the hexadecimal to integer (which you appear to have done, with your "In theory" equation). You then convert the integer to decimal.

所以从十六进制转换为十进制是一个两步过程。您首先将十六进制转换为整数(您似乎已经使用“理论上”等式完成了此操作)。然后将整数转换为十进制。

There are lots of ways to do either of these tasks, strtoland scanfwill convert either hexadecimal or decimal to integer. printfcan convert integer to either hexedecimal or decimal. Or you can write your own routines that manipulate characters to do either of these things.

有很多方法可以做到这些任务, strtol并且scanf将十六进制或十进制转换为整数。 printf可以将整数转换为十六进制或十进制。或者,您可以编写自己的例程来操作角色以执行其中任何一项操作。