Encode a string and write it into the buffer at the given offset, optionally truncating the resulting bytes to match a specific length.