十进制转二进制
我有一个数字,我想在 C 中将其转换为二进制(从十进制)。
我希望我的二进制始终为 5 位(十进制永远不会超过 31)。我已经有一个通过除法手动完成此操作的函数,但很难将其填充到 5 位。
有没有更简单的方法?也许使用按位移位?
我还希望二进制文件以 char * 表示
I have a number that I would like to convert to binary (from decimal) in C.
I would like my binary to always be in 5 bits (the decimal will never exceed 31). I already have a function that does it manually by dividing but that is hard to pad it to 5 bits.
Is there any easier way? Perhaps using bitwise shift?
I would also like the binary to be represented in a char *
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这是一个优雅的解决方案:
在这里,我们首先确保字符串以空字符结尾。然后,我们创建一个其中只有一个掩码(它是您期望的掩码,向左移动一次以考虑 while 条件的第一次运行中的移动)。每次循环时,掩码都会向右移动一位,然后将相应的字符设置为“1”或“0”(
!!
确保我们添加0 或 1 到'0'
)。最后,当掩码中的 1 移出数字时,while 循环结束。要测试它,请使用以下命令:
Here's an elegant solution:
Here, we start by making sure the string ends in a null character. Then, we create a mask with a single one in it (its the mask you would expect, shifted to the left once to account for the shift in the first run of the while conditional). Each time through the loop, the mask is shifted one place to the right, and then the corresponding character is set to either a '1' or a '0' (the
!!
ensure that we are adding either a 0 or a 1 to'0'
). Finally, when the 1 in the mask is shifted out of the number, the while loop ends.To test it, use the following:
如果你不需要前导零,你可以只使用 itoa(value, outputstring, base)
例如
将打印出来
否则你可以只写一个非常简单的函数。
将打印出
一个更通用的方法可以是一个询问您要转换多少位的函数。
当然,bitsCount 必须是 1 到 32 之间的值,并且缓冲区字符串必须至少分配 BitsCount + 1 个字符。
If you don't need leading zeroes, you can just use itoa(value, outputstring, base)
For example
will print out
Else you can just write a very simple function.
will print out
A more generic approach can be a function that ask you how much bits to convert.
Of course bitsCount must be a value from 1 to 32, and the buffer string must be allocated for at least bitsCount + 1 characters.
一种方法是这样的:
可能不是最优雅的方法......
One approach is this:
Probably not the most elegant approach...
对于 31 个值,您可能只想使用查找表,而不是执行 malloc 来分配字符串,然后通过位操作来填充它。
那么您的转换就像
return bitstrings[i]
一样简单。如果您经常这样做,这会更快(通过避免 malloc)。否则,您实际上不需要任何移位(除了使编写常量更容易);你可以只使用位与:
如果你假设 ASCII,你可以通过使用加法来进行(也许)微优化。您也可以在此处使用循环,但我需要两行注释:-P。从性能角度来看,两者都不重要。所有的时间都花在了malloc上。
For 31 values, instead of doing malloc to allocate a string, followed by the bit manipulation to fill it, you may just want to use a lookup table.
Then your conversion is as simple as
return bitstrings[i]
. If you're doing this often, this will be faster (by avoiding malloc).Otherwise, you don't really need any shifting (except to make writing your constants easier); you can just use bit-and:
There is a (maybe) micro-optimization you can do if you assume ASCII, by using addition. You can also use a loop here, but I needed two lines for the comment :-P. Performance-wise, neither will matter. All the time is spent in malloc.
您始终可以将其划分并填充到 5 位(这样做是为了填充 8 位,因为打印像 A 这样的字符将是数字 65 )
You can always divide and pad it to 5 bits (did this to pad in 8 bits because printing a character like A would be the number 65 )
既然您只使用 5 位,为什么不使用查找表呢?像下面这样的东西:
或者一个开关:
或者如果这看起来太长,也许像下面这样:
我通常会喜欢前两个之一,但如果由于某种原因其他太大(例如作为小型微控制器的代码)。
这些都假设您希望结果用 0 填充到 5 位。
Since you're only working with 5 bits, why not use a lookup table? Something like the following:
Or a switch:
Or if that seems too long, maybe something like the following:
I would normally favour one of the first two, but the last one might be better if for some reason the others are too big (such as code for small microcontroller).
These all assume you want the result left padded with zeros to 5 bits.
这是使用按位运算符将十进制转换为二进制的 C 程序,可使用系统支持的任何十进制并仅保留所需的内存
Here is C program to convert decimal to binary using bitwise operator with any decimal supported by system and holding only the required memory
希望这有帮助
Hope this helps
我的看法:
这会将 SIZE 字符写入 BUFFER:
并将打印:
My take:
This will write SIZE characters to BUFFER:
And will print: