I want to convert String into int.
For example:
if String is "0x1F60A" then it should be convert into int same as 0x1F60A
String str = "0x1F60A";
int i = 0x1F60A; // it must be something like this.
Why I need this. Because I'm going to convert unicode int into Emoji
I'm using this code.
final String emojiString = str.substring(start,end);
// value in emojiString is 0x1F60A
int unicode = // need to convert this string into int
ss.replace(start, end, new String(Character.toChars(unicode)));
Can you please let me know how can I do that.
For conversion of Unicode to Int:
You have to omit the 2 first characters with .substring(2)
int code = Integer.parseInt(unicodeStr.substring(2), 16);
For conversion of String to int:
try {
myNum = Integer.parseInt(myString);
} catch(NumberFormatException nfe) {
}
int unicode = Integer.parseInt(emojiString.substring(2), 16);
Related
I have a string named namely "-10.00","-100.00","-1000.00". I want to get value like "10","100","1000" from that string. I have tried to get substring but did not able to get.
code i have tried
String amount = "-10.00";
String trimwalletBalance = amount.substring(0, amount.indexOf('.'));
From above i only get "-10".
String trimwalletBalance = amount.substring(1, amoun.indexOf("."));
Its very simple.
Do it like String trimwalletBalance = amount.substring(1, amount.indexOf('.'));
Instead of position 0, You should get substring from position 1
Convert it into integer and then name it positive:
String amount = "-10.00";
int amountInt = (int) Double.parseDouble(amount);
if(amountInt<0)amountInt*=-1;
Try
String amount = "-10.00";
int value = (int) Double.parseDouble(amount);
if(value < 0) value *= -1;
//value will be 10
OR
String text = amount.substring(1, amount.indexOf('.'));
String value comes like float value, then I need to convert it into integer after that I want increment integer value means string will automatically get new string from j-son. How to handle this problem ?
Try this :
String stringValue = "2.3";
int intValue = Integer.valueOf(stringValue);
intValue++;
stringValue = String.valueOf(intValue);
I don't my IDE with me so maybe you should have to add try/catch etc.
From String to float use
float f = Float.parseFloat("1235.00"); // replace with your string
From String to int use
int i = Integer.parseInt("741125"); // replace with your string
From int or float to String use
String s = String.valueOf(yourIntOrFloat);
For increment use
i++;
or
i=i+1;
I used this code for converts hex to binary but works only with 8 bits.How can i expand to 16bits?For example i want to convert FFFF to 1111111111111111 ....also i need to fill the zero 0...
void HexToBinary1(String Hex) {
int i = Integer.parseInt(Hex, 16);//16 bits
String Bin = Integer.toBinaryString(i);//Converts int to binary
String Bin2="";
if(Bin.length()==8){Bin2=Bin;}
if(Bin.length()==7){Bin2="0"+Bin;}
if(Bin.length()==6){Bin2="00"+Bin;}
if(Bin.length()==5){Bin2="000"+Bin;}
if(Bin.length()==4){Bin2="0000"+Bin;}
if(Bin.length()==3){Bin2="00000"+Bin;}
if(Bin.length()==2){Bin2="000000"+Bin;}
if(Bin.length()==1){Bin2="0000000"+Bin;}
text1.setText(Bin2);//Shows binary
}
use
String HexToBinary(String Hex) {
String bin = new BigInteger(Hex, 16).toString(2);
int inb = Integer.parseInt(bin);
bin = String.format(Locale.getDefault(),"%08d", inb);
return bin;
}
It will return binary string as 8 digit format.
you need to tell Java that the int is in hex, like this:
String HexToBinary(String Hex) {
int i = Integer.parseInt(Hex, 16);
String Bin = Integer.toBinaryString(i);
return Bin;
}
Try this
public static String getBits(double value)
{
//get bit-string of double
String printString = (Long.toBinaryString(Double.doubleToRawLongBits(value)));
//add leading zeros if bitstring is shorter than 64 bits
while (printString.length() < 64)
printString = "0" + printString;
//format string by adding byte padding
StringBuilder bitwise = new StringBuilder();
for(int i = 0; i<8; i++)
bitwise.append(printString.substring(i*8, (i+1)*8)+" ");
return bitwise.toString();
}
Feel free to further adapt for ints and add in the feature of parsing a HEX string.
I get the id of resource like so:
int test = (context.getResourceId("raw.testc3"));
I want to get it's id and put it into a string. How can I do this? .toString does not work.
String testString = Integer.toString(test);
Or you can use Use
String.valueOf()
eg.
int test=5;
String testString = String.valueOf(test);
Very fast to do it if you dnt remember or dnt have time to type long text :
int a= 100;
String s = ""+a;
What about:
int a = 100;
String s = String.format("%d", a);
I'm simply trying to convert a string that is generated from a barcode scanner to an int so that I can manipulate it by taking getting the remainder to generate a set number of integers. So far I have tried:
int myNum = 0;
try {
myNum = Integer.parseInt(myString.getText().toString());
} catch(NumberFormatException nfe) {
}
and
Integer.valueOf(mystr);
and
int value = Integer.parseInt(string);
The first one gives me the error :The method getText() is undefined for the type String
while the last two don't have any compile errors but the app crashes immediately when those are called. I thought it had to do with my barcode scanning intent method but I put it into the OnCreate and still got the error.
Change
try {
myNum = Integer.parseInt(myString.getText().toString());
} catch(NumberFormatException nfe) {
to
try {
myNum = Integer.parseInt(myString);
} catch(NumberFormatException nfe) {
It's already a string? Remove the getText() call.
int myNum = 0;
try {
myNum = Integer.parseInt(myString);
} catch(NumberFormatException nfe) {
// Handle parse error.
}
You just need to write the line of code to convert your string to int.
int convertedVal = Integer.parseInt(YOUR STR);
Use regular expression:
int i=Integer.parseInt("hello123".replaceAll("[\\D]",""));
int j=Integer.parseInt("123hello".replaceAll("[\\D]",""));
int k=Integer.parseInt("1h2el3lo".replaceAll("[\\D]",""));
output:
i=123;
j=123;
k=123;
Use regular expression:
String s="your1string2contain3with4number";
int i=Integer.parseInt(s.replaceAll("[\\D]", ""))
output: i=1234;
If you need first number combination then you should try below code:
String s="abc123xyz456";
int i=((Number)NumberFormat.getInstance().parse(s)).intValue()
output: i=123;
barcode often consist of large number so i think your app crashes because of the size of the string that you are trying to convert to int. you can use BigInteger
BigInteger reallyBig = new BigInteger(myString);
You can not convert to string if your integer value is zero or starts with zero (in which case 1st zero will be neglected).
Try change.
int NUM=null;
try this
String t1 = name.getText().toString();
Integer t2 = Integer.parseInt(mynum.getText().toString());
boolean ins = myDB.adddata(t1,t2);
public boolean adddata(String name, Integer price)
// Convert String to Integer
// String s = "fred"; // use this if you want to test the exception below
String s = "100";
try
{
// the String to int conversion happens here
int i = Integer.parseInt(s.trim());
// print out the value after the conversion
System.out.println("int i = " + i);
}
catch (NumberFormatException nfe)
{
System.out.println("NumberFormatException: " + nfe.getMessage());
}