I would like to know which representation of a String (normal String or byte-array) would require more memory to be stored?
A byte-array representation is available to me in my code. Should I convert this to a String before transferring it over the network (in response to an AJAX call)?
Byte objects are sequence of Bytes, whereas Strings are sequence of characters. Byte objects are in machine readable form internally, Strings are only in human readable form. Since Byte objects are machine readable, they can be directly stored on the disk.
In . NET, a byte is basically a number from 0 to 255 (the numbers that can be represented by eight bits). So, a byte array is just an array of the numbers 0 - 255. At a lower level, an array is a contiguous block of memory, and a byte array is just a representation of that memory in 8-bit chunks.
// Code to convert byte arr to str: byte[] by_original = {0,1,-2,3,-4,-5,6}; String str1 = new String(by_original); System. out. println("str1 >> "+str1); // Code to convert str to byte arr: byte[] by_new = str1. getBytes(); for(int i=0;i<by_new.
For text or character data, we use new String(bytes, StandardCharsets. UTF_8) to convert the byte[] to a String directly. However, for cases that byte[] is holding the binary data like the image or other non-text data, the best practice is to convert the byte[] into a Base64 encoded string.
1 => see: What is the Java's internal represention for String? Modified UTF-8? UTF-16?
2 => multiple options if it's short, simply transfer a string if it's ascii, otherwise, convert it :
String serial= DatatypeConverter.printBase64Binary(bytes)
and you can use GET
decoding is possible with java, php, ...
if it's big, use POST, and binary (or native).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With