×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    19

    Byte[] to string

    Hi!

    Does anyone of you know, whats the most simple way to convert a bytearray to a string in j2me. I know the ByteToCharUTF8() function from j2se, but it is not implemented in j2me. A possible way would be to implement it by myself, but I want to be sure, that there is no implementation already I have not seen.

    thanks for your help!
    sgie

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    67

    Re: Byte[] to string

    You could use:

    String s = new String(bytes);

    if you want to use the default encoding or:

    String s = new String(bytes, encoding);

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Re: Byte[] to string

    If you want to encode 128-255 bytes to 128-255 chars on all phones, so you could get array back with String.getBytes(), you should do more work:

    private static final String ENCODINGSTR = "ISO-8859-1";
    private static byte SupportsEncoding;

    //===============================================
    //CheckEncoding()
    //===============================================
    private static final void CheckEncoding()
    {

    //SupportsEncoding:
    //0 = new String(buf,len)
    //1 = new String(buf,len,ENCODINGSTR)
    //2 = StringBuffer.ToString()

    byte[] b = new byte[1];
    b[0]=(byte)178;

    try
    {
    String s = new String(b,0,1,ENCODINGSTR);
    if (s==null) throw new Exception();
    if (s.charAt(0)!=178)
    {
    SupportsEncoding=2;
    }
    else
    {
    SupportsEncoding=1;
    }
    }
    catch (Exception e)
    {
    String s = new String(b,0,1);

    if (s==null || s.length()!=1 || s.charAt(0)!=178)
    {
    SupportsEncoding=2;
    }
    else
    {
    SupportsEncoding=0;
    }
    }
    }


    //===============================================
    // .StringToByteArray
    //===============================================
    public static final byte[] StringToByteArray(String s)
    {
    switch (SupportsEncoding)
    {
    case 0:
    default:
    return s.getBytes();

    case 1:
    {
    try
    {
    return s.getBytes(ENCODINGSTR);
    }
    catch (Exception e)
    {
    return null;
    }
    }

    case 2:
    {
    int len = s.length();
    byte[] b = new byte[len];
    for (int i=0;i<len;i++)
    {
    b[i]=(byte)s.charAt(i);
    }
    return b;
    }
    }
    }

    //===============================================
    // .ByteArrayToString
    //===============================================
    public static final String ByteArrayToString(byte[] buf, int off, int len)
    {
    switch (SupportsEncoding)
    {
    default:
    case 0:
    {
    return new String(buf,off,len);
    }

    case 1:
    {
    try
    {
    return new String(buf,off,len,ENCODINGSTR);
    }
    catch (Exception e)
    {
    return null;
    }
    }

    case 2:
    {
    StringBuffer sb = new StringBuffer(len);
    for (int i=0;i<len;i++)
    {
    sb.append((char)(buf[i] & 0xff));
    }
    return sb.toString();
    }
    }
    }

Similar Threads

  1. global root overflow(please help)
    By sushant_125 in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2004-05-14, 08:24
  2. Push SMS
    By BobbyFletcher in forum General Messaging
    Replies: 2
    Last Post: 2004-04-28, 03:05
  3. PC Connectity with VB6
    By mkintanar in forum PC Suite API and PC Connectivity SDK
    Replies: 5
    Last Post: 2003-09-24, 04:18
  4. Personal
    By JSmith77 in forum PersonalJava
    Replies: 4
    Last Post: 2003-07-30, 12:52
  5. D211 new linux driver 1.04 not working !!
    By franz_meyer in forum Multimodecards
    Replies: 1
    Last Post: 2002-12-12, 08:31

Posting Permissions

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