在Python中以十六进制格式输入数字

您所在的位置:网站首页 python输入一串数字求和 在Python中以十六进制格式输入数字

在Python中以十六进制格式输入数字

2024-07-03 18:12| 来源: 网络整理| 查看: 265

Syntax to convert hexadecimal value to an integer (decimal format),

将十六进制值转换为整数(十进制格式)的语法,

int(hex_value, 16)

Here,

这里,

hex_value should contain the valid hexadecimal value

hex_value应该包含有效的十六进制值

16 is the base value of the hexadecimal number system

16是十六进制系统的基值

Note: hex_value must contain only hexadecimal digits (0, 1, 2, 3 ,4 ,5 ,6, 7, 8, 9, A/a, B/b, C/c, D/d, E/e, F/F), if it contains other than these digits a "ValueError" will return.

注意 : hex_value必须仅包含十六进制数字(0、1、2、3、4、5、6、7、8、9,A / a,B / b,C / c,D / d,E / e,F / F),如果其中包含这些数字以外的字符,则将返回“ ValueError” 。

程序将给定的十六进制值转换为整数(十进制) (Program to convert given hexadecimal value to integer (decimal)) # function to convert given hexadecimal Value # to an integer (decimal number) def HexToDec(value): try: return int(value, 16) except ValueError: return "Invalid Hexadecimal Value" # Main code input1 = "1235A" input2 = "6ABF" input3 = "6AG09" print(input1, "as decimal: ", HexToDec(input1)) print(input2, "as decimal: ", HexToDec(input2)) print(input3, "as decimal: ", HexToDec(input3))

Output

输出量

1235A as decimal: 74586 6ABF as decimal: 27327 6AG09 as decimal: Invalid Hexadecimal Value

Now, we are going to implement the program – that will take input the number as a hexadecimal number and printing it in the decimal format.

现在,我们将实现该程序–将输入的数字作为十六进制数字并以十进制格式打印。

程序以十六进制格式输入数字 (Program to input a number in hexadecimal format) # input number in hexadecimal format and # converting it into decimal format try: num = int(input("Input hexadecimal value: "), 16) print("num (decimal format):", num) print("num (hexadecimal format):", hex(num)) except ValueError: print("Please input only hexadecimal value...")

Output

输出量

RUN 1: Input hexadecimal value: 123 num (decimal format): 291 num (hexadecimal format): 0x123 RUN 2: Input hexadecimal value: 6ABF12 num (decimal format): 6995730 num (hexadecimal format): 0x6abf12 RUN 3: Input hexadecimal value: 1234ABCFDEF num (decimal format): 1251089382895 num (hexadecimal format): 0x1234abcfdef RUN 4: Input hexadecimal value: 65afcd num (decimal format): 6664141 num (hexadecimal format): 0x65afcd RUN 5: Input hexadecimal value: 123AFG Please input only hexadecimal value...

Recommended posts

推荐的帖子

Read input as an integer in Python

在Python中将输入读取为整数

Read input as a float in Python

在Python中以浮点形式读取输入

Parse a string to float in Python (float() function)

解析要在Python中浮动的字符串(float()函数)

How do you read from stdin in Python?

您如何从Python的stdin中读取信息?

Asking the user for integer input in Python | Limit the user to input only integer value

要求用户在Python中输入整数| 限制用户仅输入整数值

Asking the user for input until a valid response in Python

要求用户输入直到Python中的有效响应

Input a number in octal format in Python

在Python中以八进制格式输入数字

Input a number in binary format in Python

在Python中以二进制格式输入数字

How to get the hexadecimal value of a float number in python?

如何在python中获取浮点数的十六进制值?

Convert an integer value to the string using str() function in Python

使用Python中的str()函数将整数值转换为字符串

Convert a float value to the string using str() function in Python

使用Python中的str()函数将浮点值转换为字符串

Input and Output Operations with Examples in Python

使用Python中的示例进行输入和输出操作

Taking multiple inputs from the user using split() method in Python

使用Python中的split()方法从用户获取多个输入

Fast input / output for competitive programming in Python

快速输入/输出,可在Python中进行有竞争力的编程

Precision handling in Python

Python中的精确处理

Python print() function with end parameter

带有结束参数的Python print()函数

翻译自: https://www.includehelp.com/python/input-a-number-in-hexadecimal-format.aspx



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3