ByteBuffer - convert long to char array and vice versa

live-love

I am trying to convert a long to a 4 char array.

It seems like the last char is not being written to the buffer.

Why is this not working?

//convert long to char array

long longValZ = 219902744986400000L;

ByteBuffer bbX = ByteBuffer.allocate(8);
bbX.putLong(longValZ); 
//longValZ = 219902744986400000

char [] charArr1 = new char[4];
charArr1[0] = bbX.getChar(0);
charArr1[1] = bbX.getChar(1);
charArr1[2] = bbX.getChar(2);
charArr1[3] = bbX.getChar(3);

long longValX = bbX.getLong(0); 
//longValX = 219902744986400000


//convert char array to long

ByteBuffer bbY = ByteBuffer.allocate(8);

bbY.putChar(0,charArr1[0]);
bbY.putChar(1,charArr1[1]);
bbY.putChar(2,charArr1[2]);
bbY.putChar(3,charArr1[3]);

long longValY = bbY.getLong(0); 
//longValY = 219902744985600000 --> why is longValY different than longValZ ?
Peter Lawrey

You don't need to provide the index

long longValZ = 219902744986400000L;

ByteBuffer bbX = ByteBuffer.allocate(8);
bbX.putLong(longValZ); 

char[] charArr1 = new char[4];
for (int i = 0; i < charArr1.length; i++)
   charArr1[i] = bbX.getChar();



//convert char array to long

ByteBuffer bbY = ByteBuffer.allocate(8);
for (int i = 0; i < charArr1.length; i++)
    bbY.putChar(charArr1[i]);

long longValY = bbY.getLong(0); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert struct array to cell of structs and vice versa

Convert a byte array to integer in Java and vice versa

Convert file to byte array and vice versa

How to convert numpy array to string and vice versa

How to convert byte array to string and vice versa?

How to convert a float into a byte array and vice versa?

How to convert from stringstream to unsigned char vector and vice versa?

C: Converting unsigned char array to signed int (vice versa)

Cannot convert String to byte array and vice versa in Java

How do you convert a byte array to a hexadecimal string, and vice versa?

Convert the string containing array and dict into the list and vice versa in c#

Convert ListView to Line separated String Array and vice versa

How to convert string into numpy array for tensorflow and vice versa

Convert binary to ASCII and vice versa

BitmapImage to byte array and vice versa

SAS: What is the quickest way to convert a dataset compressed in binary to compressed using char or vice versa?

How to convert 8-char string to a 64-bit bigint and vice versa?

Long integer to string and vice versa, operation with digits

Casting a bitset to unsigned char, and vice versa

Converting char to binary strings and vice-versa

How to access a char array and change lower case letters to upper case, and vice versa

Convert a decimal number saved in an array into a binary number saved in an array, and vice versa

What is the most efficient way to convert array of int to array of byte and vice versa?

How to convert a ByteBuffer to long[] in Java

Convert List<int> to string and vice versa?

How to convert integers to pointers and vice versa

Convert from hex to int and vice versa in java

Metafunction to convert a type to an integer and vice-versa

How to convert String[] to String and vice versa in Android