That's basically serializing. Dump the long values into an array of bytes and then encode it into a compatible representation, such as Base64:
import java.util.Base64;
public String encodeLongs(long[] numbers) {
byte[] bytes = new byte[8 * numbers.length];
for (int i = 0; i < numbers.length; i++) {
// Taken from https://stackoverflow.com/questions/18687772/java-converting-long-to-bytes-which-approach-is-more-efficient
long v = numbers[i];
int idx = i * 8;
bytes[idx + 0] = (byte)(v >>> 56);
bytes[idx + 1] = (byte)(v >>> 48);
bytes[idx + 2] = (byte)(v >>> 40);
bytes[idx + 3] = (byte)(v >>> 32);
bytes[idx + 4] = (byte)(v >>> 24);
bytes[idx + 5] = (byte)(v >>> 16);
bytes[idx + 6] = (byte)(v >>> 8);
bytes[idx + 7] = (byte)(v >>> 0);
}
return Base64.getEncoder().encodeToString(bytes);
}
You can also return a byte array instead of a String if that is more convenient for you. Base64 encode incurs in an overhead of around 1/3 of the original size (asuming you use UTF-8 or similar encoding). Note that it is not possible to have zero overhead in general if you are using a text-based format, although you may investigate other encodings such as Base-122, although Base64 has the advantage of being ubiquitous and already implemented in most languages.
Another option would be to compress the byte array first (for example with GZIP) and encode it in Base64 afterwards. Depending on the size of the input, the nature of your numbers (e.g. whether they tend to be in a certain range or not) and the compression algorithm you may have more or less success, but if the numbers are randomly distributed across the whole range of long numbers you will probably not be able to compress a lot.