Why does byteArray have a length of 22 instead of 20?

We try to convert from string to Byte[] using the following Java code:

String source = "0123456789";
byte[] byteArray = source.getBytes("UTF-16");

We get a byte array of length 22 bytes, we are not sure where this padding comes from. How do I get an array of length 20?


Asked by: Freddie279 | Posted: 21-01-2022






Answer 1

Alexander's answer explains why it's there, but not how to get rid of it. You simply need to specify the endianness you want in the encoding name:

String source = "0123456789";
byte[] byteArray = source.getBytes("UTF-16LE"); // Or UTF-16BE

Answered by: Kimberly590 | Posted: 22-02-2022



Answer 2

May be the first two bytes are the Byte Order Mark. It specifies the order of bytes in each 16-bit word used in the encoding.

Answered by: Charlie256 | Posted: 22-02-2022



Answer 3

Try printing out the bytes in hex to see where the extra 2 bytes are added - are they at the start or end?

I'm picking that you'll find a byte order marker at the start (0xFEFF) - this allows anyone consuming (receiving) the byte array to recognise whether the encoding is little-endian or big-endian.

Answered by: Madaline982 | Posted: 22-02-2022



Answer 4

UTF has a byte order marker at the beginning that tells that this stream is encoded in a particular format. As the other users have pointed out, the
1st byte is 0XFE
2nd byte is 0XFF
the remaining bytes are
0
48
0
49
0
50
0
51
0
52
0
53
0
54
0
55
0
56
0
57

Answered by: Sam451 | Posted: 22-02-2022



Similar questions

bytearray - How can I access a byte array as shorts in Java

I have a an array of byte, size n, that really represents an array of short of size n/2. Before I write the array to a disk file I need to adjust the values by adding bias values stored in another array of short. In C++ I would just assign the address of the byte array to a pointer for a short array with a cast to short and use pointer arithmetic or use a union. How may this be done in Java - I'm very new to Java...


bytearray - Filling a byte array in Java

For part of a project I'm working on I am implementing a RTPpacket where I have to fill the header array of byte with RTP header fields. //size of the RTP header: static int HEADER_SIZE = 12; // bytes //Fields that compose the RTP header public int Version; // 2 bits public int Padding; // 1 bit public int Extension; // 1 bit public int CC; // 4 bits public int Marker; // 1 bit public int...


java - How to update bytearray in C through JNI without returning a bytearray

Just wondering whether it is possible to update a ByteArray in C Code, which is created in Java, without returning it from C. I have situation, where I need to update a single bytearray for multiple times through JNI and returning bytearray from C takes lot of JNI calls. Please let me know if anybody knows how to do this? Code should be something like this Java Code byte[] storeData;...


bytearray - What is the best way to implement a circular array byte shift in java?

If the shifting is not always the same, i.e I may have to use the same function to resize 2 or 4 characters, what would be a good way to circular shift the values of an array of bytes 2 positions * a parameter? This is what I have so far for(int j=0; j<param; j++){ if(j == 0){ for(int i=0; i<myArray.length;i++){ result[i] = (byte) (myArray[i]<<2); ...


Convert ByteArray to IntArray java

I have an ByteArrayOutputStream connected with a Dataline from an AudioSource. I need to convert the Stream in some significative values that probally are the sound values taken from source or not ? Well then how can I conver the byteArray (from ByteArrayOutStream.getByteArray()) in a intArray?. I googled it but with no luck . p.s. the audioFormat that I used is : PCM_S...


java - Taking a bytearray and creating and writing a JPEG

I have a program that Accepts an encoded base64 string Converts it to a byte array. It does this fine. The final step involves writing this byte array to a file. For example C:\example.jpg. I know simply writing the bytes won't work so was not sure what I would need to do to take the byte array and create a jpg with the picture that is coming in. I have to actually sen...


bytearray - Java Array of Bytes

If I create an array of bytes with byte[], what would be the size of each element? Can they be resized/merged? Thanks,


bytearray - Java - Storing File on FTP Server fails

I am trying to store a byteArrayInputStream as File on a FTP Server. I could already connect to the Server and change the working path, but triggering the method to store the Stream as File on the Server returns always false. I am using the apache FTPClient. Can someone please give me a hint where my mistake can be!? Here the Code: String filen...


bytearray - Efficient way to split a byte array to map the fields in a java class object

Is there a more efficient way to break the data in a byte array in Java? I have written the following function to read a binary file with fixed length data field. But the performance are really slow, I need to read a binary file with 30,000 records each with the length of 300 bytes, and each record contain 240 fields. Any advise? public void breakField(byte[] input) { ByteArrayInputStream bais = new By...


c# - zeromq - bytearray (from .net server) to string in java

I'm working on a project that uses both .net and java, using zeromq to communicate between them. I can connect to the .net server, however when I try to convert the byte array to a string strange things happen. In the eclipse debugger I can see the string, and its length. When I click on the string its value changes to being only the first letter, and the length changes to 1. In the eclipse console when I try to co...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top