Converting IPV4 Address from bytes to String - java

I currently am trying to create a chat server as an assignment and want each message to contain a header. It will contain ipv4 address followed by a letter then a username
I can easily decode string letters from bytes but now I am struggling to decode an ipv4 address from bytes
the representation so far from the bytes is this
[-64, -88, 1, 5]
which in the ipv4 dotted quad format would be 192.168.1.5
I just need a way to try and decode the four bytes of integers to a string or something along those lines
THANKS :D

InetAddress.getByAddress(bytes).getHostAddress()?

That is easily done like this:
byte[] address = ...;
String addressStr = "";
for (int i = 0; i < 4; ++i)
{
int t = 0xFF & address[i];
addressStr += "." + t;
}
addressStr = addressStr.substring(1);

Related

Java: How to convert String of ASCII to String of characters?

I am sending a message from one device to another using MQTT client/broker. The message is exchanged (sent and received) between the two devices as String succesfully.
However, on the MQTT-Broker (i.e.: the server) the message characters are received as ASCII numbers within a string.
For example if I send:
"This is a test"
On the broker it show:
"84,104,105,115,32,105,115,32,97,32,116,101,115,116,10"
Using Java, I need a way to convert this string of ASCII back to string on the server for further process.
How to do that ? thanks
Convert the string to a byte[] and create a new string using the byte[]
String str = "84,104,105,115,32,105,115,32,97,32,116,101,115,116,10";
String[] chars = str.split(",");
byte[] bytes = new byte[chars.length];
for (int i = 0; i < chars.length; i++) {
bytes[i] = Byte.parseByte(chars[i]);
}
return new String(bytes);
You can break the string using StringTokenizer with delimiter as comma and then iterate on each of them and use Character.toString ((char) i);
You can use stream as well for this, if you can use java-8
String str = Stream.of("84,104,105,115,32,105,115,32,97,32,116,101,115,116,10".split(","))
.map(ch -> (char) Integer.valueOf(ch).intValue())
.collect(StringBuilder::new,
StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
System.out.println(str); // This is a test

Encode/decode hex to utf-8 string

Working on web application which accepts all UTF-8 character's including greek characters following are strings that i want to convert to hex.
Following are different language string which are not working in my current code
ЫЙБПАРО Εγκυκλοπαίδεια éaös Größe Größe
Following are hex conversions by javascript function mentioned below
42b41941141f41042041e 3953b33ba3c53ba3bb3bf3c03b13af3b43b53b93b1 e961f673 4772c3192c2b6c3192c217865 4772f6df65
Javascript function to convert above string to hex
function encode(string) {
var str= "";
var length = string.length;
for (var i = 0; i < length; i++){
str+= string.charCodeAt(i).toString(16);
}
return str;
}
Here it is not giving any error to convert but at java side I'm unable to parse such string used following java code to convert hex
public String HexToString(String hex){
StringBuilder finalString = new StringBuilder();
StringBuilder tempString = new StringBuilder();
for( int i=0; i<hex.length()-1; i+=2 ){
String output = hex.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
finalString.append((char)decimal);
tempString.append(decimal);
}
return finalString.toString();
}
It throws error while parsing above hex string giving parse exception.
Suggest me the solution
Javascript works with 16-bit unicode characters, therefore charCodeAt might return any number between 0 and 65535. When you encode it to hex you get strings from 1 to 4 chars, and if you simply concatenate these, there's no way for the other party to find out what characters have been encoded.
You can work around this by adding delimiters to your encoded string:
function encode(string) {
return string.split("").map(function(c) {
return c.charCodeAt(0).toString(16);
}).join('-');
}
alert(encode('größe Εγκυκλοπαίδεια 维'))

Java How to read unicode characters in socket? [duplicate]

This question already has an answer here:
DataInputStream and UTF-8
(1 answer)
Closed 9 years ago.
The server receives byte array as inputstream,and I wrapped the stream with DataInputStream.The first 2 bytes indicate the length of the byte array,and the second 2 bytes indicate a flag,and the next bytes consist of the content.My problem is the content contains unicode character which has 2 bytes.How can I read the unicode char ? My prev code is:
DataInputStream dis = new DataInputStream(is);
int length = dis.readUnsignedShort();
int flag = dis.readUnsignedShort();
String content = "";
int c;
for (int i = 0; i < length - 4; i++) {
c = dis.read();
content += (char) c;
}
It only can read ascII.thxs for your helps!
This depends on encoding scheme of your input. If you do not want to do the heavy-lifting, you could use Apache IOUtils and convert the bytes to unicode string.
Example :
IOUtils.toString(bytes, "UTF-8")

Python encoded utf-8 string \xc4\x91 in Java

How to get proper Java string from Python created string 'Oslobo\xc4\x91enja'?
How to decode it? I've tryed I think everything, looked everywhere, I've been stuck for 2 days with this problem. Please help!
Here is the Python's web service method that returns JSON from which Java client with Google Gson parses it.
def list_of_suggestions(entry):
input = entry.encode('utf-8')
"""Returns list of suggestions from auto-complete search"""
json_result = { 'suggestions': [] }
resp = urllib2.urlopen('https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' + urllib2.quote(input) + '&location=45.268605,19.852924&radius=3000&components=country:rs&sensor=false&key=blahblahblahblah')
# make json object from response
json_resp = json.loads(resp.read())
if json_resp['status'] == u'OK':
for pred in json_resp['predictions']:
if pred['description'].find('Novi Sad') != -1 or pred['description'].find(u'Нови Сад') != -1:
obj = {}
obj['name'] = pred['description'].encode('utf-8').encode('string-escape')
obj['reference'] = pred['reference'].encode('utf-8').encode('string-escape')
json_result['suggestions'].append(obj)
return str(json_result)
Here is solution on Java client
private String python2JavaStr(String pythonStr) throws UnsupportedEncodingException {
int charValue;
byte[] bytes = pythonStr.getBytes();
ByteBuffer decodedBytes = ByteBuffer.allocate(pythonStr.length());
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == '\\' && bytes[i + 1] == 'x') {
// \xc4 => c4 => 196
charValue = Integer.parseInt(pythonStr.substring(i + 2, i + 4), 16);
decodedBytes.put((byte) charValue);
i += 3;
} else
decodedBytes.put(bytes[i]);
}
return new String(decodedBytes.array(), "UTF-8");
}
You are returning the string version of the python data structure.
Return an actual JSON response instead; leave the values as Unicode:
if json_resp['status'] == u'OK':
for pred in json_resp['predictions']:
desc = pred['description']
if u'Novi Sad' in desc or u'Нови Сад' in desc:
obj = {
'name': pred['description'],
'reference': pred['reference']
}
json_result['suggestions'].append(obj)
return json.dumps(json_result)
Now Java does not have to interpret Python escape codes, and can parse valid JSON instead.
Python escapes unicode characters by converting their UTF-8 bytes into a series of \xVV values, where VV is the hex value of the byte. This is very different from the java unicode escapes, which are just a single \uVVVV per character, where VVVV is hex UTF-16 encoding.
Consider:
\xc4\x91
In decimal, those hex values are:
196 145
then (in Java):
byte[] bytes = { (byte) 196, (byte) 145 };
System.out.println("result: " + new String(bytes, "UTF-8"));
prints:
result: đ

Java IPv6 Address String to Bytes

How can I convert a String containing the ipv6's machine packet destination to a 16 byte array? I know about getBytes and encodings, but I can't seem to understand which encoding I should use or if I have to convert that String to Hexadecimal or not.
String ipv6 = "2001:0DB8:AC10:FE01:0000:0000:0000:0000";
byte[] bytes = ipv6.getBytes(); //must be a 16 byte array
An example of what I wanna do, just to exemplify.
Obs.: I have to convert the String to a 16 byte array
Thanks
try this
InetAddress a = InetAddress.getByName("2001:0DB8:AC10:FE01:0000:0000:0000:0000");
byte[] bytes = a.getAddress();
The open-source IPAddress Java library will handle a wide range of IPv6addresses, so it can be used if your string need validation or has a wide variety of formats. Disclaimer: I am the project manager of that library.
Example code:
String ipv6 = "::1";
try {
IPAddressString str = new IPAddressString("::1");
IPAddress addr = str.toAddress();
byte[] bytes = addr.getBytes();`
} catch(IPAddressStringException e) {
//e.getMessage has validation error
}

Categories