Reputation: 26597
I need to convert 2 longs to corresponding byte[], and then combine them with a delimeter in between. What is the most performant way to do this ? (I need to store resultant byte[] in the database.)
like I have-
long a=5324565343L
long b=423456L
then I want the byte[] representation of :-
5324565343-423456
EDIT: Couldnt understand what was actually unclear but still for clarification,
I intially have 2 long
(s) & I just want a byte[] of their concatenated form.
Upvotes: 0
Views: 130
Reputation: 718906
I need to place it with a really high performance code in my application.
No you don't.
Consider this:
The time you will save by avoiding the creation of Strings is likely to be less than a microsecond.
The time taken to perform an SQL insert over a JDBC connection is likely to be measured in milliseconds.
In other words optimizing this particular operation is likely to have negligible effect on the overall performance of your application.
Upvotes: 1
Reputation: 3078
Use ByteBuffer from java.nio.ByteBuffer:
long a = 5324565343L;
long b = 423456L;
byte[] bytes = new byte[18];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.putLong(a);
buffer.putChar('-');
buffer.putLong(b);
Upvotes: 2
Reputation: 23629
Concantenate them and call getBytes()
on the resulting String.
Something like:
(a + "-" + b).getBytes();
Upvotes: 1