IBM®
Skip to main content
    Country/region select      Terms of use
 
 
   
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

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.

%REM
       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)


 Comments

1) Nice!
Rich Schwartz | 2/13/2012 7:49:59 PM

I didn't know that LMBCS was recognized as a MIME charset.

2) Alternative?
Craig Schumann | 2/14/2012 9:24:15 AM

Couldn't you just use NLS_string_bytes()?

3) re: Finding the length of a LMBCS string
Andre Guirard | 2/14/2012 1:36:21 PM

@Craig, yes, that would also work and would likely be faster, but it seems rather more complicated because of the OS dependency.

4) what about LenB?
Charles Robinson | 2/16/2012 1:59:40 PM

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?

5) Why LenB doesn’t work
Andre Guirard | 2/21/2012 8:53:18 AM

@Charles, LotusScript string values are two-byte Unicode, so LenB just returns 2*the number of characters.

 Add a Comment
Subject:
   
Name:
Comment:  (No HTML - Links will be converted if prefixed http://)
 
Remember Me?     Cancel

Search this blog 

Disclaimer 

    About IBM Privacy Contact