2015-09-30 81 views
0

我有一个十六进制数字。字符串,我想将其转换为浮动。任何人都可以提出任何想法我在这个论坛搜索了这里,我找到了一篇文章提及下面的解决方案。我明白,第一个字符串转换为十六进制数,然后十六进制数转换为浮点数。但我不明白float_data = *((float*)&hexvalue);是怎么发生的。而且该代码也不适用于此方法。将十六进制转换为浮点数C

订正1:我想我在这里没有提到。非常遗憾。这个十六进制值39 39 2e 30 35转换为ascii时,则给出99.05。我需要这个ascii作为float no。 以下是我的代码:

#include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 
int main() 
{ 
    char *my_data; 
    double float_data; 
    unsigned int hexvalue; 
    my_data = malloc (100); 
    my_data =strcpy(my_data, "39 39 2e 30 35"); 
    sscanf (my_data, "%x", &hexvalue); 
    float_data = *((float*)&hexvalue); 
    printf ("the floating n. is %f", float_data); 
    free (my_data); 
    return 0; 
} 
+0

预期产量是多少? –

+1

这很好奇;有少数系统的浮点数是5字节长。你应该做什么?小数点在哪里?将单个字节转换为“float”不太可能产生有用的值。 'malloc()'和'strcpy()'似乎是多余的。也许你已经剥离了很多代码并将其提炼出来;内存管理行是多余的,但可能更易于解释(MCVE - [如何创建最小,完整和可验证的示例?](http://stackoverflow.com/help/mcve))。 –

回答

2

扫描,追加空字符,然后使用atof()strtod()转换为float

int main(void) { 
    unsigned char uc[5+1]; 
    sscanf("39 39 2e 30 35", "%hhx%hhx%hhx%hhx%hhx", &uc[0], &uc[1], &uc[2], &uc[3], &uc[4]); 
    uc[5] = 0; 
    float f = atof(uc); 
    printf("%f\n", f); 
    return 0; 
} 

输出

99.050003 

[编辑],因为C99提供,scanf()

hh用于指定后续diouxX,或n转换说明符适用于带有t的参数ype指向signed charunsigned char。 C11dr§7.21.6.211

+0

非常感谢。它的工作.. :) – learningpal

+0

你能解释一下格式说明符'%hhx'代表什么? – learningpal

+0

@learningpal回答已更新。 – chux

1

float_data = *((float*)&hexvalue);认为&hexvalue为指针,以float然后读取从那里&hexvalue点的数据作为float

该代码可能工作,这取决于环境。使用"%hhx"入一个字符数组

#include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 
int main() 
{ 
    char *my_data; 
    double float_data; 
    unsigned int hexvalue[4]; 
    unsigned char floatdata_hex[4]; 
    int i; 
    my_data = malloc (100); 
    strcpy(my_data, "39 39 2e 30 35"); /* the assignment isn't needed */ 
    /* the original code will read only one hex value, not four or five */ 
    sscanf (my_data, "%x%x%x%x", &hexvalue[0], &hexvalue[1], &hexvalue[2], &hexvalue[3]); /* read to int */ 
    for (i = 0; i < 4; i++) floatdata_hex[i] = hexvalue[i]; /* and convert them to char later */ 
    float_data = *((float*)floatdata_hex); 
    /* the value may be too small to display by %f */ 
    printf ("the floating n. is %.15g", float_data); 
    free (my_data); 
    return 0; 
} 
+4

'float_data = *((float *)floatdata_hex);'打破严格的别名规则,因此它是未定义的行为。 – mch

+0

非常感谢@MikeCAT:你的代码有效。对不起,我误解了这个问题。但是你的代码解释了这个概念。 – learningpal

1

该代码将执行新的转换,支持任意长度的输入。

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

double ascii_string_to_float(const char *str) 
{ 
    double ret = 0.0; 
    char *work1; 
    char *work2; 
    char *tok, *out; 
    size_t len = strlen(str); 
    work1 = malloc(len + 1); 
    work2 = malloc(len + 1); 
    if (work1 == NULL || work2 == NULL) 
    { 
     if (work1 != NULL) free(work1); 
     if (work2 != NULL) free(work2); 
     exit(1); 
    } 
    strcpy(work1, str); 
    out = work2; 
    tok = strtok(work1, " "); 
    do { 
     int tmp; 
     sscanf(tok, "%x", &tmp); 
     *(out++) = tmp; 
    } while ((tok = strtok(NULL, " ")) != NULL); 
    *out = '\0'; 
    sscanf(work2, "%lf", &ret); 
    free(work1); 
    free(work2); 
    return ret; 
} 

int main(void) 
{ 
    const char *my_data = "39 39 2e 30 35"; 
    double float_data; 
    float_data = ascii_string_to_float(my_data); 
    printf("the floating n. is %f", float_data); 
    return 0; 
} 

错误检查没有完成,所以请在需要时添加它们。

+0

非常感谢.. :) – learningpal

相关问题