{"id":7393,"date":"2021-09-30T11:30:31","date_gmt":"2021-09-30T06:00:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=7393"},"modified":"2021-11-22T18:35:29","modified_gmt":"2021-11-22T13:05:29","slug":"python-program-to-convert-decimal-to-binary-octal-and-hexadecimal","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-decimal-to-binary-octal-and-hexadecimal\/","title":{"rendered":"Python Program to Convert Decimal to Binary, Octal and Hexadecimal"},"content":{"rendered":"

Decimal System:<\/strong> The decimal system is the most extensively used number system. This is a base ten numbering system. A number is represented by 10 numbers (0-9) in this system.<\/p>\n

Binary Number System:<\/strong> The binary number system is a base two number system. Because computers can only understand binary numbers, the binary system is utilized (0 and 1).<\/p>\n

Octal system: <\/strong>Octal system\u00a0is a base 8 numbering system.<\/p>\n

Hexadecimal Number System:<\/strong> The Hexadecimal number system is a base 16 number system.<\/p>\n

The decimal system is the most extensively used system of numbering. Computers, on the other hand, only understand binary. We may need to translate decimal into binary, octal, or hexadecimal number systems because they are closely connected.<\/p>\n

The decimal system is based on base ten (ten symbols, 0-9, are used to represent a number), while binary is based on base 2, octal is based on base 8, and hexadecimal is based on base 16.<\/p>\n

Binary numbers have the prefix 0b<\/strong>, octal numbers have the prefix 0o<\/strong>, and hexadecimal numbers have the prefix 0x<\/strong>.<\/p>\n

Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Decimal_number=1028<\/pre>\n

Output:<\/strong><\/p>\n

Printing the values after the conversion of bases that is decimal to binary,octal,hexadecimal:\r\nThe binary value of the given decimal number 1028 = 0b10000000100\r\nThe octal value of the given decimal number 1028 = 0o2004\r\nThe hexadecimal value of the given decimal number 1028 = 0x404<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Decimal_number=937238<\/pre>\n

Output:<\/strong><\/p>\n

Printing the values after the conversion of bases that is decimal to binary,octal,hexadecimal:\r\nThe binary value of the given decimal number 937238 = 0b11100100110100010110\r\nThe octal value of the given decimal number 937238 = 0o3446426\r\nThe hexadecimal value of the given decimal number 937238 = 0xe4d16<\/pre>\n

Example3:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Decimal_number=0<\/pre>\n

Output:<\/strong><\/p>\n

Printing the values after the conversion of bases that is decimal to binary,octal,hexadecimal:\r\nThe binary value of the given decimal number 0 = 0b0\r\nThe octal value of the given decimal number 0 = 0o0\r\nThe hexadecimal value of the given decimal number 0 = 0x0<\/pre>\n

Program to Convert Decimal to Binary, Octal and Hexadecimal in Python<\/h2>\n

Python provides different types of built in functions to convert into different number systems like<\/p>\n

    \n
  1. bin() :<\/strong> To convert the given decimal number to binary number system<\/li>\n
  2. hex() :<\/strong> To convert the given decimal number to binary number system<\/li>\n
  3. oct()\u00a0 :<\/strong>To convert the given decimal number to binary number system<\/li>\n<\/ol>\n

    We will use the above mentioned functions to convert the given decimal number into binary,octal,hexadecimal number system.<\/p>\n

    Below is the implementation:<\/strong><\/p>\n

    # given number in decimal number system\r\ndecimal_Number = 1028\r\n# converting the given decimal_number to binary_number system\r\n# we use bin() function to convert given decimal_number to binary_number system\r\nbinValue = bin(decimal_Number)\r\n# converting the given decimal_number to octal_number system\r\n# we use bin() function to convert given decimal_number to binary_number system\r\noctalValue = oct(decimal_Number)\r\n# converting the given decimal_number to hexadecimal_number system\r\n# we use bin() function to convert given decimal_number to binary_number system\r\nhexValue = hex(decimal_Number)\r\n# printing the values after the conversion of bases that is\r\n# decimal to binary,octal,hexadecimal\r\nprint(\"Printing the values after the conversion of bases\"\r\n      \" that is decimal to binary,octal,hexadecimal:\")\r\n# printing the binary converted value\r\nprint(\"The binary value of the given decimal number\",\r\n      decimal_Number, \"=\", binValue)\r\n# printing the octal converted value\r\nprint(\"The octal value of the given decimal number\",\r\n      decimal_Number, \"=\", octalValue)\r\n# printing the hexadecimal converted value\r\nprint(\"The hexadecimal value of the given decimal number\",\r\n      decimal_Number, \"=\", hexValue)\r\n<\/pre>\n

    Output:<\/strong><\/p>\n

    Printing the values after the conversion of bases that is decimal to binary,octal,hexadecimal:\r\nThe binary value of the given decimal number 1028 = 0b10000000100\r\nThe octal value of the given decimal number 1028 = 0o2004\r\nThe hexadecimal value of the given decimal number 1028 = 0b10000000100<\/pre>\n

    Related Programs<\/strong>:<\/p>\n