在Arduino中从大十进制值转换为二进制
我正在尝试获取以下十进制值 51043465443420856213到Arduino中的二进制 结果应该是这样在Arduino中从大十进制值转换为二进制,arduino,Arduino,我正在尝试获取以下十进制值 51043465443420856213到Arduino中的二进制 结果应该是这样 10 1100 0100 0101 1110 1101 0001 0110 0001 1000 0100 0000 0110 1101 1111 1001 0101 显然,您的值不是以无符号长的c程序形式存储的,因为它大于64位。Arduino有python。下面是一个python函数,它可以实现您想要的功能。该示例使用80位。如果确实需要,可以添加空格 def int2bin(n,
10 1100 0100 0101 1110 1101 0001 0110 0001 1000 0100 0000 0110 1101 1111 1001 0101
显然,您的值不是以无符号长的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)
输出:
00000000000000 10110001011110110110100001011000011000000001101101111110010101下面是一个代码模板,我不想把所有的乐趣都浪费掉
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上进行迭代?二进制只是一个基数,所以从技术上讲,这个数字已经等同于二进制中的数字。结果是一个66位的二进制数。正在寻求什么:文本10 1100 0100。。。1001 0101或某些值为10 1100 0100的数字。。。10010101基地2?