I have a String containing ASCII representation of a character i.e.
String test = "0x07";
Is there a way I can somehow parse it to its character value.
I want something like
char c = 0x07;
But what the character exactly is, will be known only by reading the value in the string.
You have to add one step:
String test = "0x07";
int decimal = Integer.decode(test);
char c = (char) decimal;
Related
Is there a java library to convert special characters into decimal equivalent?
example:
input: "©™®"
output: "& #169; & #8482; & #174;"(space after & is only for question purpose, if typed without a space decimal equivalent is converted to special character)
Thank you !
This can be simply achieved with String.format(). The representations are simply the character value as decimal, padded to 4 characters and wrapped in &#;
The only tricky part is deciding which characters are "special". Here I've assumed not digit, not whitespace and not alpha...
StringBuilder output = new StringBuilder();
String input = "Foo bar ©™® baz";
for (char each : input.toCharArray()) {
if (Character.isAlphabetic(each) || Character.isDigit(each) || Character.isWhitespace(each)) {
output.append(each);
} else {
output.append(String.format("&#%04d;", (int) each));
}
}
System.out.println(output.toString());
You just need to fetch the integer value of the character as mentioned in How do I get the decimal value of a unicode character in Java?.
As per Oracle Java doc
char: The char data type is a single 16-bit Unicode character. It has
a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or
65,535 inclusive).
Assuming your characters fall within the character range, you can just get the decimal equivalent of each character from your string.
String text = "©™®";
char[] cArr = text.toCharArray();
for (char c : cArr)
{
int value = c; // get the decimal equivalent of the character
String result = "& #" + value; // append to some format string
System.out.println(result);
}
Output:
& #169
& #8482
& #174
I want to know that how to recognize and print next character in ASCII sequence if input is a non- string value like "space" or "!".
I know that for string value we can convert it into ASCII value by using
char character = 'a';
int ascii = (int) character;
Then adding 1 to it and converting it back to char , we can get next value in the sequence .
You can use:
char character = 'a';
int ascii = (char)((int)character+1);
It should work. But I have haven`t tested it.
I have an use case wherein I want to get the ASCII value for characters represented in String format. For example: I have string variable charString="'\0'". So I wanted the ASCII value for the character '\0' i.e 0
String charString = "'\0'";
Kindly let me know how this can be acheived in Java
Do you mean like this?
String charString = "'\0'";
int i = charString.charAt(0); // == 0
This works for all characters < 65536, not just ASCII ones.
I need to generate the hexadecimal code of Java characters into strings, and parse those strings again later. I found here that parsing can be performed as following:
char c = "\u041f".toCharArray()[0];
I was hoping for something more elegant like Integer.valueOf() for parsing.
How about generating the hexadecimal unicode properly?
This will generate a hex string representation of the char:
char ch = 'ö';
String hex = String.format("%04x", (int) ch);
And this will convert the hex string back into a char:
int hexToInt = Integer.parseInt(hex, 16);
char intToChar = (char)hexToInt;
After doing some deeper reading, the javadoc says the Character methods based on char parameters do not support all unicode values, but those taking code points (i.e., int) do.
Hence, I have been performing the following test:
int codePointCopyright = Integer.parseInt("00A9", 16);
System.out.println(Integer.toHexString(codePointCopyright));
System.out.println(Character.isValidCodePoint(codePointCopyright));
char[] toChars = Character.toChars(codePointCopyright);
System.out.println(toChars);
System.out.println();
int codePointAsian = Integer.parseInt("20011", 16);
System.out.println(Integer.toHexString(codePointAsian));
System.out.println(Character.isValidCodePoint(codePointAsian));
char[] toCharsAsian = Character.toChars(codePointAsian);
System.out.println(toCharsAsian);
and I am getting:
Therefore, I should not talk about char in my question, but rather about array of chars, since Unicode characters can be represented with more than one char. On the other side, an int covers it all.
On String level:
The following uses not char but int, say for Chinese, but is also adequate for chars.
int cp = "\u041f".codePointAt(0);
String s = new String(Character.toChars(cp));
On native2ascii level:
If you want to convert back and forth between \uXXXX and Unicode character, use from apache, commons-lang the StringEscapeUtils:
String t = StringEscapeUtils.escapeJava(s + "ö");
System.out.println(t);
On the command-line native2ascii can convert back and forth files between u-escaped and say UTF-8.
In Java, is there a simple method to convert the format of a given string? For example, I have the string "test22". I'd like the binary value and hex value. As well as possibly the ascii values of each character?
My solution would be to take the String, convert it to a char array, and then convert the integer values of the char array into binary or hex through the Integer.toBinaryString() or Integer.toHexString() or Integer.toOctalString() if you would like.
just replace binary string with hex and the function will do the same thing
public String convertToBinary(String str){
char [] array = str.toCharArray();
String binaryToBeReturned = "";
for(int i=0;i<str.length();i++){
binaryToBeReturned += Integer.toBinaryString((int)array[i]) + " ";
}
return binaryToBeReturned;
}
Also to get the ASCII values of the String int value = (int)string.charAt(i); will get the ASCII value.
I added a space just for formatting, not sure how you needed it formatted, and this is just a simple implementation.