How do I convert Long to byte[] and back in java


HOME ยป Java

How do I convert a long to a byte[] and back in Java?
I'm
trying convert a long to a byte[] so that I will be able to send the
byte[] over a tcp connection. On the other side I want to take that
byte[] and convert it back into a double.
Any tips would be
appreciated.

I'm trying to convert date of birth (three ints) to byte and
convert it back but I'm having an issue. I have to convert it by using
bit operations and send data over multicast server and receive it and
change back to int. Server works fine, but bit operations are hard for
me. What's the matter with the code:


Convert:

/> int D=12;
int M=9;
int
Y=1983;
short DMY=0;
DMY = (short)(DMY | (D
<< 19));
DMY = (short)(DMY | (M << 15));

DMY = (short)(DMY | Y);
byte[] data = new byte[3]; /> data[0] = (byte)(DMY >>> 8 );
data[1]
Programming Languages

Lazy programmer alert. :)


Cassandra stores column
values as bytes (Java example). Specifying a LongType comparator
compares those bytes as a long. I want the value of a long into a
Cassandra-friendly byte[]. How? I poked around for awhile. I think
you people can help me faster.


EDIT:


Both
Alexander and Eli's answers agreed with this reverse transformation.
Thanks!

Java

I believe conversion exactly to BigInteger[] would be optimal in my
case. Anyone had done or found this written in Java and willing to
share?


So imagine I have arbitrary size byte[] =
{0xff,0x3e,0x12,0x45,0x1d,0x11,0x2a,0x80,0x81,0x45,0x1d,0x11,0x2a,0x80,0x81}
/>How do I convert it to array of BigInteger's and then be able to
recover it back the original byte array safely?


ty in
advance.

Programming Languages

I want to achieve something in my android application. I need to
create a HEX representation of a String variable in my class and to
convert it to byte array. Something like this :


String
hardcodedStr = "SimpleText";
String hexStr =
someFuncForConvert2HEX(hardcodedStr); // this should be the HEX
string
byte[] hexStr2BArray = hexStr.getBytes();
/>

and after that I want to be able to convert this
hexStr2BArray to String and get it's value. Something
like this :


String hexStr = new String(hexStr2BArray,
"UTF-8");
String firstStr = someFuncConvertHEX2Str(hexStr); //
the result must be : "Si
Java

I am reading 8 bytes of data in from a hardware device. I need to
convert them into a numeric value. I think I want to convert them to
a long as that should fit 8 bytes. I am not very familiar with Java
and low level data type operations. I seem to have two problems (apart
from the fact there is almost no documentation for the hardware in
question), The bytes are expecting to be unsigned, so I can't do a
straight integer conversion. I am not sure what endianness they
are.


Any advice would be appreciated.


/>

Ended up with this (taken from some source code I probably should
have read a week ago):


public sta
Java

How do I convert a long to a byte[] and back in Java?
I'm
trying convert a long to a byte[] so that I will be able to send the
byte[] over a tcp connection. On the other side I want to take that
byte[] and convert it back into a double.
Any tips would be
appreciated.

Java

- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow