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

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

问题描述

我试图写一个算法为十六进制数转换为浮点数,反之亦然。这是一个任务的一部分,该计划应该接受,要么以0X或浮点数,并使用计算机的内置的IEEE 754的能力转换为十六进制或浮数开头的8位十六进制数。在code应该在C

我的做法如下。由用户作为字符串存储输入。输入字符数组传递给来检查,如果它是通过检查阵列,这应该是0和X的前两个位置的十六进制数的方法(假设所有的十六进制数传递这种方式)。如果是这样的话,那么我检查十六进制是在适当的形式,即没有太多的数字或数字0-15范围内(也考虑ABCDEF约定)。外

所以,我的问题是,我不知道该怎么把这个数组回了一些,我可以使用转换为浮点。我想过使用sscanf的把它放回一个浮动,但我不知道够不够C和没有发现任何的好来源网上

有人能指出我在正确的方向?我也想过存储用户输入既作为一个字符串和一些在同一时间,但我不知道这是可能的。

下面是两种形式的检查功能和不完整的主要方法:

  INT is_hex(字符ARR [])
{
    如果(改编[0] =='0'和;及(改编[1] =='X'||改编[1] =='X')){
        返回1;
    }
    返回0;
}INT check_if_good(ARR的char [])
{
    如果(is_hex(ARR)){
        INT LEN = strlen的(ARR);
        的for(int i = 2; I< LEN,我++){
            如果((ARR [I]>'F'和;&安培;常用3 [1] - ='Z')||(ARR [I]>'F'和;&安培;常用3 [1] - = Z)){
                返回0;
            }
        }
    }    返回1;
}INT主(INT ARGC,为const char * argv的[])
{
    浮X;
    字符输入[30];
    scanf函数(%S,输入);
    的printf(%S,输入);
    返回0;
}

如果有人也可以告诉我是什么'%X'的基本意思,我真的AP preciate它。从我的理解它让我收集十六进制格式的数字,但我可以把它们存储在整型和浮点型?非常感谢你的帮助。

- 编辑:额外的code为弗洛里斯。我想在不使用任何额外的库来解决这个问题,所以你不能使用stdint库。

 字符输入[30];
scanf函数(%S,输入);
如果(check_if_good(输入)){//忽略此if语句。
    的printf(输入十六进制格式);
}
INT NUM = convert_hex_string(输入); //这个功能完美。它返回十六进制的整型形式。
双F = *((*双)试验#);
的printf(%F \\ N,F);


解决方案

如果你真的想知道什么浮点数再由一个特定的十六进制字符串psented $ P $,可以将字符串转换为整数转换,再看看在它包含一个浮点数的地址pretending。下面是如何将工作:

 的#include< stdint.h>
#包括LT&;&stdio.h中GT;诠释主要(无效){
  焦炭的myString [] =0x3f9d70a​​4;
  uint32_t的NUM;
  浮F;
  的sscanf(myString的,%X,试验#); //假设你输入检查
  F = *((*浮动)试验#);
  的printf(十六进制为0x%08X变为%.3f为float \\ n,NUM,F);
}

这将产生作为输出

 十六进制0x3f9d70a​​4变为1.230为float

如所预期的。请参见我的回答您的其他问题有关的详细信息相关主题

这也应该是很容易看到你如何能做到上述反向 - 开始一个浮点数,并获得十六进制再presentation

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.

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).

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.

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;
}

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.

--Edit: 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);
}

This produces as output

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天全站免登陆