Send information in byte

Posted on 2014-03-25
Medium Priority
Last Modified: 2014-03-27
I am creating an android application (Java) and I have numerous information in various formats such as string, double, long, etc.

Regardless of what it is, in the end of it all I would like to send all these information in binary format over UDP.

If you can provide a detailed example of how I can accomplish this it will be much appreciated.  I just need an example on how I can first store a string, double, long, etc. in binary then send as binary over UDP.

Thank you.
Question by:Wayne88
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
LVL 36

Expert Comment

ID: 39954903
Are you particularly concerned about the exact binary representation of the data that you send? This will probably depend on the answer to the next question too; is the receiving end for this data implemented in Java too?

If both ends are Java and so you aren't particularly worried about the exact representation, just that it "works" on both ends, than I would suggest using DataOutputStream on the sending side (and DataInputStream on the receiving side).

If the above assumptions are true, let me know and I can give you some examples. If they aren't, then you will need to provide more detail about your problem, such as what is going to be on the receiving end of this data, what the exact format of the data needs to be, etc.
LVL 18

Author Comment

ID: 39955068
Hi mccarl,

Thank you for your fast response.  I am sorry that I made a mistake in the description.  I want to send the data not in binary but in byte.  The receiving end is not java, it is in C#.  

For example, I have written a small Android app that can report the device condition such as battery level in integer and the strength of the wifi signal it is connected to as char.  The format of the data really doesn't matter but these info must be send to a server.

The server will accept UDP and it has already reserved the first byte location for the battery level and the second byte location for the signal strength of the WiFi the device is connected to.

So for example, the UDP packet may look like 02F4 we will know right away the first byte (02) represent the battery level and the second byte (F4) represent the wifi signal strength.

I am unsure on how to ensure that the various information needed to be sent in whatever format they may be to be in byte by the time it's passed onto the method to send via UDP.

Thanks in advance
LVL 36

Accepted Solution

mccarl earned 800 total points
ID: 39955097
Ok, so if you requirement is to only send simple data values, those that can be represented by single byte values, as in your last comment, the code can be quite simple. Check out the below...
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

public class TestUDPData {
    public static void main(String[] args) throws IOException {
        int batteryLevel = 2;
        char wifiSignal = 'O';
        byte[] buffer = new byte[2];
        buffer[0] = (byte) batteryLevel;
        buffer[1] = (byte) wifiSignal;
        // Set the hostname and port appropriately below...
        SocketAddress address = new InetSocketAddress("dest.host.name", 9123);
        DatagramSocket socket = new DatagramSocket();
        socket.send(new DatagramPacket(buffer, buffer.length, address));

Open in new window

The problem might be if you DO still have to send more complex data types like you mentioned in the original question, ie. string, double, long, etc, as they can't be represented by single byte values.

If this is still the case, then you are probably better off thinking about 2 issues separately. First, disregard the Java side of things and work out exactly how the format of the data should look in the array of bytes that is sent in the UDP packet. And only after that is fully/properly worked out, you can think about how to code your app to convert the data that you have into the byte[] to send.
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

LVL 18

Author Comment

ID: 39956383
Thanks again for your detailed answer mccarl.  I will need to send more complex data types as the first post.  I was thinking of using byte array but I am unsure of how that works.

I was hoping that I can simply store any format value to a byte array location (which I hope byte array will automatically convert these values to bytes) then I will pass the array content to the UDP socket for sending.  Will this work?
LVL 86

Assisted Solution

CEHJ earned 400 total points
ID: 39956447
Just use a DataOutputStream opened on a ByteArrayOutputStream. Use toArray on the latter to get the result

Alternatively use ByteBuffer which will also give you useful methods to get you out of endian issues should they arise
LVL 18

Author Closing Comment

ID: 39956970
Thanks guys.  I will follow mccarl's examples for now and will experiment with byte array when I have to start sending other data formats.
LVL 36

Expert Comment

ID: 39957855
Your welcome! Glad that I could help.

As for the other data types, while I did suggest DataOutputStream in my initial post, I went away from it because you mentioned that the other end was C# and so the two languages may represent those data types in different ways.

But, while I don't do C#, there appears to be a "BinaryReader" class in C# that can be used for converting byte array (or binary) information into these other data types. I couldn't find any specific information on whether the Java DataOutputStream creates a compatible binary format for the C# BinaryReader, but you could try that. Just make sure you do some good comprehensive testing to check for compatibility issues between the two.
LVL 86

Expert Comment

ID: 39958443
so the two languages may represent those data types in different ways.
It should be largely compatible except for possible endian issues, although even there, both ends should theoretically use 'network byte order'
LVL 18

Author Comment

ID: 39959080
Thanks again guys, I will experiment with all suggested methods.

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Starting up a Project
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question