Discussion Board

Results 1 to 4 of 4
  1. #1
    Nokia Developer Champion
    Join Date
    Feb 2009

    How to read individual bytes from a byte array


    I have a byte array whose individual bytes need to be read as they refer to some parameters. My byte array looks like:
    Can anyone help in reading these bytes into proper strings?

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Oslo, Norway

    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
    Port Elizabeth, South Africa

    Re: How to read individual bytes from a byte array


    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

    // 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 =
    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];
    • 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?


  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2009

    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:
    DataOutputStream dout = new DataOutputStream(new
    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 );
    The code I am trying now is:
    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