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!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...