StringByteLength

Got an idea for enhancing SpiderBasic? New command(s) you'd like to see?
Dirk Geppert
Posts: 330
Joined: Fri Sep 22, 2017 7:02 am

StringByteLength

Post by Dirk Geppert »

The StringByteLength function is required in particular for string processing in memory, such as Base64 encoding and decoding, if the text contains emojis or other Unicode characters.

Code: Select all

Procedure.i StringByteLength(str.s)
  Protected n.i
    
  !  const encoder = new TextEncoder();
  !  const byteArray = encoder.encode(v_str);
  !  // Die Länge des Byte-Arrays entspricht der Anzahl der Bytes, die der String benötigt
  !  v_n = byteArray.length;
  ProcedureReturn n
EndProcedure


txt.s = RSet(txt, 100, Chr(8211))

Debug Len(txt)

Debug StringByteLength(txt)

Quin
Posts: 118
Joined: Wed Nov 08, 2023 4:38 pm

Re: StringByteLength

Post by Quin »

+1
Nice function by the way, it works great. Thanks for introducing me to TextEncoder :)
juror
Posts: 18
Joined: Fri Aug 01, 2014 4:39 am

Re: StringByteLength

Post by juror »

Another +1

Thanks for sharing
Post Reply