- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以向我解释一下这是如何工作的吗?我一直看着它,但我就是不明白。它似乎工作得很好,但对我来说这毫无意义。请有人帮忙。
# Convert a decimal to a hex as a string
def decimalToHex(decimalValue):
hex = ""
while decimalValue != 0:
hexValue = decimalValue % 16
hex = toHexChar(hexValue) + hex
decimalValue = decimalValue // 16
return hex
# Convert an integer to a single hex digit in a character
def toHexChar(hexValue):
if 0 <= hexValue <= 9:
return chr(hexValue + ord('0'))
else: # 10 <= hexValue <= 15
return chr(hexValue - 10 + ord('A'))
def main():
# Prompt the user to enter a decimal integer
decimalValue = eval(input("Enter a decimal number: "))
print("The hex number for decimal",
decimalValue, "is", decimalToHex(decimalValue))
main() # Call the main function
最佳答案
好吧,我会尝试解释一下。首先,让我们看看将数字从 0-15 转换为 0123...ABCDEF 的函数:
def toHexChar(hexValue):
if 0 <= hexValue <= 9:
return chr(hexValue + ord('0'))
else: # 10 <= hexValue <= 15
return chr(hexValue - 10 + ord('A'))
可以看到,它处理两种情况:一种是数字0-9需要转换成'0'到'9'的文本字符,另一种是数字10-16需要转换成从“A”到“F”的文本字符。它将根据字符的 ASCII 代码返回字符。例如,ord('A')
返回 65。chr()
会将整数从 65 转换回 ascii 字符“A”。
这就是打印功能。现在循环:
当十进制值!= 0:
此循环将一直运行,直到无法获取 16er block 为止
十六进制值 = 小数值 % 16
正常模数。它从小数中提取一个 block (hexValue 将为 0-15)
十六进制 = toHexChar(hexValue) + 十六进制
构建一个字符串。由于十六进制被标准化为在左侧有更大的数字部分,因此它会预先考虑转换后的字符。
decimalValue =decimalValue//16
准备下一个循环:使用 floordiv(a, b)
删除这个 16er block (写为//b)。
让我们了解它是如何工作的。假设我们想要将数字 255 转换为 HexString。它将首先调用模 255 % 16 = 15
。然后它将被转换为 toHexChar(15) = 'F'
。
下一步是删除该 16er block 。如果我们使用普通除法,我们会得到255/16 = 15.9375
。然而,这将被四舍五入,因为它在小数点后高于 0.5 导致错误的行为(结果将是 0x1FF)。这就是为什么我们必须使用一个 Floordivsion ,它是floor(255/16) = 15
或简称:255//16 = 15
。
我希望这能更好地解释它,特别是为什么需要//。
关于Python程序解释: Decimal to hex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23046232/
你好,我是 SMARTRF-05EB - 评估板的新手,我已经在 Linux 虚拟机中安装了 SDCC 和 Contiki-2.6。我能够构建此目录 contiki-2.6/contiki/examp
下面的简单代码会产生奇怪的输出: #include #include #include "/tmp/sha.h" #define DIGEST 64 //taken from coreutils
我不知道为什么 Hex 函数返回一个像 '0x41' 而不是 0x41 的字符串 我需要将 ASCII 值转换为十六进制。但我想要 0x INT 格式,而不是 '0x' 字符串。 ascii = 36
所以我试图从十六进制文件中读取,修改十六进制值,将新的十六进制值写入新文件。然后打开新文件,再次修改十六进制并重写到第三个文件。我正在对十六进制值进行非常简单的加密。 我用来阅读的函数是休闲的: ve
是否有一种惯用的方式将 Clojure 中的字符串编码和解码为十六进制?来自 Python 的示例: 'Clojure'.encode('hex') # ⇒ '436c6f6a757265' '436
下面的函数可以完美地将字符串转换为十六进制: function String2Hex(const Buffer: AnsiString): string; begin SetLength(Resu
我要做的是将 double 字符串转换为十六进制字符串,然后再转换回 double 。 以下代码将 double 字符串转换为十六进制字符串。 char * double2HexString(doub
我正在尝试将整数转换为十六进制。我找到了解决此问题的答案,例如 this ,尽管它们对我不起作用。我的意思是: 如果我拿这个: buf = "" buf += "\xda\xc7\xd9\x74\x
许多在线示例将哈希显示为十六进制表示,它们通常是自定义实现。改用 Apache Commons Base64 编码有什么问题或安全性降低吗?在阅读有关编码的内容时,通常是在如何将二进制表示为 XML
我需要存储这种类型的值 0xff0000或 0x00ff08 (十六进制颜色表示)在solidity智能合约中,并且能够在合约内将其转换为具有相同文本字符的字符串"ff0000" .我打算在 RSK
如果我有两种颜色: #ffcc00 和#334455 我如何获得这两种颜色之间的所有#hex 颜色,并逐渐排序? 最佳答案 你可以试试这个工具: http://www.colorhexa.com/ff
我的命令输出类似于 0x53 0x48 0x41 0x53 0x48 0x49。现在我需要将其存储在十六进制值中,然后将其转换为 ASCII 作为 SHASHI。 我试过的- 我尝试将十六进制值存储为
我正在使用计算 16 位 CRC 校验和的函数。 该函数生成一个包含校验和的 LONG(以 10 为基数的数字格式)。当然,这可以以十六进制等效形式打印到控制台,如下所示: printf("Check
我有一些以 INTEGER 形式提供给我的数字数据。我将它与其他数据一起插入到 SQLite 中,但当我将其写出时 INTEGER 数字需要为 8 位 Hex 数字,前导零。 例如 输入 400 80
在尝试附加数据对象转换为十六进制值后,我有如下代码,但它给了我以下错误: Unexpectedly found nil while unwrapping an Optional value let n
我有三个程序。程序A的代码如下: #include "stdafx.h" #include #include #include using namespace std; int _tmain(
我得到了这段代码,它从十六进制转换为 base64,反之亦然。我从另一个 SO 问题中得到了 to_base64,然后我通过一些猜测和反复试验编写了 to_hex。 class String de
我有一些关于控制建筑物访问的磁卡的信息,并且需要对软件在卡上存储数据的方式进行逆向工程,以制作具有更多功能的东西。 (我们已经和原来的程序员失去了联系。) 我不知道如何编写订单;我不是磁卡专家。 我知
我想在我拥有的 HEX 文件中找到模式并按出现次数对它们进行排序。 我不是在寻找某种特定的模式,只是为了对那里发生的事件进行一些统计并对其进行排序。 DB0DDAEEDAF7DAF5DB1FDB1DD
我在出示卡片时从 RFID 阅读器接收字节,但我无法弄清楚如何从这些字节中获取卡 ID。 例如,我有一张卡片,上面印有这些数字: 0007625328 116,23152 .我希望这是那张卡的 ID,
我是一名优秀的程序员,十分优秀!