Android How to convert Hex String to int but remain 0x - android

I has a Hex String like "0xff"
I want to convert the String to int
but remain the part of "0x"
such as
String hex = "0x32";
int convert = Integer.parseInt(hex, 16);
the result of convert = 50
but what i want is directly convert the hex to int like:
convert = 0x32
How should i do?
Edit:
I have an variable hex like
String Hex = "0x32"
Now I want to parse it to int value but i want the int result is
int convert = 0x32
My question is which method can help me convert the hex String directly to int like
String hex = "0x32"
int convert = hex do some thing but result will be convert = 0x32

You seem to confuse the value of an integer with its representation as a string. For example the integer value twelve can be represented in various ways, decimal 12, octal 14, hex c, binary 1100, roman XII, are just a few of the possible representations.
An int in Java just stores the value. Representations come into play when you convert the value to a string or vice versa.

Related

Converting int 0 to binary in android studio

I am converting 4 integers into binary and everything works fine until it has to convert a zero.
for example:
int subnet1 = 255;
int subnet2 = 255;
int subnet3 = 255;
int subnet4 = 0;
binarystring = Integer.toBinaryString(subnet1)
+ Integer.toBinaryString(subnet2)
+ Integer.toBinaryString(subnet3)
+ Integer.toBinaryString(subnet4);
BinaryView.setText(binarystring);
text would look like this: 11111111 11111111 11111111 0 (without the space inbetween)
why wont it convert the 0 to 00000000 ??
Because value for "0000000" is same for "0" that's why it set as "0".
If you want to print that result. Just make a string of "0000000" for printing. Because int value always convert it.
Why? By design. From the documentation:
This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.
(Emphasis added.)
If you want 8 characters, append the result to a string of 7 zeros, and take the last 8 characters. In pseudo code:
intermediateString = "0000000" + Integer.toBinaryString( 0 ); // obviously don't hard code zero; just for example
finalString = intermediateString.substring( intermediateString.length() - 8);
If you read the documentation here: https://developer.android.com/reference/java/lang/Integer.html#toBinaryString(int)
It tells you:
The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.

change hex of integer resource to String

I am new in android.
in my value resource i create an xml layout and put this line in to it:
<integer name="mode_happy"> 0x1F60A</integer>
in my activity I want convert 0x1F60A to String. for this i create a method:
private String getStringOfEmojiCode(int emogiCode) {
StringBuilder sb = new StringBuilder();
//convert hex to char
sb.append(Character.toChars(emogiCode));
return sb.toString();
}
when I pass mode_happyto my method:
mSelectedMode =
getStringOfEmojiCode(getResources().getInteger(R.integer.mode_happy));
I receive this :
mSelectedMode: ��
but i want to get like this:0x1F60A
where is my mistake?
A formatter may be used to build the string from hex characters in a particular format:
String mSelectedMode
= String.format("0x%05X", getResources().getInteger(R.integer.mode_happy));
This worked for me, the hex which you stored as an integer was being converted to a decimal for me, but using the formatter worked. This also meant I did not have to use any other method.
Explanation:
The format 0x%05X takes in a 5 digit hex character signified by %05X with 'X' indicating hex. A lowercase x may be used which in this case would give out the output in lower case: 0x1f60a. The 0x is added to the format so as to have it as a prefix for each string. Refer to this for further details on formatting.

Android Bluetooth and sending an unsigned Hex value

I am currently working on a Android application that takes values from a text box and then sends it over bluetooth, all operations are in Hex values.
I have a convertion method that can take the string make give me the unsigned integer for the string, but once i place it in the byte array it becomes signed and the board that receives this cannot do signed hex.
This is how the process works:
//sample string to send
String toSend = "0BDD";
//sending the byte[] to the board over bluetooth
btOutputStream.write(SendByteData(toSend));
// --- perform the conversion to byte[] ---
public static byte[] SendByteData(String hexString)
{
byte[] sendingThisByteArray = new byte[hexString.length()/2];
int count = 0;
for( int i = 0; i < hexString.length() - 1; i += 2 )
{
//grab the hex in pairs
String output = hexString.substring(i, (i + 2));
//convert the 2 characters in the 'output' string to the hex number
int decimal = (int)(Integer.parseInt(output, 16)) ;
//place into array for sending
sendingThisByteArray[count] = (byte)(decimal);
Log.d(TAG, "in byte array = " + sendingThisByteArray[count]);
count ++;
}
return sendingThisByteArray;
}
The issue is as follows:
When the for for loop runs through the string and picks up "0B" it correctly gives me integer 11; then when the loop runs through "DD" it give me integer 221 which is also correct
When I perform the operation of
sendingThisByteArray[count] = (byte)(decimal);
11 gets correctly placed in sendingThisByteArray[0]
but for sendingThisByteArray[1] the number 221 gets changed to -35
I know that Java has signed bytes.. is there a way to put/place/change the byte array so i can place and number 221 or any other value higher than 127?
your help is greatly appreciated
You can convert from a signed integer to unsigned byte like this, by binary AND'ing it with 0xFF:
sendingThisByteArray[count] = (byte)(decimal & 0xFF);
This way you can send values from 0 to 255
I found the problem, when i tried to do unsigned hex into a byte array it would always put a sign, i had to create single byte to retain the unsigned hex
int zeroA = (int)(Integer.parseInt("0D", 16));
sendStream.write(unsignedToBytes((byte) zeroA));

How to roud off a String value "5571.329849243164" in android

I'm getting below error while converting String array to Long array.
java.lang.NumberFormatException: Invalid long: "5571.329849243164". How to round off this value like 5571.
Use this
String.format("%.2f", d)
//try this way, hope this will help you...
String value = "5571.329849243164";
Toast.makeText(MyActivity.this,""+Math.round(Double.parseDouble(value)),Toast.LENGTH_SHORT).show();
round(Double value)`
see here
String string = "5571.329849243164";
Double value1 = Double.parseDouble(string);
tv.setText(Math.round(value1) + "/");
I understand you want to convert a String representing a floating point value to an Integer with rounding, not truncation. Perfectly reasonable.
Two choices.
Convert the string to a Double first (Double.valueOf()), then convert to Integer (intValue()) using a rounding algorithm. Usually it's enough to just add 0.5 to the Double.
Round the String using string operations, then convert to Integer. Split the string at the first ".", convert it to Integer (getInteger()). If the digit after the "." is in the range 5-9 then add one.
I'm sure you can write the code.

String to HexString conversion

I've got a String filled with hex values like this:
received_Value = fffec780
The string isn't declared as a hex string, it is declared as a normal string and filled with this characters. But I must define it as a Hex string to make the conversion to int.
int_value_receive = Integer.parseInt(received_Value, 16)
Because when doing this i'm getting an error.
fffec780's value as an int is greater than MAX_INTEGER.
Try this
Long.parseLong(received_Value, 16);

Categories

Resources