Solved

Send information in byte

Posted on 2014-03-25
10
493 Views
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.
0
Comment
Question by:Wayne88
  • 4
  • 3
  • 2
10 Comments
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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.
0
 
LVL 13

Author Comment

by:Wayne88
Comment Utility
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
0
 
LVL 35

Accepted Solution

by:
mccarl earned 200 total points
Comment Utility
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));
        socket.close();
    }
}

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.
0
 
LVL 13

Author Comment

by:Wayne88
Comment Utility
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?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
Comment Utility
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
0
 
LVL 13

Author Closing Comment

by:Wayne88
Comment Utility
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.
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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'
0
 
LVL 13

Author Comment

by:Wayne88
Comment Utility
Thanks again guys, I will experiment with all suggested methods.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now