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!

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...

Observability | Use Synthetic Monitoring for Website Metadata Verification

If you are on Splunk Observability Cloud, you may already have Synthetic Monitoringin your observability ...

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...