将字节转换为二进制、将输出附加到字符串、转换为双精度、打印输出...输出中丢失位
所以,我在某些代码上遇到了问题。
我希望这个函数接受一个字节数组(现在用单字节进行测试),将字节转换为二进制,然后将其附加到“1”。用于计算。
例如:
输出:01110000 ----> 1.01110000
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
double calcByteValue(uint8_t data[], int size);
int main() {
uint8_t test[10];
test[0] = 0x0e;
double d = calcByteValue(test, 8);
return 0;
}
double calcByteValue(uint8_t data[], int size) {
int i;
uint8_t bits[21];
char binary[100];
char str[100] = "1.";
for (i = 0;i < size;i++) {
bits[i] = (data[0] >> i) & 1;
if (bits[i] == 0) {
binary[i] = '0';
printf("0(%d)\n", i);
} else {
binary[i] = '1';
printf("1(%d)\n", i);
}
}
strcat(str, binary);
float d = atof(str);
printf("%f\n", d);
return 0;
//return pow(-1, bits[0]) * pow(2, (128-127)) * atof(str));
}
这是我的输出,由于某种原因,它很好地完成了整个循环,但只打印了 6 个原始位,删除了最后几个位。我做错了什么???
0(0)
1(1)
1(2)
1(3)
0(4)
0(5)
0(6)
0(7)
1.011100
So, I am having trouble with some code.
I want this function to take in a byte array(testing with single byte for now), convert the byte into binary and then append it to a "1." to use in a calculation.
ex:
ouput: 01110000 ----> 1.01110000
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
double calcByteValue(uint8_t data[], int size);
int main() {
uint8_t test[10];
test[0] = 0x0e;
double d = calcByteValue(test, 8);
return 0;
}
double calcByteValue(uint8_t data[], int size) {
int i;
uint8_t bits[21];
char binary[100];
char str[100] = "1.";
for (i = 0;i < size;i++) {
bits[i] = (data[0] >> i) & 1;
if (bits[i] == 0) {
binary[i] = '0';
printf("0(%d)\n", i);
} else {
binary[i] = '1';
printf("1(%d)\n", i);
}
}
strcat(str, binary);
float d = atof(str);
printf("%f\n", d);
return 0;
//return pow(-1, bits[0]) * pow(2, (128-127)) * atof(str));
}
Here is my output, for some reason it is going through the whole loop just fine, but only printing 6 of the original bits, knocking off the last couple ones. What am I doing wrong???
0(0)
1(1)
1(2)
1(3)
0(4)
0(5)
0(6)
0(7)
1.011100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一:
你从来没有以 null 终止过
binary
数组。您必须将其用作字符串。binary
在块作用域中定义,未显式初始化的块作用域自动对象具有不确定的值。以下是如何以空值终止数组:
第二:
您以相反的顺序插入位值。把
(data[0] >> 7) & 1
表示binary[0]
、(data[0] >> 6) & 1
表示binary[1]
等等。另外,具有
%f
转换规范的printf
会打印小数点后 6 位数字。如果您想要更多数字(例如 16),您可以像这样指定精度:printf("%.16f\n", d);
您还使用类型
float< /code> 对于对象
d
,如果您发现float
精度不够,可以使用double
类型。First:
You never null terminated the
binary
array. You have to to use it as a string.binary
is defined at block scope, and block scope automatic objects that are not explicitly initialized have an indeterminate value.Here is how to null terminate the array:
Second:
you are inserting the bits values in the reverse order. Put
(data[0] >> 7) & 1
forbinary[0]
,(data[0] >> 6) & 1
forbinary[1]
and so on.Also
printf
with%f
conversion specification prints 6 digits after the decimal point. If you want more digits (e.g., 16), you can specify the precision like this:printf("%.16f\n", d);
You are also using the type
float
for objectd
, if you find you don't have enough precision withfloat
, you can use typedouble
.当你像这样将字符串转换为浮点数时,你总是会失去一些精度。您几乎无法将十进制数精确地表示为浮点数。 1.0111000 是十进制数,而不是二进制数。
When you convert a string to a float like that, you're always going to lose some precision. You can hardly ever express a decimal number as a float precisely. 1.0111000 is a decimal number, not a binary one.