×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    How to read individual bytes from a byte array

    Hi

    I have a byte array whose individual bytes need to be read as they refer to some parameters. My byte array looks like:
    Code:
    "\0\0\0\0\b12:29:46\0ACC\0NSE\01204.1\0-3.96\0\049.60\0\b1,253.70\0\b12:29:45\0HMT\0NSE\037.6\0-2.34\0\00.90\038.50"
    Can anyone help in reading these bytes into proper strings?

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: How to read individual bytes from a byte array

    If you have it in a byte array already, Encoding.GetString (with System.Text.ASCIIEncoding perhaps) and String.Split may be an approach. However if you are getting this from a stream, it is probably better to parse it as you read.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    Port Elizabeth, South Africa
    Posts
    37

    Re: How to read individual bytes from a byte array

    Hi,

    There are a few ways you could handle this array of bytes. I like wizard_hu_'s suggestion of converting it to a string and splitting it. I've shared some code that may help guide you to a solution.

    I take it you want to extract the strings out of the byte array. The method below may work, but it will fail if the null characters ever change to something else. At least it will show you a way of getting the strings.

    Also, there are not just null characters (0x0 or \0) in this byte array. There are 0x8 characters as shown by the "\b". You may have to replace these characters with empty strings to get rid of them. They probably mean something though

    Code:
    // Sample Byte Array: "\0\0\0\0\b12:29:46\0ACC\0NSE\01204.1\0-3.96\0\049.60\0\b1,253.70\0\b12:29:45\0HMT\0NSE\037.6\0-2.34\0\00.90\038.50"
    byte[] array =
    {
        0,0,0,8,49,50,58,50,57,58,52,54,0,65,67,67,0,78,83,69,
        0,49,50,48,52,46,49,0,45,51,46,57,54,
        0,0,52,57,46,54,48,0,8,49,44,50,53,51,46,55,48,
        0,8,49,50,58,50,57,58,52,53,0,72,77,84,0,78,83,69,
        0,51,55,46,54,0,45,50,46,51,52,
        0,0,48,46,57,48,0,51,56,46,53,48
    };
    string wholeString = UTF8Encoding.UTF8.GetString(array, 0, array.Length);
    char[] splitChar = { Convert.ToChar(0x0) };
    string[] stringArray = wholeString.Split(splitChar);
    
    string SampleOutput = stringArray[3] + " " + stringArray[4] + ", " + stringArray[5] + ", " + stringArray[6] + ", " + stringArray[7];
    Explanation:
    • Convert the byte array to a string. Your byte array already exists so ignore mine.
    • Split the string on the null character into a new array of strings.
    • Wonder what those 0x8 characters are all about. If you ran this you would see one of these characters in the sampleOutput variable.


    A proper solution will have to take into account the format of the data. Some questions to ask yourself include: Why does it begin with "\0\0\0\b"? Will it always begin with those four characters? What do the "\b" characters mean in the other parts of the byte array?

    Regards,
    Stephen

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Re: How to read individual bytes from a byte array

    Thank you Stephen and Wizard for your replies.

    Regarding the output format, I just have one documentation in Java which says that the output of my web service is in the form of DataOutputStream(ByteArrayOutputStream). Also, the doc says that the info. can be extracted as:
    Code:
    DataOutputStream dout = new DataOutputStream(new
    ByteArrayOutputStream());
    dout.writeInt( No Of Records );
    LOOP (as per no of records)
    dout.writeByte( Packet type );
    dout.writeUTF( last update time );
    dout.writeUTF( scrip );
    dout.writeUTF( exchange );
    dout.writeUTF( LTP );
    dout.writeUTF( % change );
    dout.writeByte( color );
    dout.writeUTF( price difference );
    dout.writeUTF( previous close );
    LOOP END
    The code I am trying now is:
    Code:
    Stream stream = response.GetResponseStream();
                        StreamReader reader = new StreamReader(stream);
                        strResponse = reader.ReadToEnd();
                         if (strResponse.Trim() != "")
                        {
                          byte[] byteArray;
                          byteArray = Encoding.UTF8.GetBytes(strResponse);
                          string wholeString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
                          char[] splitChar = { Convert.ToChar(0x0) };
                          string[] stringArray = wholeString.Split(splitChar);
                        }
    Last edited by pooja_1650; 2012-07-03 at 06:30.

Similar Threads

  1. read file into byte array in j2me
    By mohammad_kaied in forum Mobile Java General
    Replies: 2
    Last Post: 2014-06-18, 19:08
  2. Trying to read a file into a byte array
    By jcampom in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2010-02-04, 15:09
  3. file read into byte array
    By doskabouter in forum Symbian
    Replies: 6
    Last Post: 2009-04-09, 09:43
  4. saving unsigned bytes in byte array
    By nicenouman in forum Mobile Java General
    Replies: 3
    Last Post: 2006-10-05, 10:26
  5. read image as byte array and save that in to RMS.
    By ar_kan in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2004-02-22, 22:11

Posting Permissions

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