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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...