Hi everyone,
I have a program that converts variables long,string,double to byte array here's the code :
for long : -
//CompanyId
-
temp = longToByteArray(CompanyId);
-
for (i=0,i2=7; i<5; i++,i2--)
-
buffer[position + i] = temp[i2];
-
-
private byte[] longToByteArray(long l)
-
{
-
byte[] bArray = new byte[8];
-
ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
-
LongBuffer lBuffer = bBuffer.asLongBuffer();
-
lBuffer.put(0, l);
-
return bArray;
-
}
-
for String : -
//CustomerCode
-
for (i=0; i <20; i++)
-
{
-
if (i < customerCode.length())
-
buffer[i + position] = (byte) customerCode.charAt(i);
-
else
-
buffer[i + position] = 0;
-
}
-
for double : -
-
// LastClaimPoints
-
temp = longToByteArray( Double.doubleToRawLongBits(LastClaimPoints));
-
for (i=0; i<8; i++)
-
buffer[position+i] = temp[i];
-
-
-
private byte[] longToByteArray(long l)
-
{
-
byte[] bArray = new byte[8];
-
ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
-
LongBuffer lBuffer = bBuffer.asLongBuffer();
-
lBuffer.put(0, l);
-
return bArray;
-
}
-
-
Now the problem is i don't know how to convert it back. Byte array back to its
different datatypes (long, String, double).
any help will be appreciated,
Regards,
Jeff
5 16121
Now the problem is i don't know how to convert it back. Byte array back to its
different datatypes (long, String, double).
any help will be appreciated,
Regards,
Jeff
You can wrap that byte array in a ByteBuffer again and use one of the getX()
methods that return your doubles, longs etc. again. check the API docs for that
class.
kind regards,
Jos
Hi Jos,
Can you show me how it's done? i have a sample code here that will retrieve a long datatype value from a specific index of the byte array... but the problem is, it returns a very long number value ex : 45622334413644464646 don't know how should i fix this... anyway here's the code... -
-
ByteBuffer a = ByteBuffer.wrap(buffer);
-
long testLong = a.getLong(buffer[1]);
-
System.out.println("Value : " + testLong);
-
-
i need to retrieve string, long and double.... thanks !
Regards,
Jeff
Hi Jos,
Can you show me how it's done? i have a sample code here that will retrieve a long datatype value from a specific index of the byte array... but the problem is, it returns a very long number value ex : 45622334413644464646 don't know how should i fix this... anyway here's the code... -
-
ByteBuffer a = ByteBuffer.wrap(buffer);
-
long testLong = a.getLong(buffer[1]);
-
System.out.println("Value : " + testLong);
-
-
i need to retrieve string, long and double.... thanks !
Regards,
Jeff
I think the parameter to the getLong( ... ) method is wrong: you're using 'bufffer'
to be wrapped in the ByteBuffer and you're also using buffer[1] as the index value.
Shouldn't it read something like 'getLong(1)'? In this case the buffer byte array
is read, starting at position 1, and a long value is constructed.
kind regards,
Jos
Hi Jos,
Thanks for helping... still i can't get what i want to get.... here's the datatype of the variables : -
byte FormatId; // byte (1)
-
long CompanyId; // long (5)
-
String CompanyName; // string (30)
-
byte customerLevelId; // byte (1)
-
String customerCode; // string (20)
-
String CustomerName; // string (50)
-
String Birthday; // string (6)
-
String ExpiryDate; // string (6)
-
long totalAmountSold; // long (8)
-
double balancePoints; // double (8)
-
String lastSaleDate; // string (6)
-
String LastClaimDate; // string (6)
-
double LastClaimPoints; // double (8)
-
long UpdateCount; // long (8)
-
and here's the value for each variables : -
// c.variable ? because variable is located in another class..... FYI only... ^^
-
c.FormatId = 1;
-
c.CompanyId = 127;
-
c.CompanyName = "Cripple";
-
c.customerLevelId = 1;
-
c.customerCode = "ABCDE12345ABCDE12345X";
-
c.CustomerName = "Victory Kups";
-
c.Birthday = "060685";
-
c.ExpiryDate = "060685";
-
c.totalAmountSold = 257;
-
c.balancePoints = 1.999;
-
c.lastSaleDate = "060107";
-
c.LastClaimDate = "060685";
-
c.LastClaimPoints = 100.00;
-
c.UpdateCount = 3;
-
I'm still trying other solutions for this..... never quit right? ehehe thanks man.
Best regards,
Jeff
It's been 4 years since last post, but I still found this thread ^^ So if anyone is wondering why it still wasn't working, it's just that arrays start at position 0, so you should use getLong(0). That gives the following code : - ByteBuffer a = ByteBuffer.wrap(buffer);
-
long testLong = a.getLong(0);
-
System.out.println("Value : " + testLong);
Sign in to post your reply or Sign up for a free account.
Similar topics
by: john |
last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case
labeling printer setting out on one of our production lines. Now then i have
a vb.net application that sends data to this...
|
by: plank |
last post by:
Hey Peeps,
Ok here is my situation.. I have a Java applet which allows the user to select files and upload them to the server. The
applet converts the file to Base64 and then POSTS the data to an...
|
by: Colin J. Williams |
last post by:
Python advertises some basic service:
C:\Python24>python
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
With...
|
by: baustin75 |
last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie
only when debugging in php designer 2005
--------------------------------------------------------------------------------
...
|
by: Pete Davis |
last post by:
I've never done this in C# so I don't know what the appropriate way of doing
it is.
I've got an array of bytes and I need to convert the array into "usable"
data. For example, the first 4 bytes...
|
by: moondaddy |
last post by:
I need to convert a byte array to a string and pass it as a parameter in a
URL and then convert it back to the original byte array. However, its
getting scrambled in the conversion. In short,...
|
by: ZillionDollarSadist |
last post by:
Hello,
I'm working at a simple Access '97 + VB4 application, and I ran into a
terrible problem: something I never modified now gives me a totally
unwanted "Invalid use of null" error.
It happens...
|
by: tbh |
last post by:
for historical reasons i need to be able to call, from C# under DotNet 2, as
COM+ DLL function that returns a "string" which is really an array of
seemingly arbitrary bytes (presumably non-zero)....
|
by: shahiz |
last post by:
basically im having null pointer exception
//read an inputstream
is = new DataInputStream(new FileInputStream("test.mpg"));
loadBytes(is);
//pass it as a datasource for the player
public...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |