473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help converting byte array to String and long

jeffbroodwar
118 100+
Hi everyone,

I have a program that converts variables long,string,double to byte array here's the code :

for long :

Expand|Select|Wrap|Line Numbers
  1.        //CompanyId
  2.        temp = longToByteArray(CompanyId);
  3.        for (i=0,i2=7; i<5; i++,i2--)
  4.            buffer[position + i] = temp[i2];
  5.  
  6.        private byte[] longToByteArray(long l) 
  7.        {
  8.           byte[] bArray = new byte[8];
  9.           ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
  10.           LongBuffer lBuffer = bBuffer.asLongBuffer();
  11.           lBuffer.put(0, l);
  12.           return bArray;
  13.         }
  14.  
for String :

Expand|Select|Wrap|Line Numbers
  1.          //CustomerCode
  2.         for (i=0; i <20; i++)
  3.         {
  4.            if (i < customerCode.length())
  5.                buffer[i + position] = (byte) customerCode.charAt(i);
  6.            else
  7.                buffer[i + position] = 0;
  8.         }
  9.  
for double :

Expand|Select|Wrap|Line Numbers
  1.  
  2.        // LastClaimPoints
  3.        temp = longToByteArray( Double.doubleToRawLongBits(LastClaimPoints));
  4.        for (i=0; i<8; i++)
  5.            buffer[position+i] = temp[i];
  6.  
  7.  
  8.        private byte[] longToByteArray(long l) 
  9.        {
  10.           byte[] bArray = new byte[8];
  11.           ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
  12.           LongBuffer lBuffer = bBuffer.asLongBuffer();
  13.           lBuffer.put(0, l);
  14.           return bArray;
  15.         }
  16.  
  17.  

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
Jun 22 '07 #1
5 16121
JosAH
11,448 Expert 8TB
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
Jun 22 '07 #2
jeffbroodwar
118 100+
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...

Expand|Select|Wrap|Line Numbers
  1.  
  2.         ByteBuffer a = ByteBuffer.wrap(buffer);
  3.         long testLong = a.getLong(buffer[1]);
  4.         System.out.println("Value : " + testLong);
  5.  
  6.  
i need to retrieve string, long and double.... thanks !

Regards,
Jeff
Jun 25 '07 #3
JosAH
11,448 Expert 8TB
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...

Expand|Select|Wrap|Line Numbers
  1.  
  2.         ByteBuffer a = ByteBuffer.wrap(buffer);
  3.         long testLong = a.getLong(buffer[1]);
  4.         System.out.println("Value : " + testLong);
  5.  
  6.  
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
Jun 25 '07 #4
jeffbroodwar
118 100+
Hi Jos,

Thanks for helping... still i can't get what i want to get.... here's the datatype of the variables :

Expand|Select|Wrap|Line Numbers
  1.     byte   FormatId;         // byte (1) 
  2.     long   CompanyId;        // long (5)
  3.     String CompanyName;      // string (30)
  4.     byte   customerLevelId;  // byte (1)  
  5.     String customerCode;     // string (20)
  6.     String CustomerName;     // string (50)
  7.     String Birthday;         // string (6)
  8.     String ExpiryDate;       // string (6)
  9.     long   totalAmountSold;  // long (8)
  10.     double balancePoints;    // double (8)
  11.     String lastSaleDate;     // string (6)
  12.     String LastClaimDate;    // string (6)
  13.     double LastClaimPoints;  // double (8)
  14.     long   UpdateCount;      // long (8)
  15.  
and here's the value for each variables :

Expand|Select|Wrap|Line Numbers
  1.         // c.variable ? because variable is located in another class..... FYI only... ^^
  2.         c.FormatId = 1;
  3.         c.CompanyId = 127;
  4.         c.CompanyName = "Cripple";
  5.         c.customerLevelId = 1;
  6.         c.customerCode = "ABCDE12345ABCDE12345X";
  7.         c.CustomerName = "Victory Kups";
  8.         c.Birthday = "060685";
  9.         c.ExpiryDate = "060685";
  10.         c.totalAmountSold = 257;
  11.         c.balancePoints = 1.999;
  12.         c.lastSaleDate = "060107";
  13.         c.LastClaimDate = "060685";
  14.         c.LastClaimPoints = 100.00;
  15.         c.UpdateCount = 3;
  16.  
I'm still trying other solutions for this..... never quit right? ehehe thanks man.


Best regards,
Jeff
Jun 27 '07 #5
Aeren
1
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 :

Expand|Select|Wrap|Line Numbers
  1. ByteBuffer a = ByteBuffer.wrap(buffer);
  2. long testLong = a.getLong(0);
  3. System.out.println("Value : " + testLong);
Nov 21 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
2
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...
3
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...
8
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 -------------------------------------------------------------------------------- ...
3
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...
8
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,...
43
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...
8
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)....
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.