数字转换可以将二进制转换为十进制,十六进制转换为八进制,以存在3和格雷码 [英] Digital Conversion that can convert binary to decimal to hexadecimal to octal to exess 3 and to graycode

查看:85
本文介绍了数字转换可以将二进制转换为十进制,十六进制转换为八进制,以存在3和格雷码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写仅需简单代码即可将二进制转换为十六进制的代码

How do you wirte a code that can convert binary to hexadecimal just a simple code

推荐答案

我举一个手动计算"示例,实现可以模仿,假设您具有数字的二进制表示形式,即10011.
它的值是1 * 2^4 + 0 * 2^3 + 0 *2^2+ 1 * 2^1 + 1 * 2^0 = 16 + 2 + 1 = 19 (decimal).
您可以通过将其除以8的幂来获得八进制表示形式,因为8^2 = 64可以从8^1开始:
19 / 8^1 = 2,带有提醒3
现在为3 / 8^0 = 3,因此八进制表示(十进制的19)为23.
相同的算法可以成功地用于十六进制数字,获得13.
多余的3和格雷码稍微复杂一些,您可以分别查看它们的定义此处 [ ^ ]和此处 [
I give you an example of ''manual computation'' that your implementation could mimic, suppose you have the binary representation of a number, namely 10011.
It''s value is 1 * 2^4 + 0 * 2^3 + 0 *2^2+ 1 * 2^1 + 1 * 2^0 = 16 + 2 + 1 = 19 (decimal).
You may obtain its octal representation by iteratively dividing it by power of 8, since 8^2 = 64 you may start with 8^1:
19 / 8^1 = 2, with reminder 3
now 3 / 8^0 = 3, hence the octal representation (of 19 decimal) is 23.
The same algo can be succesfully used for hex numbers, obtaining 13.
Excess-3 and Gray code are a bit more complex, you may look up their definition respectively here[^] and here[^] and then implement their algorithms.

Good luck.


我建​​议您看一下这篇CP文章是否可以满足您的所有需求:"MarkGwilliam使用C#编写的数字基转换类| 2006年12月23日" [ ^ ],然后考虑信息如下.

但是,以上链接的文章并未涉及"exess 3和格雷码".

C#中没有二进制(基数2)或八进制(基数8)整数的本机表示形式.唯一可能的整数文字"是十进制和十六进制.

您可以使用 Convert.ToInt32(string value,int base )方法,它要求第一个参数(要转换的整数)为字符串形式:第二个参数指定基数.

试试这个:
I''d recommend you see if this CP article might meet all your needs: "Number base conversion class in C# by MarkGwilliam | 23 Dec 2006"[^], and then consider the information presented below.

However, the article linked to above does not deal with "exess 3 and to graycode."

There are no native representations for binary (base 2) or octal (base 8) integers in C#. The only possible integer "literals" are decimal and hexadecimal.

You can convert an integer which you intend to be octal or binary, to its equivalent integer value in bases 2, 8,10,16 by using the Convert.ToInt32(string value, int base) method which requires the first argument (the integer to be converted) to be in string form: the second argument specifies the base.

Try this:
int binInt1 = 11110;

octInt1 = 64;

// then execute this code:

int base8int = Convert.ToInt32(octInt1.ToString(), 8);

int base2int = Convert.ToInt32(binInt1.ToString(), 2);

并检查结果.

当然,一旦有了实际的整数值,转换为十六进制就变得微不足道了:

And examine the results.

Of course once you have the actual integer value, converting to hexadecimal is trivial:

int base2AsHex = Convert.ToInt32(base2int.ToString(), 16);

但是,您会注意到,在所有这些示例中,您都是在为字符串转换付出代价"……除非您在应用程序中已经将八进制和二进制表示为字符串.

因此,解决方案,如此响应顶部引用的解决方案,试图通过代码加快转换速度,正如上面CPallini的答案所发现的,也可以在这里找到:[

But, you''ll note that in all these examples you are "paying the price" for string conversion ... unless in your application you are already expressing octals and binaries as string.

Hence solutions, like the one cited at the top of this response, that try to speed-up conversions via code, and as found as in the answer by CPallini above, and, also, here:[^].

I find it strange that C# did not come with native built-in numeric base conversions that did not require string conversion or input.


这篇关于数字转换可以将二进制转换为十进制,十六进制转换为八进制,以存在3和格雷码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆