将十进制转换为十六进制和八进制
展示如何用 C 语言编写一个常量,其十进制值为 65 作为
a。十六进制常量65/16 = 1 r1
1/16 = 0 r1
十六进制常数 = 11
b.八进制常量(C 语言)65/8 = 8 r1
8/8 = 1 r0
1/8 = 0 r1 八进制常量 = 101
这是在 C 中转换常量的正确方法吗?
Show how to write a constant in C, whose decimal value is 65 as
a. a hexadecimal constant65/16 = 1 r1
1/16 = 0 r1
Hexadecimal constant = 11
b. an octal constant (in C)65/8 = 8 r1
8/8 = 1 r0
1/8 = 0 r1
Octal constant = 101
Is this the right way to convert constants in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需要一个 while 循环和一个字符串。由于这是作业,我认为我不应该说更多。
You just need a while loop and a string. As this is homework, I do not think I should say more than that.
方法是除以基数,直到结果小于基数。
所以 65/8 给出 8 r1 但你不会停在那里,因为结果是 8 不小于 8
你再次除以 8 并得到 1
它应该是
65/64 = 10 r 1 其中 64 = 8x8 = 八进制 10
我认为我没有说太多
The method is to divide by the base until the result is less than the base.
So 65/8 gives 8 r1 but you don't stop there because the result is 8 not less than 8
You divide by 8 again and get 1
It should be
65/64 = 10 r 1 where 64 = 8x8 = octal 10
I don't think I've said too much
也许我误解了这些问题,但似乎您被问到如何在 C 中表示十六进制和八进制常量,而不是如何实现将十进制转换为十六进制和八进制的算法。
如果是这种情况:
十六进制数字由前面的 0x 或 0X 表示
八进制数字由前面的 0 表示
当然,您可以通过打印十进制值来验证这一点:
Maybe I am misunderstanding the questions, but it seems like you are being asked how hex and oct constants are represented in C, not how to implement an algorithm to convert dec to hex and oct.
If that is the case:
hex numbers are represented by a preceding 0x or 0X
oct numbers are represented by a preceding 0
Of course, you can verify this by printing our the values in decimal: