Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Octal String to Integer in Python

#1
How to Convert Octal String to Integer in Python

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;948232&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="705" height="497" src="https://blog.finxter.com/wp-content/uploads/2022/12/oct_to_int.gif" alt="" class="wp-image-948279"/></figure>
</div>
<h2>Problem Formulation</h2>
<p>Given a string in the octal form:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">s = '0o77'
# or s = '77'</pre>
<p><strong>How to convert the octal string to an integer in Python?</strong></p>
<p>For example, you want to convert the octal string <code>'o10'</code> to the decimal integer <code>8</code>. </p>
<p>Here are a few other examples:</p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Octal String</th>
<th>Decimal</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>'0o0'</code></td>
<td>0</td>
</tr>
<tr>
<td><code>'0o4'</code></td>
<td>4</td>
</tr>
<tr>
<td><code>'0o10'</code></td>
<td>8</td>
</tr>
<tr>
<td><code>'0o14'</code></td>
<td>12</td>
</tr>
<tr>
<td><code>'0o20'</code></td>
<td>16</td>
</tr>
<tr>
<td><code>'0o77'</code></td>
<td>63</td>
</tr>
<tr>
<td><code>'0o77777'</code></td>
<td>32767</td>
</tr>
</tbody>
</table>
</figure>
<h2>Oct String to Integer using int() with Base 8</h2>
<p class="has-pale-cyan-blue-background-color has-background">To convert an octal string to an integer, pass the string as a first argument into Python’s<a title="Python int() Function" rel="noreferrer noopener" href="https://blog.finxter.com/python-int-function/" target="_blank"> built-in <code>int()</code></a> function. Use <code>base=8</code> as a second argument of the <code>int()</code> function to specify that the given string is an octal number. The <code>int()</code> function will then convert the octal string to an integer with base 10 and return the result.</p>
<p>Here’s a minimal example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> int('0o77', base=8)
63</pre>
<h2>Examples</h2>
<p>And here’s how you can convert the additional examples shown above: </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> int('0o0', base=8)
0
>>> int('0o4', base=8)
4
>>> int('0o10', base=8)
8
>>> int('0o14', base=8)
12
>>> int('0o20', base=8)
16
>>> int('0o77', base=8)
63
>>> int('0o77777', base=8)
32767</pre>
<p>You actually don’t need to use the prefix <code>'0o'</code> because your second argument already defines unambiguously that the given string is an octal number:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> int('0', base=8)
0
>>> int('4', base=8)
4
>>> int('10', base=8)
8
>>> int('14', base=8)
12
>>> int('20', base=8)
16
>>> int('77', base=8)
63
>>> int('77777', base=8)
32767</pre>
<p>However, skipping the base but leaving the prefix raises a <code>ValueError: invalid literal for int() with base 10: '0o77'</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1,5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> int('0o77')
Traceback (most recent call last): File "&lt;pyshell#16>", line 1, in &lt;module> int('0o77')
ValueError: invalid literal for int() with base 10: '0o77'</pre>
<p>It assumes that the input string is in base 10 when in fact, it isn’t.</p>
<p class="has-pale-pink-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: Even though passing a prefixed string <code>'0o...'</code> into the <code>int()</code> function is unambiguous, Python’s <code>int()</code> function doesn’t accept it if you don’t also define the base. This may be fixed in future <a title="HOW TO CHECK YOUR PYTHON VERSION" rel="noreferrer noopener" href="https://blog.finxter.com/how-to-check-your-python-version/" target="_blank">versions</a>!</p>
<p>In fact, you can specify the base argument as <code>0</code> to switch on base guessing—which should be the default behavior anyway! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Base Guessing</h2>
<p>You can pass a prefixed string <code>'0o...'</code> into the <code>int()</code> function and set the base to <code>0</code> to switch on <strong><em>base guessing</em></strong> in Python. This uses the prefix to determine the base automatically—without you needing to set it to <code>16</code>. Yet, you still have to set it to <code>0</code> so the benefit is marginal in practice.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> int('0o7', base=8)
7
>>> int('0o7', base=0)
7
>>> int('0o7', 0)
7</pre>
<h2>Converting Octal Literals to Int</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="978" height="548" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-40.png" alt="" class="wp-image-948266" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-40.png 978w, https://blog.finxter.com/wp-content/uplo...00x168.png 300w, https://blog.finxter.com/wp-content/uplo...68x430.png 768w" sizes="(max-width: 978px) 100vw, 978px" /></figure>
</div>
<p>If you don’t have an octal string but a octal number—called a <em><strong>literal</strong></em>—such as <code>0xff</code>, you don’t even need the <code>int()</code> function because Python will automatically <a rel="noreferrer noopener" href="https://blog.finxter.com/python-hex-string-to-decimal/" data-type="URL" data-id="https://blog.finxter.com/python-hex-string-to-decimal/" target="_blank">convert it to a decimal number</a>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> 0o743
483
>>> 0o7
7
>>> 0o10
8</pre>
<h2>Background int()</h2>
<pre class="wp-block-preformatted"><strong>Syntax</strong>: <code>int(value [, base]) – &gt; int</code></pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>Argument</strong></td>
<td><code>value</code></td>
<td>A Python object to be converted into an integer number. The value object must have an <code>__int__()</code> method that returns the associated integer number—otherwise a <code>TypeError</code> will be raised.</td>
</tr>
<tr>
<td></td>
<td><code>base</code></td>
<td>An optional integer argument <code>base</code> to define the base of the numerical system in the <code>value</code> argument. If you set the base, the <code>value</code> argument must be a string. The <code>base</code> argument determines how the string argument is interpreted.</td>
</tr>
<tr>
<td><strong>Return Value</strong></td>
<td><code>int</code></td>
<td>Returns an integer number after converting the input argument <code>value</code> using its required <code>__int__()</code> method for the conversion.</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-convert-octal-string-to-integer-in-python/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJKDzKWtlaQE%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p>Do you still need more background information about Python’s <a href="https://blog.finxter.com/python-built-in-functions/" target="_blank" rel="noreferrer noopener" title="Python Built-In Functions">built-in</a> <code>int()</code> function? No problem, read over the related tutorial.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Related Tutorial:</strong> <a rel="noreferrer noopener" title="https://blog.finxter.com/python-int-function/" href="https://blog.finxter.com/python-int-function/" target="_blank">Python’s Built-in <code>int()</code> Function</a></p></p>
</div>


https://www.sickgaming.net/blog/2022/12/...in-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016