-5

I read this question and its answers here: Java: Convert String “\uFFFF” into char. This question, although very close to what I require, doesn't answer my requirement.

What if the string is formed by String ori = new String("\"" + "\u" + n + "\""); where n is a string formed by Integer.toString(i) where i is an int? How can this string variable ori be converted into a Unicode Character?

The chain from i to n is as follows: String n = Integer.toString(i); n = "0000".substring(n.length()) + n;

In that case, how are those replies modulated? For instance, char c = "\uFFFF".toCharArray()[0];?

I checked. The above code can't be replaced by: char c = ori.toCharArray()[0]; Since the coversion is within the program, hard-coding the value like shown: char c = '\uFFFF'; is not possible for my codes.

One could have a look at Character and Byte Streams and the example program StreamConverter.java. Replace the stream "\u65e5\u672c\u8a9e\u6587\u5b57\u5217" with my entry, for instance, "\u005a" and one would have the Character Z in both the console and the GUI.

Also, look at the program and stringConverter.java, where the each of the 6-element integer strings is replaced by a UTF character.

But why that code works, but not mine?!

Rajibando
  • 1
  • 4
  • 1
    Simply post a new question referencing the old one and explain why the answers to the existing one don't solve your problem. – dpr Sep 21 '17 at 07:43
  • *Why* is the `String ori` formed like that? What is your requirement? `\unnnn` is a notation for the compiler, not for runtime. At runtime you will have actual character codes, not hex digits. – user207421 Sep 21 '17 at 07:47
  • Because using a random code, every time, the variable i creates a new n and therefore a new string of a unicode value. So I need to convert the string into a Unicode character. I can form two forms like these: "\u0056" and "0x0056", but no further! – Rajibando Sep 21 '17 at 07:57
  • You have `n` so just cast it to a character. eg. If `int n = 47` then `char c = (char)n` is the same as `char c = '\u002F';` – matt Sep 21 '17 at 08:02
  • matt, I have n as string, like this: `String n = Integer.toString(i);` and then, `n = "0000".substring(n.length()) + n;`. So please advise. I also perused this post: How to convert/parse from String to char in java? at https://stackoverflow.com/questions/7853502/how-to-convert-parse-from-string-to-char-in-java?rq=1. The solution doesn't appear to work. – Rajibando Sep 21 '17 at 08:14
  • This is not clear in the slightest. Just cast `i`, why on earth do you change to a `String n`. Do you want to convert `i` to a substring? Note that the unicode numbers are in hex. eg. `String n = Integer.toString(i, 16);` Or even better since you want to pad with zeros. `String n = String.format("%04x", i);` – matt Sep 21 '17 at 08:43
  • Matt, I need unicode chars from A to Z in the end, randomly. So int 'i' creates a new string 'n' in the form of a unicode char, e.g., "\u0056" and "0x0056". – Rajibando Sep 21 '17 at 08:51
  • You want a char from A to Z? Just cast the integer value. `char c = (char)('A' + i); Here is a working example. https://ideone.com/tw8YX8 – matt Sep 21 '17 at 08:58
  • No, that is just to simplify the situation. For details, please peruse the Oracle code examples I have provided in my edited post above. – Rajibando Sep 21 '17 at 12:43

1 Answers1

0

You cannot form String ori like that for two reasons. First, unicode escape sequences are in hexidecimal, and when you add an int it is base 10.

String ori = String.format("\\u%04x", i);

That will give you an String with the hex/unicode representation. eg if i is 47 then ori = "\u002f". Now if you want to get the character that represents. You have to parse the integer value from it. That value will be equal to i though so just use i.

char c = (char)i;

So now we have all of the tools.

for(int i = 'A'; i<='Z'; i++){
     System.out.println(String.format("\\u%04x is the character: %s", i, (char)i)); 
}

That will print out each letter, for example.

\u0051 is the character: Q

Consider the representations.

String one = "\u0041";

That String is a string with one character, 'A'. Then consider this string.

String two = "\\u0041";

That is a string with 6 characters. If you have a string like this you can grab the number out of the string and make a character.

char c = (char)Integer.parseInt(two.substring(2), 16);

For example.

    String two = "\\u00B6";
    char c = (char)(Integer.parseInt(two.substring(2), 16));
    System.out.println(two + " is the code for " + c);
    #\u00B6 is the code for ¶
matt
  • 10,892
  • 3
  • 22
  • 34
  • Matt, A is UTF-8: 0x41 or UTF-16: 0x0041 and Z is UTF-8: 0x5A or UTF-16: 0x005A. So I would create a string like the unicode format, "0x005A" or "0x5A" . But my wit stops at converting the string form to be read as a single character. You could check the example programs in oracle. You could replace, as I did, the strings with only one string, "\u005A", and get the desired output, both in GUI and console. – Rajibando Sep 21 '17 at 09:19
  • Thank you, Matt! But Matt, if you look at the Oracle java code stream converter, there is a definite line which I have replaced into: String str = new String("\u005A"); for instance, and I get the required output.¶ I don't need to look at the Unicode values programmatically. I use Linux and have kcharselect to find the values.¶ I need the reverse program, to read hex code format into Unicode alphabets.¶ ¶ is for showing paragraph / line break – Rajibando Sep 21 '17 at 09:38
  • What is your input, and what do you want for output. You never need to write `String str = new String("\u005A");` Do you have a unicode string and you want to turn it into a character? ie `String str = "\\u005A";` or do you have a unicode character. `String str = "\u005A".` – matt Sep 21 '17 at 09:42
  • Input is nothing. It is handled by random generation of i from 2 to 26. The output I want is unicode A to Z, that too, randomly, based on input. →→→ Please check this part again, Matt! if you look at the Oracle java code stream converter, there is a definite line which I have replaced into: String str = new String("\u005A"); for instance, and I get the required output.¶ →→→→ Oh, no, we were writing and posting together! Okay, I shall remain silent for some time! – Rajibando Sep 21 '17 at 09:53
  • By the way. "\u005A" is "Z". You can check [here](https://unicode-table.com/en/#basic-latin) for another reference. – matt Sep 21 '17 at 09:56
  • I lnow that 0x41 and 0x0041 are the exact same number.
    What I am planning is the reverse of what you are writing.
    Very well, can you design a code-line that would create a Unicode character from the simple strings like "\u005A"? I am also aware that "\u005A" is Z.. If you want, I could upload the Oracle cod in this thread. I don't know whether that will violate Oracle copyright, though.
    – Rajibando Sep 21 '17 at 10:09
  • Are you joking? I have done both. I have changed the String "\u005A" into a character, and I have changed the character into the String. How can it be the reverse? Why would you paste oracle code. What does the oracle code have to do with anything? – matt Sep 21 '17 at 10:15
  • BTW, Matt, your code line, `char n = (char)('A' + c);` added to my program, gives me the intended result. But remember to work on the part of the code I have given to use straight Unicode conversion. The link of the StringConverter.java is here: https://docs.oracle.com/javase/tutorial/i18n/text/examples/StreamConverter.java Just remember to replace the Japanese Unicode chars by "\u005A" for instance, and you will get both Z in the console, and the GUI. – Rajibando Sep 21 '17 at 10:23
  • [quote Matt]Why would you paste oracle code[/quote] Sorry, I did not refresh when I posted my comment. I did not read yours just before mine.
    And the oracle code is important, because there you write the code in digits in unicode representation and get a different single unicode character.
    – Rajibando Sep 21 '17 at 10:40
  • [Quote Matt]...First, unicode escape sequences are in hexidecimal, and when you add an int it is base 10....[/quote] Matt, I am not adding int, I am using int to form string. My above comment with "\u005A" will illustrate. Please also refer to the Oracle example, StringConverter.java. The same principles are used there too. – Rajibando Sep 21 '17 at 10:55
  • "I am not adding int", but in your example you wrote, `String ori = new String("\"" + "\u" + n + "\"");` where 'n' is a string formed by Integer.toString(i). Which is base 10. – matt Sep 21 '17 at 12:04