{"id":24790,"date":"2021-11-02T09:47:52","date_gmt":"2021-11-02T04:17:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=24790"},"modified":"2021-11-05T20:36:19","modified_gmt":"2021-11-05T15:06:19","slug":"python-hex-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-hex-method-with-examples\/","title":{"rendered":"Python hex() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python float() Method with Examples<\/a> The hex() function converts the given number to a hexadecimal value.<\/p>\n The prefix 0x is always present in the returned string.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n The hex() function only accepts one argument.<\/p>\n x – integer number (either an int object or a __index__() method that returns an integer)<\/p>\n Return Value:<\/strong><\/p>\n The hex() function converts an integer to its corresponding hexadecimal number and returns it as a string.<\/p>\n The hexadecimal string returned begins with the prefix 0x, indicating that it is in hexadecimal form.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python float() Method with Examples hex() Method in Python: The hex() function converts the given number to a hexadecimal value. The prefix 0x is always present in the returned string. Syntax: hex(number) Parameters The hex() function only accepts one argument. x – integer number (either an int object …<\/p>\n
\nhex() Method in Python:<\/strong><\/p>\nhex(number)<\/pre>\n
Given Number = 20<\/pre>\n
The given number's{ 20 } Hexadecimal Value = 0x14\r\nThe hex() function Return Type = <class 'str'><\/pre>\n
The prefix 0x denotes that the outcome is a Hexadecimal string.<\/pre>\n
Given Number = 4.5<\/pre>\n
The given float number's{ 4.5 } Hexadecimal Value = 0x1.2000000000000p+2<\/pre>\n
hex() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)<\/h5>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numbr = 20\r\n# Pass the given number as an argument to the hex() function to get the\r\n# hexadecimal value of a given number.\r\n# Store it in another variable.\r\nhexadeciml_valu = hex(gvn_numbr)\r\n# Print the given number's Hexadecimal Value.\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Hexadecimal Value = \", hexadeciml_valu)\r\n# Print the return Type of hex() Function using the type() method by passing\r\n# the hex(given number) as an argument to it.\r\nprint(\"The hex() function Return Type =\", type(hex(gvn_numbr)))\r\n<\/pre>\n
The given number's{ 20 } Hexadecimal Value = 0x14\r\nThe hex() function Return Type = <class 'str'><\/pre>\n
2)Hexadecimal Value of a floating-point Number<\/h5>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numbr = 4.5\r\n# Pass the given number as an argument to the hex() function and apply float\r\n# to the result to get the hexadecimal value of a given float number.\r\n# Store it in another variable.\r\nhexadeciml_valu = float.hex(gvn_numbr)\r\n# Print the given float number's Hexadecimal Value.\r\nprint(\"The given float number's{\", gvn_numbr,\r\n \"} Hexadecimal Value = \", hexadeciml_valu)\r\n<\/pre>\n
The given float number's{ 4.5 } Hexadecimal Value = 0x1.2000000000000p+2<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numbr = int(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the hex() function to get the\r\n# hexadecimal value of a given number.\r\n# Store it in another variable.\r\nhexadeciml_valu = hex(gvn_numbr)\r\n# Print the given number's Hexadecimal Value.\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Hexadecimal Value = \", hexadeciml_valu)\r\n# Print the return Type of hex() Function using the type() method by passing\r\n# the hex(given number) as an argument to it.\r\nprint(\"The hex() function Return Type =\", type(hex(gvn_numbr)))\r\n<\/pre>\n
Enter some random number = 50\r\nThe given number's{ 50 } Hexadecimal Value = 0x32\r\nThe hex() function Return Type = <class 'str'><\/pre>\n
2)Hexadecimal Value of a floating-point Number<\/h5>\n
\n
# Give the number as user input using the float(input()) function and store it in a variable.\r\ngvn_numbr = float(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the hex() function and apply float\r\n# to the result to get the hexadecimal value of a given float number.\r\n# Store it in another variable.\r\nhexadeciml_valu = float.hex(gvn_numbr)\r\n# Print the given float number's Hexadecimal Value.\r\nprint(\"The given float number's{\", gvn_numbr,\r\n \"} Hexadecimal Value = \", hexadeciml_valu)\r\n\r\n<\/pre>\n
Enter some random number = 7.5\r\nThe given float number's{ 7.5 } Hexadecimal Value = 0x1.e000000000000p+2<\/pre>\n
\n