Help converting byte array to String and long

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeffbroodwar
    New Member
    • Oct 2006
    • 118

    Help converting byte array to String and long

    Hi everyone,

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

    for long :

    Code:
           //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 :

    Code:
             //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 :

    Code:
           // 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
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • jeffbroodwar
      New Member
      • Oct 2006
      • 118

      #3
      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 : 456223344136444 64646 don't know how should i fix this... anyway here's the code...

      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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        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 : 456223344136444 64646 don't know how should i fix this... anyway here's the code...

        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

        Comment

        • jeffbroodwar
          New Member
          • Oct 2006
          • 118

          #5
          Hi Jos,

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

          Code:
              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 :

          Code:
                  // 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

          Comment

          • Aeren
            New Member
            • Nov 2011
            • 1

            #6
            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 :

            Code:
            ByteBuffer a = ByteBuffer.wrap(buffer);
            long testLong = a.getLong(0);
            System.out.println("Value : " + testLong);

            Comment

            Working...