Results 1 to 5 of 5
  1. #1
    ozpenstillson is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Byte encapsulation in String

    Due to restrictions on incoming data in my program, I need to take a byte array and encapsulate it in a string, meaning I need to do something like this (that doesn't break)

    Byte[] byteArray = "Encapsulate";
    String encap = (String) byteArray;

    I need to preserve the byteArray data, but wrap it in a String.

    Any help would be greatly appreciated, thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Byte encapsulation in String

    The String class has a constructor that takes a byte array.

    Can you give an example of what you want to do?
    Give a byte array with some values and show what String you want to get.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ozpenstillson is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: Byte encapsulation in String

    This example code:
    byte[] b = {116, 101};
    String toString = byteToString(b);
    System.out.println(toString);

    Produces this:
    "116101"
    Where the "" is in place to show it is a string

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Byte encapsulation in String

    That looks like you are converting the byte values as int values to their base 10 representation as a String.
    Look at the Integer class's toString() method.

    The trick will be for values < 100 (2 digits) and values < 10 (1 digit). They will have to be padded to get 3 digits.
    Last edited by Norm; 06-29-2012 at 07:30 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Byte encapsulation in String

    As Norm mentioned, you might have a problem parsing the string . An alternative would be to convert the byte to its binary representation as a String (the Integer class has a toBinaryString() method to convert an int to a binary representation - take the least significant 8 chars). You can convert back using the Integer.parseInt method with the appropriate base

Similar Threads

  1. Replies: 2
    Last Post: 11-21-2011, 01:08 AM
  2. Expressing a byte string
    By DeptOfMeteors in forum New To Java
    Replies: 14
    Last Post: 11-25-2010, 11:47 PM
  3. Converting string to byte[]
    By bobo67 in forum New To Java
    Replies: 12
    Last Post: 09-10-2010, 10:10 PM
  4. String from byte array
    By justint in forum New To Java
    Replies: 2
    Last Post: 01-22-2010, 07:58 AM
  5. String byte storage
    By bozovilla in forum New To Java
    Replies: 1
    Last Post: 11-24-2007, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •