Results 1 to 10 of 10
  1. #1
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default convert byte array to string.

    How to convert byte array to string..below codes return unknown strings..

    Java Code:
     public String getLocalIPAddress() {
            String ret = "";
            try {
                InetAddress addr = InetAddress.getLocalHost();
                byte[] ipAddr = addr.getAddress();
                ret = ipAddr.toString();
            } catch (UnknownHostException ex) {
                this.msgBox(null, ex.toString(),  Class1.MESSAGE_ERROR);
            }
            return ret;
        }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by newbiejava View Post
    How to convert byte array to string..below codes return unknown strings..

    Java Code:
     public String getLocalIPAddress() {
            String ret = "";
            try {
                InetAddress addr = InetAddress.getLocalHost();
                byte[] ipAddr = addr.getAddress();
                ret = ipAddr.toString();
            } catch (UnknownHostException ex) {
                this.msgBox(null, ex.toString(),  Class1.MESSAGE_ERROR);
            }
            return ret;
        }
    Arrays (and so byte arrays) don't have methods; you could have a look at the Arrays utility class and see if it can solve your problem.

    kind regards,

    Jos

  3. #3
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    tried below.. same problem occurs :<(

    byte[] ipAddr = addr.getAddress();
    ret = String.valueOf(ipAddr);

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    Have you looked at the String class's constructors? Some of them take a byte array.

    Do you know how you want the bytes to be converted to a String?
    Do you want a hex representation? ie 2 hex digits for each byte
    Or treat each 4 bytes as an int and convert that to a decimal number.

    Can you show the input bytes and what you'd like to see output as a String?

    What about: addr.getHostAddress()
    Last edited by Norm; 07-30-2010 at 04:11 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by newbiejava View Post
    tried below.. same problem occurs :<(

    byte[] ipAddr = addr.getAddress();
    ret = String.valueOf(ipAddr);
    Sure, Math.cos(ipAddr) doesn't make any sense either; have you read my previous reply?

    kind regards,

    Jos

  6. #6
    jnadelman is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    This doesn't work?
    byte[] ipAddr = addr.getAddress();
    String ret = new String(ipAddr);

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    What do you mean by "work"?
    Executes without error?
    Produces desired result?

  8. #8
    jnadelman is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Both. It seems newbie wants to "convert byte array to string," which my suggestion should accomplish.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    Not if the bytes do not represent chars in a String.
    If you read the API doc for the source of the bytes, you'll see what is in the bytes and why your solution makes no sense.

    There is a method that will return a String for the address vs the byte array. The OP probably should use that one.

  10. #10
    jnadelman is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. String byte array
    By myka in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 02:33 AM
  3. String from byte array
    By justint in forum New To Java
    Replies: 2
    Last Post: 01-22-2010, 06:58 AM
  4. How Convert a Byte array to a image format
    By perlWhite in forum New To Java
    Replies: 2
    Last Post: 08-23-2009, 08:11 PM
  5. How Convert a Byte array to a image format
    By perlWhite in forum Advanced Java
    Replies: 1
    Last Post: 08-22-2009, 07:05 PM

Posting Permissions

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