在C中将十六进制转换为浮点数,反之亦然 [英] Convert a hexadecimal to a float and viceversa in C

查看:29
本文介绍了在C中将十六进制转换为浮点数,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一种算法来将十六进制数转换为浮点数,反之亦然.这是作业的一部分,程序应该接收以0x"开头的 8 位十六进制数或浮点数,并使用计算机的内置 IEEE 754 功能"将数字转换为十六进制或浮点数.代码应该是 C 语言.

I'm trying to write an algorithm to convert a hexadecimal number to a floating point number and vice versa. This is part of an assignment, the program is supposed to receive either an 8 digit hexadecimal number that starts with '0x' or a floating point number, and using the computer's 'built in IEEE 754 capabilities' convert the number to hex or float. The code should be in C.

我的方法如下.将用户的输入存储为字符串.将输入的字符数组传递给一个方法,该方法通过检查数组的前两个位置来检查它是否是十六进制数,这两个位置应该是 0 和 X(假设所有十六进制数都以这种方式传递).如果是这种情况,那么我会检查十六进制是否采用适当的形式,即没有太多超出 0-15 范围的数字或数字(也考虑 ABCDEF 约定).

My approach is the following. Store the input by the user as a string. Pass the input char array to a method that checks if it's a hexadecimal number by checking the first two positions of the array, which should be 0 and X (assume all hex numbers are passed this way). If this is the case, then I check that the hex is in the appropriate form, i.e not too many digits or numbers outside the 0-15 range (also considering the ABCDEF convention).

所以,我的问题是,我不知道如何将这个数组转回一个可以用来转换为浮点数的数字.我想过用 sscanf 把它变回浮点数,但我对 C 的了解不够,也没有在网上找到任何好的来源

So, my problem is, I don't know how to turn this array back into a number that I can use to convert to floating point. I thought about using sscanf to turn it back into a float but I don't know enough C and haven't found any good sources online

有人能指出我正确的方向吗?我还考虑将用户输入同时存储为字符串和数字,但我不确定这是否可行.

Can somebody point me in the right direction? I also thought about storing the user input both as a string and a number at the same time, but I am not sure if this is possible.

这里是两个表单检查函数和不完整的main方法:

Here are the two form checking functions and the incomplete main method:

int is_hex(char arr[])
{
    if (arr[0] == '0' && (arr[1] == 'x' || arr[1] == 'X')) {
        return 1;
    }
    return 0;
}

int check_if_good(char arr[])
{
    if (is_hex(arr)) {
        int len = strlen(arr);
        for (int i = 2; i < len; i++) {
            if ((arr[i] > 'f' && arr[i] <= 'z') || (arr[i] > 'F' && arr[i] <= 'Z')) {
                return 0;
            }
        }
    }

    return 1;
}

int main(int argc, const char * argv[])
{
    float x;
    char input[30];
    scanf("%s", input);
    printf("%s", input);
    return 0;
}

如果有人也能告诉我 '%x' 的基本含义,我将不胜感激.据我了解,它可以让我以十六进制形式收集数字,但我可以将它们存储在整数和浮点数中吗?非常感谢您的帮助.

If somebody could also tell me the basics of what '%x' means i would really appreciate it. From what I understand it lets me collect numbers in hexadecimal form, but can i store them in ints and floats? Thank you very much for your help.

--floris 的额外代码.我试图在不使用任何额外库的情况下解决此问题,因此您无法使用 stdint 库.

-- Extra code for floris. I'm trying to solve this problem without using any extra libraries, so you can't use the stdint lib.

char input[30];
scanf("%s", input);
if(check_if_good(input)){  //Ignore this if-statement.
    printf("Input is in hex form");
}
int num = convert_hex_string(input); // this function works perfectly. It return the int form of the hex.
double f = *((double*)&num);
printf("%f\n", f);

推荐答案

如果你真的想知道一个特定的十六进制字符串代表的是什么浮点数,你可以把字符串转换成整数,然后看地址伪装它包含一个浮点数.这是它的工作原理:

If you actually want to know what floating point number is represented by a particular hex string, you can convert the string to an integer, then look at the address pretending that it contains a floating point number. Here's how that would work:

#include <stdint.h>
#include <stdio.h>

int main(void) {
  char myString[]="0x3f9d70a4";
  uint32_t num;
  float f;
  sscanf(myString, "%x", &num);  // assuming you checked input
  f = *((float*)&num);
  printf("the hexadecimal 0x%08x becomes %.3f as a float\n", num, f);
}

这产生作为输出

the hexadecimal 0x3f9d70a4 becomes 1.230 as a float

正如预期的那样.有关详细信息,请参阅我对您关于相关主题的其他问题的回答.

As expected. See my answer to your other question about a related topic for more details.

也应该很容易看出如何反向执行上述操作 - 从浮点数开始,并获得十六进制表示.

It should also be easy to see how you can do the above in reverse - start with a floating point number, and get the hexadecimal representation.

这篇关于在C中将十六进制转换为浮点数,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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