C语言 将双精度值转换为 C 中的字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7462349/
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
Convert double value to a char array in C
提问by Hashan
How do I convert double value to a char array in C?
如何将双精度值转换为 C 中的字符数组?
double a=2.132;
char arr[8];
Is there any way to do this in standard C? Can anyone give any solution?
有没有办法在标准 C 中做到这一点?任何人都可以提供任何解决方案吗?
回答by vmatyi
If you are about to store the double DATA, not the readable representation, then:
如果您要存储双数据,而不是可读表示,则:
#include <string.h>
double a=2.132;
char arr[sizeof(a)];
memcpy(arr,&a,sizeof(a));
回答by James Waldby - jwpat7
To make a character string representing the number in human-readable form, use snprintf(), like in code below.
要以人类可读的形式生成表示数字的字符串,请使用 snprintf(),如下面的代码所示。
To access the bytes of the double, use a union. For example, union u { double d; char arr[8]; }
要访问 double 的字节,请使用联合。例如,union u { double d; 字符 arr[8]; }
However, from your added comment, perhaps you mean to convert a number from characters to double. See the atof() call in code below. The code produces the following 4 output lines:
但是,根据您添加的评论,您可能想将数字从字符转换为双精度数。请参阅下面代码中的 atof() 调用。该代码产生以下 4 行输出:
u.d = 2.132000 u.arr = 75 ffffff93 18 04 56 0e 01 40
res = 2.13200000
u.d = 37.456700 u.arr = ffffffa6 0a 46 25 75 ffffffba 42 40
res = 37.45670000
Code:
代码:
#include <stdio.h>
#include <stdlib.h>
union MyUnion { char arr[8]; double d; };
void printUnion (union MyUnion u) {
int i;
enum { LEN=40 };
char res[LEN];
printf ("u.d = %f u.arr = ", u.d);
for (i=0; i<8; ++i)
printf (" %02x", u.arr[i]);
printf ("\n");
snprintf (res, LEN, "%4.8f", u.d);
printf ("res = %s\n", res);
}
int main(void) {
union MyUnion mu = { .d=2.132 };
printUnion (mu);
mu.d = atof ("37.4567");
printUnion (mu);
return 0;
}
回答by bubbly
In case someone looks at this, you can also try sprintf:
如果有人看到这个,你也可以试试 sprintf:
Example:
例子:
char charray[200];
double num = 11.1111111111111;
sprintf(charray, "%2.13f", num);
回答by Alan Moore
Although I see some answers, I imagine you'd like to see code -- I'll just use snprintf although you might prefer a more secure form:
虽然我看到了一些答案,但我想您希望看到代码——尽管您可能更喜欢更安全的形式,但我将只使用 snprintf:
snprintf(arr, 8, "%2.4f", a);
more here: http://msdn.microsoft.com/en-us/library/2ts7cx93(VS.71).aspx
更多信息:http: //msdn.microsoft.com/en-us/library/2ts7cx93(VS.71).aspx
回答by Mike Sokolov
If what you are asking is how to find out what bytes make up the double value in memory, try this:
如果您要问的是如何找出内存中构成双精度值的字节,请尝试以下操作:
double a=2.132;
double darr[1] = { a };
char *arr = (char*) darr;
although you probably want unsigned char, not char
虽然你可能想要无符号字符,而不是字符
回答by Mohamed Alimoussa
double b=222.215;
双 b=222.215;
String bb=""+b;
char[] tab=new char[bb.length()];
for(int m=0;m<bb.length();m++)
{
tab[i]=bb.charAt(m);
}//un tableau mtn qui contient notre resultat dans un tableua de char

