This sent me down a bit of a rabbit hole, so please bare with me. I searched for the dec2hex
filter in the official Jekyll docs and in the Shopify Liquid docs and came up with nothing. A few Google searches also came up with nothing. This leads me to believe that while that filter may have worked, it is undocumented or unsupported.
You could create a plugin that performs this function, or you could roll your own code to do something similar.
From what I can tell there is no out-of-box way to convert decimal to hexadecimal, so I looked up how to do it using this article.
From there, I re-created the code for you to re-use.
Before you read the rest of this:
- This is sample code I tested for just a little bit
- You could remove all the extra stuff here and turn it into an include file, passing it a parameter of the value you want
In this case, I created a markdown file at the root of my Jekyll folder
test.md
---
layout: page
---
{% assign decimal = "now" | date: '%s' | round %}
decimal: {{decimal}}
{% assign quotient = decimal %}
{% assign remainder = quotient %}
{% assign hex = null %}
{% for i in (1..100) %}
{% assign remainder = quotient | modulo: 16 %}
remainder: {{remainder}}
{% assign quotient = quotient | divided_by: 16 %}
quotient: {{quotient}}
{% case remainder %}
{% when 0 %}
{% assign hex = hex | prepend: "0" %}
{% when 1 %}
{% assign hex = hex | prepend: "1" %}
{% when 2 %}
{% assign hex = hex | prepend: "2" %}
{% when 3 %}
{% assign hex = hex | prepend: "3" %}
{% when 4 %}
{% assign hex = hex | prepend: "4" %}
{% when 5 %}
{% assign hex = hex | prepend: "5" %}
{% when 6 %}
{% assign hex = hex | prepend: "6" %}
{% when 7 %}
{% assign hex = hex | prepend: "7" %}
{% when 8 %}
{% assign hex = hex | prepend: "8" %}
{% when 9 %}
{% assign hex = hex | prepend: "9" %}
{% when 10 %}
{% assign hex = hex | prepend: "a" %}
{% when 11 %}
{% assign hex = hex | prepend: "b" %}
{% when 12 %}
{% assign hex = hex | prepend: "c" %}
{% when 13 %}
{% assign hex = hex | prepend: "d" %}
{% when 14 %}
{% assign hex = hex | prepend: "e" %}
{% when 15 %}
{% assign hex = hex | prepend: "f" %}
{% else %}
{% endcase %}
hex: {{hex}}
{% if quotient <= 0 %}
{% break %}
{% endif %}
{% endfor %}
Final hex: {{hex}}
Define the decimal value
First, this code gets the number of seconds since 1970-01-01 00:00:00 UTC using Liquid’s special word now
with the date
operator. I assume this based on Ruby’s Time.Parse method. Also, just in case, I round off the number since this code assumes you are working with a whole number decimal.
Initialize variables
There are a few variables to initialize so we can use them outside iterators:
quotient
is the result as we slowly widdle down the number, dividing by 16
remainder
represents each digit for the hexadecimal output
hex
is the final dec2hex
number you are looking for
Loop until divide by 16 is <= 0
Unfortunately, Liquid – and therefore Jekyll – does not have a do until
loop, so we have to use the for
iterator so I just said “go from 1-100”. You could make the number bigger or smaller. I don’t think your particular number will go more than a few loops.
Note that once the quotient
is <= 0
the loop breaks.
Divide the decimal down to get the hex equivalent
The trick here is to use the modulo
filter to get the remainder and the divide_by
filter to get the quotient. As a reminder, the quotient
is divided by 16
until we get to zero and the remainder
represents the hex number.
Construct the hex number
As noted earlier, each remainder
value in the loop becomes a digit in your hex
value. We need to keep building the hex
variable until the quotient
hits zero.
If I were to keep appending
the hex value, you might have something that looks like this:
2f53
But really, the number is in reverse, so that is why I prepend
, so the number looks like this:
35f2
Here is the table I used to create the case
tag:
Decimal |
Hexidecimal |
0 |
0 |
1 |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
5 |
6 |
6 |
7 |
7 |
8 |
8 |
9 |
9 |
10 |
a |
11 |
b |
12 |
c |
13 |
d |
14 |
e |
15 |
f |
Finish!
With the quotient
down to zero, the hex
value is yours to use!
Remember, you could put this code in an include
file and pass whatever variable you want with a few changes.
Hope this helps!
[edit] pressed save too soon so finished the post 