private byte[] checkAndEncodeLayout(ByteBuffer encoded) {
		switch (stringLayout) {
			case CHAR_SEQ:
			case FIXED_LEN: {
				if (length != -1 && encoded.limit() > length) {
					throw new IllegalArgumentException("Encoded string does not fit");
				}
				byte[] result = new byte[length != -1 ? length : encoded.limit()];
				encoded.get(result, 0, encoded.limit());
				return result;
			}
			case NULL_TERMINATED_BOUNDED: {
				if (length != -1 && encoded.limit() + charSize > length) {
					throw new IllegalArgumentException("Encoded string does not fit");
				}
				byte[] result = new byte[encoded.limit() + charSize];
				encoded.get(result, 0, encoded.limit());
				return result;
			}
			case NULL_TERMINATED_UNBOUNDED: {
				byte[] result = new byte[encoded.limit() + charSize];
				encoded.get(result, 0, encoded.limit());
				return result;
			}
			case PASCAL_255: {
				if (!fitsIn(encoded, StringDataInstance.SIZEOF_PASCAL255_STR_LEN_FIELD)) {
					throw new IllegalArgumentException("Encoded string does not fit");
				}
				ByteBuffer result = allocBuf(buf,
					encoded.limit() + StringDataInstance.SIZEOF_PASCAL255_STR_LEN_FIELD);
				result.put((byte) encoded.limit());
				result.put(encoded);
				return result.array();
			}
			case PASCAL_64k: {
				if (!fitsIn(encoded, StringDataInstance.SIZEOF_PASCAL64k_STR_LEN_FIELD)) {
					throw new IllegalArgumentException("Encoded string does not fit");
				}
				ByteBuffer result = allocBuf(buf,
					encoded.limit() + StringDataInstance.SIZEOF_PASCAL64k_STR_LEN_FIELD);
				result.putShort((short) encoded.limit());
				result.put(encoded);
				return result.array();
			}
			default: {
				throw new IllegalArgumentException("Unknown string layout");
			}
		}
	}
Parse and encode a single character from its representation to replace the current value

@param repr the representation of a single character
@return the encoded value
@throws StringParseException if the representation could not be parsed
@throws UnmappableCharacterException if a character could not be encoded
@throws MalformedInputException if the input contains invalid character sequences
Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.java