A single character might be represented by multiple bytes in certain encoding. Byte-based length checks are important for localizing to languages requiring different character encodings (e.g., UTF-8, UTF-16, ISO-8859-1).
Calculating string length in bytes also ensures accurate data handling, especially in systems with size constraints, network transmissions or storage limitations.
Calculate string length in bytes
The number of bytes can be calculated from base64 encoded strings. Here's how we use it with the EncodeAsBase64() formula using the base formula:
- Create a Formula column
- In the Insert formula field, add this:
Round
(3*(
Len
(
Substitute
(
EncodeAsBase64
(
Text_column
)),"=",""))/4,0)
Replace Text_column
with the actual name of the column that you want to calculate the length of.
Do byte-based length check
To expand on this, we can add a length check column to determine if your string length falls within the specified limit. In this example, if it adheres to the limitation, the cells are highlighted in green; otherwise, they are highlighted in red.
- Create a Formula column
- In the Insert formula field, add this:
If
(
Or
(
Max_length_column
<1,
Round
(3*(
Len
(
Substitute
(
EncodeAsBase64
(
Text_column
),"=",""))/4),0)<1),
ResultWithColor
("#0f0", "Ok"),
If
(
Round
(3*(
Len
(
Substitute
(
EncodeAsBase64
(
Text_column
),"=",""))/4),0)>
Max_length_column
,
ResultWithColor
("#f00", "Check"),
ResultWithColor
("#0f0", "Ok")))
Remember to replace:
Max_length_column
: the column containing the character limitation. This column must be a Number column type.
Text_column
: the column that you want to calculate the length of.
And here's the result: