# Convert a hex number to decimal

**URL:** <https://forum.ansible.com/t/convert-a-hex-number-to-decimal/30419>\
**Category:** Ansible Project\
**Created:** [May 24, 2019, 7:25pm UTC](https://forum.ansible.com/t/convert-a-hex-number-to-decimal/30419 "2019-05-24T19:25:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob.carroll](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bob.carroll](https://forum.ansible.com/u/bob.carroll)\
**Post date:** [May 24, 2019, 7:25pm UTC](https://forum.ansible.com/t/convert-a-hex-number-to-decimal/30419/1 "2019-05-24T19:25:05Z")

</div>

This has to be easy, but I have tried a way to many things. Given a hex number as a string, say ‘8d’, I cannot convert it to a decimal string.  
I presumed to prepend ‘0x’ but ‘{{ “0x8d” | int }}’ returns 0. I am not having any better luck with jinja2 format(). I feel pretty stupid right now and I am tempted to just call the shell.  
Sorry, but sometimes I really miss perl’s weak type casting.

One of 100 attempts:

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [May 24, 2019, 8:20pm UTC](https://forum.ansible.com/t/convert-a-hex-number-to-decimal/30419/2 "2019-05-24T20:20:39Z")

</div>

`{{ hex_val | int(base=16) }}`

[http://jinja.pocoo.org/docs/2.10/templates/#int](http://jinja.pocoo.org/docs/2.10/templates/#int)

---

<div class="post-metadata">

**Author:** ![bob.carroll](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bob.carroll](https://forum.ansible.com/u/bob.carroll)\
**Post date:** [May 24, 2019, 9:15pm UTC](https://forum.ansible.com/t/convert-a-hex-number-to-decimal/30419/3 "2019-05-24T21:15:56Z")

</div>

Thanks so much for your reply. Perhaps I need a bit more explanation.  
When I try using this construct in a template, I get “do\_int() got an unexpected keyword argument ‘base’”

I get the same thing using a playbook:

---

<div class="post-metadata">

**Author:** ![bob.carroll](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bob.carroll](https://forum.ansible.com/u/bob.carroll)\
**Post date:** [May 24, 2019, 9:33pm UTC](https://forum.ansible.com/t/convert-a-hex-number-to-decimal/30419/4 "2019-05-24T21:33:14Z")

</div>

One more thing to note:  
If the hex string is originally defined as an integer, the example playbook works as expected. i.e. hex\_val: 0x8d _#without quotes_  
But since I am getting the “8d” from an external command, there does not seem to be a way to turn it back into an integer.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 24, 2019, 9:36pm UTC](https://forum.ansible.com/t/convert-a-hex-number-to-decimal/30419/5 "2019-05-24T21:36:00Z")

</div>

You are using a old Jinja, base was added to int in 2.8 that was released in 2015.  
Current stable is 2.10.

---

<div class="post-metadata">

**Author:** ![bob.carroll](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bob.carroll](https://forum.ansible.com/u/bob.carroll)\
**Post date:** [June 6, 2019, 8:23pm UTC](https://forum.ansible.com/t/convert-a-hex-number-to-decimal/30419/6 "2019-06-06T20:23:20Z")

</div>

Thanks for that. I thought I was going crazy. Unfortunately, as much as Red Hat is promoting Ansible, they are using really old back-end support libraries. I did verify your solution with an updated python-jinja2 package.
