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!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...