将双精度转换为字符数组 C++
Convert double to Char Array C++
我正在尝试将 double 转换(并舍入)为 char 数组,而不首先在 double 上使用 std::to_string 进行转换。但是,我收到的是随机记忆文本。我究竟做错了什么?
这是我的代码:
double d = 1.0929998;
d = std::round(d * 100) / 100;
char s[sizeof(d)];
std::memcpy(s,&d,sizeof(d));
结果:
s: q=×£pñ?
预期值:
s: 1.09
您正在将 double
(数字的二进制表示)复制到 char
数组中;这些字节没有理由对应于数字字符。
如果您想在不使用 to_string
的情况下手动转换,您将不得不一次拉出一个数字。 double 不会作为一组字符存储在内存中,因此您不能只是复制内存并希望一切正常。 (有关如何解释双打的更多信息,请参阅 here)。
您正在将 double
的文字字节翻译成 char
。 double
值是二进制表示(通常类似于 IEEE 754) while a char
is a binary representation of a character (usually something based on ASCII)。这两个不兼容。
不幸的是,这意味着您必须进行某种转换过程。您说您不想做的 std::to_string()
,或者更复杂的 std::stringbuf
(无论如何都会在后台调用 std::to_string()
)
先 do this:
std::ostringstream strs;
strs << dbl;
std::string str = strs.str();
这会将双精度 dbl
转换为字符串 str
。
然后你可以把它转换成这样的字符数组:
char *cstr = new char[str.length()+1];
str.copy(cstr, str.length());
cstr[str.length()] = '[=11=]';
// use cstr as needed...
delete[] cstr;
或者,简单地这样:
const char *cstr = str.c_str();
// use cstr as needed...
这没有经过测试,但您可以尝试:
std::string to_string(double x)
{
std::ostringstream ss;
ss << x;
return ss.str();
}
然后您可以将返回的字符串的字符放入一个 char 数组中,如下所示:
std::string str = to_string(doubleValue);
char digits[str.length()];
然后,感谢 Remy Lebeau 的评论,您可以执行以下任一操作:
std::strncpy(digits, to_string(doubleValue).c_str(), sizeof(digits))
或者这个:
to_string(doubleValue).copy(digits, sizeof(digits))
不使用to_string
自行转换的过程可能如下:
char* to_char_array(double num_double, int decimal_place)
{
int num_int = std::round(num_double * pow(10, decimal_place));
int sign = num_int < 0 ? 1 : 0;
num_int = abs(num_int);
if (num_int == 0)
{
char* s = (char*)malloc(decimal_place + 3);
s[0] = '0';
s[1] = '.';
for (int i = 2; i < decimal_place + 2; i++)
s[i] = '0';
s[decimal_place + 2] = '[=10=]';
return s;
}
int digit_count = 1;
int n = num_int;
if (n >= 100000000) { digit_count += 8; n /= 100000000; }
if (n >= 10000) { digit_count += 4; n /= 10000; }
if (n >= 100) { digit_count += 2; n /= 100; }
if (n >= 10) { digit_count++; }
int size = digit_count + 1 + (decimal_place > 0 ? 1 : 0) + sign;
char* s = (char*)malloc(size);
for (int i = 0, integer = num_int; integer != 0; integer /= 10) {
s[size - 2 - i++] = integer % 10 + 48;
if (decimal_place > 0 && i == decimal_place)
s[size - 2 - i++] = '.';
}
s[size - 1] = '[=10=]';
if (sign)
s[0] = '-';
return s;
}
void main()
{
double d = -12.268904;
char* s = to_char_array(d, 3);
cout << "double: " << d << " - char array: " << s << endl;
system("pause");
}
就这样做:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
double a=23.25;
char b[12];
stringstream ss;
ss << a;
ss >> b;
cout << b << endl;
return 0;
}
或者这个:
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
int main()
{
double a=23.25;
char b[12];
stringstream ss ;
ss<<a;
sprintf(b,ss.str().c_str());
cout<<b<<endl;
return 0;
或者这个:
here don't forget to
#include<bits/stdc++.h>
double a=23.25;
char b[12];
stringstream ss ;
ss<<a;
strcpy(b,ss.str().c_str());
cout<<b<<endl;
return 0;
我正在尝试将 double 转换(并舍入)为 char 数组,而不首先在 double 上使用 std::to_string 进行转换。但是,我收到的是随机记忆文本。我究竟做错了什么?
这是我的代码:
double d = 1.0929998;
d = std::round(d * 100) / 100;
char s[sizeof(d)];
std::memcpy(s,&d,sizeof(d));
结果:
s: q=×£pñ?
预期值:
s: 1.09
您正在将 double
(数字的二进制表示)复制到 char
数组中;这些字节没有理由对应于数字字符。
如果您想在不使用 to_string
的情况下手动转换,您将不得不一次拉出一个数字。 double 不会作为一组字符存储在内存中,因此您不能只是复制内存并希望一切正常。 (有关如何解释双打的更多信息,请参阅 here)。
您正在将 double
的文字字节翻译成 char
。 double
值是二进制表示(通常类似于 IEEE 754) while a char
is a binary representation of a character (usually something based on ASCII)。这两个不兼容。
不幸的是,这意味着您必须进行某种转换过程。您说您不想做的 std::to_string()
,或者更复杂的 std::stringbuf
(无论如何都会在后台调用 std::to_string()
)
先 do this:
std::ostringstream strs;
strs << dbl;
std::string str = strs.str();
这会将双精度 dbl
转换为字符串 str
。
然后你可以把它转换成这样的字符数组:
char *cstr = new char[str.length()+1];
str.copy(cstr, str.length());
cstr[str.length()] = '[=11=]';
// use cstr as needed...
delete[] cstr;
或者,简单地这样:
const char *cstr = str.c_str();
// use cstr as needed...
这没有经过测试,但您可以尝试:
std::string to_string(double x)
{
std::ostringstream ss;
ss << x;
return ss.str();
}
然后您可以将返回的字符串的字符放入一个 char 数组中,如下所示:
std::string str = to_string(doubleValue);
char digits[str.length()];
然后,感谢 Remy Lebeau 的评论,您可以执行以下任一操作:
std::strncpy(digits, to_string(doubleValue).c_str(), sizeof(digits))
或者这个:
to_string(doubleValue).copy(digits, sizeof(digits))
不使用to_string
自行转换的过程可能如下:
char* to_char_array(double num_double, int decimal_place)
{
int num_int = std::round(num_double * pow(10, decimal_place));
int sign = num_int < 0 ? 1 : 0;
num_int = abs(num_int);
if (num_int == 0)
{
char* s = (char*)malloc(decimal_place + 3);
s[0] = '0';
s[1] = '.';
for (int i = 2; i < decimal_place + 2; i++)
s[i] = '0';
s[decimal_place + 2] = '[=10=]';
return s;
}
int digit_count = 1;
int n = num_int;
if (n >= 100000000) { digit_count += 8; n /= 100000000; }
if (n >= 10000) { digit_count += 4; n /= 10000; }
if (n >= 100) { digit_count += 2; n /= 100; }
if (n >= 10) { digit_count++; }
int size = digit_count + 1 + (decimal_place > 0 ? 1 : 0) + sign;
char* s = (char*)malloc(size);
for (int i = 0, integer = num_int; integer != 0; integer /= 10) {
s[size - 2 - i++] = integer % 10 + 48;
if (decimal_place > 0 && i == decimal_place)
s[size - 2 - i++] = '.';
}
s[size - 1] = '[=10=]';
if (sign)
s[0] = '-';
return s;
}
void main()
{
double d = -12.268904;
char* s = to_char_array(d, 3);
cout << "double: " << d << " - char array: " << s << endl;
system("pause");
}
就这样做:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
double a=23.25;
char b[12];
stringstream ss;
ss << a;
ss >> b;
cout << b << endl;
return 0;
}
或者这个:
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
int main()
{
double a=23.25;
char b[12];
stringstream ss ;
ss<<a;
sprintf(b,ss.str().c_str());
cout<<b<<endl;
return 0;
或者这个:
here don't forget to
#include<bits/stdc++.h>
double a=23.25;
char b[12];
stringstream ss ;
ss<<a;
strcpy(b,ss.str().c_str());
cout<<b<<endl;
return 0;