This discussion is archived
11 Replies Latest reply: Oct 15, 2012 6:04 PM by rp0428 RSS

Create and read byte data

968333 Newbie
Currently Being Moderated
How to create a byte data, I dont know how to call it, i will clear it below,

-------------------------------------
Header | Message |
-------------------------------------
Username | 01101001 |
-------------------------------------

How to create like this, sorry if my question is not clear, i will clarify the doubt if you have.
  • 1. Re: Create and read data
    Jörg Explorer
    Currently Being Moderated
    i will clarify the doubt if you have.
    Please do so.

    This is how I create byte data
    byte b= 1;
    byte[] bArray= new byte[]{2,3,4,5};
    But I am afraid that's not what you want to know.
    Maybe the method String.getBytes() is what you are looking for.
  • 2. Re: Create and read data
    gimbal2 Guru
    Currently Being Moderated
    965330 wrote:
    How to create a byte data, I dont know how to call it, i will clear it below,

    -------------------------------------
    Header | Message |
    -------------------------------------
    Username | 01101001 |
    -------------------------------------

    How to create like this, sorry if my question is not clear, i will clarify the doubt if you have.
    Guess: are you perhaps asking about BIT in stead of BYTE data? The username value looks suspiciously like a 1-byte value displayed in bits.
  • 3. Re: Create and read data
    968333 Newbie
    Currently Being Moderated
    Actually i am working in a sensor project, the sensor reads the heart beat and body temperature and then it sends through Bluetooth, so first i have to read it and later i have to convert it into string. Hope it clears now.
  • 4. Re: Create and read data
    gimbal2 Guru
    Currently Being Moderated
    So you get a byte[] array and you want to turn that into a String?

    new String(bytes) could already do it. The javadoc is your friend:

    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

    When transforming bytes into characters you must be aware of encoding though. If you don't know much about that it is worth to research that subject.

    http://www.joelonsoftware.com/articles/Unicode.html
  • 5. Re: Create and read data
    rp0428 Guru
    Currently Being Moderated
    >
    Actually i am working in a sensor project, the sensor reads the heart beat and body temperature and then it sends through Bluetooth, so first i have to read it and later i have to convert it into string. Hope it clears now.
    >
    No - it isn't clear now.

    You need to explain how the sensor hardware is providing the data. Is it as a stream of bytes? What is the interface between the pc and the sensor? How does the Java app know that there is sensor data available? How does it query the sensor to determine the status.

    If the sensor provides a pulse value of '60' how does the sensor encode that value? Does it use binary? BCD? floating point?

    A binary value of '60' would be hexadecimal 0x3c and in binary would be 00111100. Why would you want to display 00111100? No one would understand what that value means. Shouldn't you display '60'?

    Provide sample sensor data, the format the sensor uses and what you are really trying to do with the data.
  • 6. Re: Create and read data
    968333 Newbie
    Currently Being Moderated
    Thanks for your reply, your link will helpful, first i will go through that one and return...
  • 7. Re: Create and read data
    968333 Newbie
    Currently Being Moderated
    You are exactly correct, my task is to i have to create a data packet manually in java, as i described in my first post like header, access code and payload. I have to create data like that and have to change it to the string.
  • 8. Re: Create and read data
    rp0428 Guru
    Currently Being Moderated
    >
    You are exactly correct, my task is to i have to create a data packet manually in java, as i described in my first post like header, access code and payload. I have to create data like that and have to change it to the string.
    >
    I can't be 'correct' because that is is not what I said or ask.

    It is the sensor and sensor package that generally creates a header, access code and payload. Then Java code has to interact with the sensor to read the data stream and parse it into its pieces. Code might create a polling loop that polls the sensors on a regular basis and downloads any data accumulated.

    You haven't told us how the code is supposed to get the data from the sensor. How does the Java app know that there is sensor data to get. What is the sensor api?

    For example, here is how you might read and process sensor data for android
    http://www.vogella.com/articles/AndroidSensor/article.html
  • 9. Re: Create and read data
    968333 Newbie
    Currently Being Moderated
    That details i didnt get even from my team, now they told me to create a packet and study about that format. Sorry...
  • 10. Re: Create and read data
    968333 Newbie
    Currently Being Moderated
    Is it possible to create data packet in java by own?
  • 11. Re: Create and read data
    rp0428 Guru
    Currently Being Moderated
    >
    That details i didnt get even from my team, now they told me to create a packet and study about that format. Sorry...
    >
    Well - you can wait and see how others respond but I can't help you without knowing how, and in what format, the Java code is going to get the data from the sensor.

    Displaying analog data like temperature or pulse rate as a string of binary bits doesn't make any sense to me.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points