def write_utf16be_string(*args)
    # Check for a cell reference in A1 notation and substitute row and column
    args = row_col_notation(args)

    return -1 if args.size < 3                  # Check the number of args

    record      = 0x00FD                        # Record identifier
    length      = 0x000A                        # Bytes to follow

    row, col, str = args
    xf          = xf_record_index(row, col, args[3]) # The cell format
    encoding    = 0x1
    str_error   = 0

    # Check that row and col are valid and store max and min values
    return -2 unless check_dimensions(row, col) == 0

    # Limit the utf16 string to the max number of chars (not bytes).
    if str.bytesize > 32767* 2
      str       = str[0..32767*2]
      str_error = -3
    end

    num_bytes = str.bytesize
    num_chars = (num_bytes / 2).to_i

    # Check for a valid 2-byte char string.
    raise "Uneven number of bytes in Unicode string" unless num_bytes % 2 == 0

    # Change from UTF16 big-endian to little endian
    str = utf16be_to_16le(str)

    # Add the encoding and length header to the string.
    str_header  = [num_chars, encoding].pack("vC")
    str         = str_header + str

    str_unique = update_workbook_str_table(str)

    header = [record, length].pack("vv")
    data   = [row, col, xf, str_unique].pack("vvvV")

    # Store the data or write immediately depending on the compatibility mode.
    store_with_compatibility(row, col, header + data)

    str_error
  end
def write_utf16be_string(*args)
    # Check for a cell reference in A1 notation and substitute row and column
    args = row_col_notation(args)

    return -1 if args.size < 3                  # Check the number of args

    record      = 0x00FD                        # Record identifier
    length      = 0x000A                        # Bytes to follow

    row, col, str = args
    xf          = xf_record_index(row, col, args[3]) # The cell format
    encoding    = 0x1
    str_error   = 0

    # Check that row and col are valid and store max and min values
    return -2 unless check_dimensions(row, col) == 0

    # Limit the utf16 string to the max number of chars (not bytes).
    if str.bytesize > 32767* 2
      str       = str[0..32767*2]
      str_error = -3
    end

    num_bytes = str.bytesize
    num_chars = (num_bytes / 2).to_i

    # Check for a valid 2-byte char string.
    raise "Uneven number of bytes in Unicode string" unless num_bytes % 2 == 0

    # Change from UTF16 big-endian to little endian
    str = utf16be_to_16le(str)

    # Add the encoding and length header to the string.
    str_header  = [num_chars, encoding].pack("vC")
    str         = str_header + str

    str_unique = update_workbook_str_table(str)

    header = [record, length].pack("vv")
    data   = [row, col, xf, str_unique].pack("vvvV")

    # Store the data or write immediately depending on the compatibility mode.
    store_with_compatibility(row, col, header + data)

    str_error
  end
:call-seq:
   write_utf16be_string(row, col, string, format)

 Write a Unicode string to the specified row and column (zero indexed).
 $format is optional.
 Returns  0 : normal termination
         -1 : insufficient number of arguments
         -2 : row or column out of range
         -3 : long string truncated to 255 chars