{"id":241,"date":"2024-03-08T16:05:20","date_gmt":"2024-03-08T16:05:20","guid":{"rendered":"https:\/\/offensivepython.com\/?p=241"},"modified":"2024-03-15T02:07:31","modified_gmt":"2024-03-15T02:07:31","slug":"binary-hexadecimal-number-assignments","status":"publish","type":"post","link":"https:\/\/offensivepython.com\/index.php\/2024\/03\/08\/binary-hexadecimal-number-assignments\/","title":{"rendered":"Binary and Hexadecimal Number Assignments"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"241\" class=\"elementor elementor-241\">\n\t\t\t\t<div class=\"elementor-element elementor-element-10e58b9e e-flex e-con-boxed e-con e-parent\" data-id=\"10e58b9e\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-4407d048 elementor-widget elementor-widget-text-editor\" data-id=\"4407d048\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><\/p>\n<p>\u00a0 \u00a0 \u00a0Sometimes we need to work with binary and\/or hexadecimal numbers in Python. Maybe we are designing a program that is processing packet capture data, or we are working with IP addressing at a low level, whatever the case may be. In Python, we can easily assign binary and\/or hexadecimal numbers to variables in a couple of different ways.<\/p>\n<ol>\n<li>Precede a binary number with <strong>0b<\/strong>. Precede a hexadecimal number with <strong>0x<\/strong>.<\/li>\n<li>Use the<strong> int<\/strong>\u00a0function, where the first positional argument is a string representation of our binary or hexadecimal number, and the second argument is an integer representing the base value, <strong>2 for binary<\/strong>, and <strong>16 for hexadecimal<\/strong>.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\">Example code:<\/span><\/p>\n<p><strong>bin_1 = 0b1011<\/strong><br \/><strong>bin_2 = int(&#8220;1001&#8221;, 2)<\/strong><br \/><strong>hex_1 = 0x0B<\/strong><br \/><strong>hex_2 = int(&#8220;0009&#8221;, 16)<\/strong><\/p>\n<p><strong>print(bin_1, bin_2, hex_1, hex_2)<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\">Output:<\/span><\/p>\n<p>11 9 11 9<\/p>\n<p>\u00a0 \u00a0 \u00a0Regardless of the fact that we assigned binary and hexadecimal numbers to the variables, decimal values are printed. If we need to print the binary or hexadecimal numbers, we have to use the <strong>bin<\/strong>\u00a0and\/or <strong>hex<\/strong>\u00a0functions for casting.<\/p>\n<p><span style=\"text-decoration: underline;\">Example code:<\/span><\/p>\n<p><strong>print(bin(bin_1), bin(bin_2))<\/strong><br \/><strong>print(hex(hex_1), hex(hex_2))<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\">Output:\u00a0<\/span><\/p>\n<p>0b1011 0b1001\u00a0<br \/>0xb 0x9<\/p>\n<p><span style=\"text-decoration: underline;\">And of course, it works this way as well:<\/span><\/p>\n<p><strong>print(hex(bin_1), hex(bin_2))<\/strong><br \/><strong>print(bin(hex_1), bin(hex_2))<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\">Output:\u00a0<\/span><\/p>\n<p><strong>0b1011 0b1001\u00a0<\/strong><br \/><strong>0xb 0x9<\/strong><\/p>\n<p><\/p>\n<p><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 \u00a0Sometimes we need to work with binary and\/or hexadecimal numbers in Python. Maybe we are designing a program that is processing packet capture data, or we are working with IP addressing at a low level, whatever the case may be. In Python, we can easily assign binary and\/or hexadecimal numbers to variables in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[19],"tags":[23,22,24],"class_list":["post-241","post","type-post","status-publish","format-standard","hentry","category-binary-and-hexadecimal","tag-bin","tag-hex","tag-int"],"_links":{"self":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/comments?post=241"}],"version-history":[{"count":14,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/241\/revisions"}],"predecessor-version":[{"id":340,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/241\/revisions\/340"}],"wp:attachment":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/categories?post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/tags?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}