public static byte[] getDirectAddressBytes(Program program, Address toAddress) {

		Memory memory = program.getMemory();
		boolean isBigEndian = memory.isBigEndian();

		int addrSize = toAddress.getSize();

		DataConverter dataConverter = DataConverter.getInstance(memory.isBigEndian());
		byte[] addressBytes = new byte[addrSize / 8];

		if (toAddress instanceof SegmentedAddress) {
			// Only search for offset (exclude segment)
			addressBytes = new byte[2];
			SegmentedAddress segAddr = (SegmentedAddress) toAddress;
			short addrShort = (short) segAddr.getSegmentOffset();
			dataConverter.getBytes(addrShort, addressBytes);
		}
		else if (addrSize == 64) {
			long addrLong = toAddress.getAddressableWordOffset();
			dataConverter.getBytes(addrLong, addressBytes);
		}
		else if (addrSize == 32) {
			int addrInt = (int) toAddress.getAddressableWordOffset();
			dataConverter.getBytes(addrInt, addressBytes);
		}
		else if (addrSize == 16) {
			short addrShort = (short) toAddress.getAddressableWordOffset();
			dataConverter.getBytes(addrShort, addressBytes);
		}
		else {
			// Handle any addrSize the hard way
			byte[] tempBytes = new byte[8];
			long addrLong = toAddress.getAddressableWordOffset();
			dataConverter.getBytes(addrLong, tempBytes);
			System.arraycopy(tempBytes, isBigEndian ? (tempBytes.length - addressBytes.length) : 0,
				addressBytes, 0, addressBytes.length);
		}

		return addressBytes;
	}
Get a representation of an address as it would appear in bytes in memory.

@param program program
@param toAddress target address
@return byte representation of toAddress
Ghidra/Features/Base/src/main/java/ghidra/program/util/ProgramMemoryUtil.java