Results 1 to 6 of 6
  1. #1
    Joe2003 is offline Member
    Join Date
    Jan 2008
    Posts
    21
    Rep Power
    0

    Default String array to byte array?!

    Hey

    If I have to following array:-

    Java Code:
    String[] someArray = 
    {
        "ff","ff","ff","ff","ff","ff",
        "ff","ff","ff","ff","ff","ff"
    };
    How can I turn it into a byte array?

    thanks

    Joe

  2. #2
    only2008 is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default Hi

    someArray.getbytes(0 willl return byte array..

    regards,
    ibrahim

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you mean to a byte array turning the String into the hex byte equivalents? I think that there is no other way than to create the byte array and then loop through the String array parsing each String as you go and placing each byte into the byte array. I don't see any short-cuts here.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default String vs Bytes

    I don't think that only2008's solution will work as posted. The getBytes() method is a string method, not an array method.
    I can think of two ways to do it:
    • The method Fubarable suggested
    • convert/write the string array to a string and then use the getBytes() method.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
    byte[] anotherArray = new byte[someArray.length];
    
    int pos = 0;
    for(String s : someArray) {
      anotherArray[pos++] = Byte.decode("#"+s);
    }

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    have you tried compiling this? #ff = 255
    Java Code:
    s:\tmp>java TestB
    Exception in thread "main" java.lang.NumberFormatException: Value out of range.
    Value:"ff" Radix:16
            at java.lang.Byte.parseByte(Unknown Source)
            at java.lang.Byte.valueOf(Unknown Source)
            at java.lang.Byte.decode(Unknown Source)
            at TestB.main(TestB.java:12)
    it worked fine when i changed byte to short.
    Byte.MAX/MIN_VALUE = -128 ... 127

    edit:
    Byte.parseByte(String,radix) also works (still overflow...)

    edit2:
    getBytes() solution didn't work because it returns the character's ascii dec value. which is F NOT FF.
    Last edited by angryboy; 02-28-2009 at 06:42 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Sending an ArrayList over UDP, as a byte array.
    By eggmanpete in forum Advanced Java
    Replies: 9
    Last Post: 11-12-2010, 12:37 PM
  2. Printing Byte Array
    By suchismitasuchi in forum New To Java
    Replies: 3
    Last Post: 01-19-2009, 10:58 AM
  3. Byte Array
    By sandor in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 03:31 AM
  4. Replies: 1
    Last Post: 07-04-2008, 05:39 PM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM

Posting Permissions

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