Connecting Tech Pros Worldwide Forums | Help | Site Map

Help converting byte array to String and long

jeffbroodwar's Avatar
Member
 
Join Date: Oct 2006
Posts: 118
#1: Jun 22 '07
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

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jun 22 '07

re: Help converting byte array to String and long


Quote:

Originally Posted by jeffbroodwar

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
jeffbroodwar's Avatar
Member
 
Join Date: Oct 2006
Posts: 118
#3: Jun 25 '07

re: Help converting byte array to String and long


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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 25 '07

re: Help converting byte array to String and long


Quote:

Originally Posted by jeffbroodwar

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
jeffbroodwar's Avatar
Member
 
Join Date: Oct 2006
Posts: 118
#5: Jun 27 '07

re: Help converting byte array to String and long


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
Reply