Splunk Search

How do I convert my hex/oct field into a decimal value?

Lowell
Super Champion

I have a bunch of hexadecimal and/or octadecimal fields in my events. How do I convert these fields into normal decimal values so I can make cool charts and stuff?

Tags (2)
1 Solution

Lowell
Super Champion

There is a new search-time option for this scenario. In splunk 4.1.5 the tonumber() eval command was added. This function allows conversions between different bases which can convert from hexadecimal (base 16) or ocatadecimal (base 😎 to a standard decimal (base 10) value.

Hex example:

| eval dec_value = tonumber(hex_value, 16)

Oct example:

| eval dec_value = tonumber(oct_value, 8)


Of course you'd have to do this many times depending on how many fields you have, and often when there's one hex field, there are many more to follow. So it's possible to make this process slightly less painful by writing a macro.

macros.conf:

[hex2dec(1)]
args = field_name
definition = eval $field_name$=tonumber($field_name$, 16)
iseval = 0

[oct2dec(1)]
args = field_name
definition = eval $field_name$=tonumber($field_name$, 8)
iseval = 0

# Just for the sake of completeness.  Here's a macro to convert back to hex:
[dec2hex(1)]
args = field_name
definition = eval $field_name$=tostring($field_name$, "hex")
iseval = 0

So now in your search, you can convert a number of fields quite quickly, for example:

 sourcetype=stats_in_hex | hex2dec(f1) | hex2dec(f2) | hex2dec(f3) | ...

You still have to name each field individually, but it's faster than writing an entire eval statement each time. (Macros have no looping construct, so this is really the best option at this time.)

Hope you find this useful.

View solution in original post

Lowell
Super Champion

There is a new search-time option for this scenario. In splunk 4.1.5 the tonumber() eval command was added. This function allows conversions between different bases which can convert from hexadecimal (base 16) or ocatadecimal (base 😎 to a standard decimal (base 10) value.

Hex example:

| eval dec_value = tonumber(hex_value, 16)

Oct example:

| eval dec_value = tonumber(oct_value, 8)


Of course you'd have to do this many times depending on how many fields you have, and often when there's one hex field, there are many more to follow. So it's possible to make this process slightly less painful by writing a macro.

macros.conf:

[hex2dec(1)]
args = field_name
definition = eval $field_name$=tonumber($field_name$, 16)
iseval = 0

[oct2dec(1)]
args = field_name
definition = eval $field_name$=tonumber($field_name$, 8)
iseval = 0

# Just for the sake of completeness.  Here's a macro to convert back to hex:
[dec2hex(1)]
args = field_name
definition = eval $field_name$=tostring($field_name$, "hex")
iseval = 0

So now in your search, you can convert a number of fields quite quickly, for example:

 sourcetype=stats_in_hex | hex2dec(f1) | hex2dec(f2) | hex2dec(f3) | ...

You still have to name each field individually, but it's faster than writing an entire eval statement each time. (Macros have no looping construct, so this is really the best option at this time.)

Hope you find this useful.

Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...