Today I needed to use LotusScript to find the length of a string, in bytes. It took a little experimentation, so I figured I should post it here in case anyone else might sometime find it useful. This is in LotusScript; there's probably an easier way in Java, but I needed it for a form validation.
Function LmbcsLen
Description: Return the number of bytes in a string if expressed in LMBCS character set.
Strings in LotusScript are in Unicode, but are converted to LMBCS when stored
in items or passed to C API functions.
%END REM
Function LmbcsLen(x$) As Long
Dim session As New NotesSession, db As NotesDatabase, docTmp As NotesDocument
Dim mime As NotesMIMEEntity, streamIn As NotesStream, streamOut As NotesStream
Set db = session.CurrentDatabase
Set docTmp = db.CreateDocument
Set mime = docTmp.CreateMIMEEntity("Body")
Set streamIn = session.CreateStream
streamIn.WriteText x
streamIn.Position = 0
Call mime.SetContentFromText(streamIn, "text/plain;charset=LMBCS", ENC_NONE)
Set streamOut = session.CreateStream
mime.getContentAsBytes streamOut, False
LmbcsLen = streamOut.Bytes
End Function
Andre Guirard | 13 February 2012 06:06:17 PM ET | | Comments (6)
I didn't know that LMBCS was recognized as a MIME charset.
Couldn't you just use NLS_string_bytes()?
@Craig, yes, that would also work and would likely be faster, but it seems rather more complicated because of the OS dependency.
LotusScript has LenB, which returns the number of bytes. { Link } . Are you using this function so you can set your own code page, or is LenB not compatible with LMBCS?
@Charles, LotusScript string values are two-byte Unicode, so LenB just returns 2*the number of characters.
