我想获得以下小数值
51043465443420856213到Arduino中的二进制文件。
结果应该是这个。
10 1100 0100 0101 1110 1101 0001 0110 0001 1000 0100 0000 0110 1101 1111 1001 0101
答案 0 :(得分:1)
下面是一个代码模板,可以帮助您前进,因为我不想给您带来所有乐趣。
void print_bin(char *s) {
// Keep track of carry from previous digit - what should this be initialized to?
int carry = ...;
// Divide the "number string" by 2, one digit/character at a time
// for each array element in `s` starting with the first character
for (... ; ... ; ...) {
// Extract the string element (character), covert to a decimal digit & add 10x carry
int digit = ...
// Divide digit by 2 and then save as a character
array element = ...
// Mod the digit by 2 as the new carry
carry = ...
}
// If first digit is a `0`, then move `s` to the next position.
if (*s == '0') {
...
}
// If `s` is not at the end, recursively call this routine on s
if (...) {
print_bin(s);
}
// Since the more significant digits have been printed, now print
// this digit (which is "mod 2" of the original "number string") as a character
fputc('0' + carry, stdout);
}
int main(void) {
char s[] = "51043465443420856213";
print_bin(s);
}
结果
101100010001011110110100010110000110000100000001101101111110010101
答案 1 :(得分:0)
显然,您的值不会被存储为无符号长整数c程序,因为它大于64位。 Arduino有python。这是一个python函数,可以执行您想要的操作。该示例使用80位。如果你真的想要它们,你可以添加空格。
def int2bin(n, count=24):
"""returns the binary of integer n, using count number of digits"""
return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])
print int2bin(51043465443420856213, 80)
输出:
00000000000000101100010001011110110100010110000110000100000001101101111110010101