Suppose you need to copy a string into a structure that is used by a function. The function expects the string to be composed of single-byte characters. dBL strings are double-byte, so you will need to use setByte( ) to copy each character of the string into the structure string, byte-by-byte.

The following function copies a string into a structure string at the specified offset, and pads the rest of the length in the structure will null characters.

function setStructString( cStruct, nIndex, cValue, nChars )

   if argcount( ) < 4 

      nChars = len( cValue ) // Default length is length of string 

   endif 

   local n 

   for n = 0 to min( len( cValue ), nChars ) - 1 

      cStruct.setByte( nIndex + n, asc( cValue.charAt( n )) ) 

   endfor 

   do while n < nChars // Pad length with null characters 

      cStruct.setByte( nIndex + n++, 0 ) 

   enddo 

In the FOR loop, the MIN( ) function is used to copy all the characters in the string, or the specified number of characters, whichever is less. This means that you can safely pass a string of any length to the function, and as long as you specify the correct length, you don’t have to worry about the string being too long. Each character is extracted with the charAt( ) method and converted to its ASCII value (a number from 0 to 255) with the ASC( ) function.