Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Hex values to a string - Android

Tags:

java

android

I need to create a String from hex values I'm providing.

For example, instead of assigning like this:

String message = "123";

I want to assign like this:

String message = 0x31, 0x32, 0x33;

Is that possible?

like image 747
Singee Avatar asked Jan 25 '16 18:01

Singee


1 Answers

You should probably also specify the character set you are using but this will do it:

String message = new String(new byte[] {0x31, 0x32, 0x33});
System.out.println(message); // prints 123.

If you did want to specify the character set it is just another parameter to the String constructor. The following example will work if targeting Android API 19+:

String message = new String(new byte[] {0x31, 0x32, 0x33}, StandardCharsets.UTF_8);
like image 75
George Mulligan Avatar answered Sep 20 '22 07:09

George Mulligan