parse_bytes

This page explains how to use the parse_bytes function in APL.

The parse_bytes function parses a string representation of data size (like 1 KB, 500 MB) and returns the numeric value in bytes. Use this function to convert human-readable byte strings from logs or configuration files into numeric values for calculations and comparisons.

Usage

Syntax

parse_bytes(bytes_string, base)

Parameters

NameTypeRequiredDescription
bytes_stringstringYesA string representing a data size with units (for example, 1 KB, 500 MB, 2 GB).
baseintNoEither 2 (default, 1024-based) or 10 (1000-based) for unit calculations.

Returns

Returns the numeric value in bytes, or 0 if the string can't be parsed.

Use case examples

Parse human-readable size strings to analyze and aggregate data transfer volumes.

Query

['sample-http-logs']
| extend size_bytes = parse_bytes('512 KB')
| where req_duration_ms > 3
| summarize total_bytes = sum(size_bytes), request_count = count() by status
| sort by total_bytes desc
| limit 10

Run in Playground

Output

statustotal_bytesrequest_count
20045875208765
50010485762341
4045242881234

This query parses size strings to calculate total data transfer by HTTP status code, enabling volume-based analysis of API usage.

Convert size strings from span attributes to numeric bytes for threshold-based analysis.

Query

['otel-demo-traces']
| extend payload_size = parse_bytes('256 KB', 2)
| where duration > 100ms
| summarize avg_size = avg(payload_size), span_count = count() by ['service.name']
| sort by avg_size desc
| limit 10

Run in Playground

Output

service.nameavg_sizespan_count
checkout2621443421
frontend2621444532
cart2621442987

This query converts size strings to bytes for numeric analysis of payload sizes across different services.

  • format_bytes: Formats numeric bytes as human-readable strings. Use this to reverse the parsing operation.
  • strlen: Returns the length of a string. Use this when you need string length rather than byte parsing.

Other query languages