简体   繁体   中英

Retrieve String from Binary Data

i am working with UDP Multicast in Java.My code is as follows:

import java.io.*;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.lang.*;
import java.nio.charset.Charset;

public class MulticastTest {

    static String MCAST_ADDR = "224.0.1.129";
    static int DEST_PORT = 320;
    static int BUFFER_LENGTH = 4096;

    public static void main(String args[]) {

        try {
            byte[] b = new byte[BUFFER_LENGTH];
            DatagramPacket dgram = new DatagramPacket(b, b.length);
            MulticastSocket socket = new MulticastSocket(DEST_PORT); 
            socket.joinGroup(InetAddress.getByName(MCAST_ADDR));
            socket.receive(dgram); // blocks until a datagram is received
            System.err.println("Received " + dgram.getLength() + " bytes from " + dgram.getAddress());
            dgram.setLength(b.length); // must reset length field!

    String received = new String(dgram.getData());
    System.out.println("Bytes received and printed at Console: " + received);

            } catch (Exception e) {
        }
    }
}

I am able to receive the data from another node but only as binary data. Could someone please tell me how to convert the variable "received" into something meaningful such as Strings/Character so that it is readable to humans.And could you please write the code exactly, i am really new to programming.I would really appreciate any help.

 String received = new String(dgram.getData());

This is where you get the received data from the other node, and it is already in String format. The problem you're having is that you're converting from byte to String and since your buffer is 4096 in size, your String variable has blank characters in it. This will appear just after the actual data in your variable.

What you could do to make it more "readable to humans", is to pass it through some string manipulation function to remove the blank characters. The idea is to go through the String you received, and retrieve all alphanumeric characters present there.

Hope this helps.

You could use this to extract your data

String received = new String(dgram.getData());  
String dataReceived = "";
for(int i = 0;i<received.lenght();i++){

char charac = received.charAt(i);//this will allow you to get all data in String
switch(charac){

    case 'a': dataReceived = dataReceived.concat(String.valueOf(charac));
    case 'b': dataReceived = dataReceived.concat(String.valueOf(charac));
    case 'c': dataReceived = dataReceived.concat(String.valueOf(charac));
    case 'd': dataReceived = dataReceived.concat(String.valueOf(charac));
    ...
    case '1': dataReceived = dataReceived.concat(String.valueOf(charac));
    case '2': dataReceived = dataReceived.concat(String.valueOf(charac));
    ...
    case '.': dataReceived = dataReceived.concat(String.valueOf(charac));
    case ',': dataReceived = dataReceived.concat(String.valueOf(charac));
    ..          
   }
 } 
System.out.println("Data Received: "+dataReceived);

Note that the code is incomplete, if your string will contain only letters no need to check for numbers and symbols.

GoodLuck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM