0

I want to allow a user specify a Unicode range via an XML config file. E.g. they could state 0100..017F as the range. For my (Java) app to consume this char range, I need to convert the XML input (String) to type char. Any ideas?#

E.g.

String input = "0100..017F"; // I can change format of input, if enables a solution
char from = '\u0100';
char to = '\u017f';

Thanks.

2
  • So you mean from string to char, right? Commented Feb 16, 2014 at 16:22
  • input.charAt(0); doesnt help you? not should i get the question, give us a more fixed example of an input and expected output Commented Feb 16, 2014 at 16:22

1 Answer 1

4

If it always matches exactly that format, then this will suffice:

char from = (char)Integer.parseInt(input.substring(0, 4), 16);
char to = (char)Integer.parseInt(input.substring(6), 16);

For something more flexible:

char from;
char to;
java.util.regex.Matcher m = java.util.regex.Pattern.compile(
    "^([\\da-fA-F]{1,4})(?:\\s*\\.\\.\\s*([\\da-fA-F]{1,4}))?$").matcher(input);
if (!m.find()) throw new IllegalArgumentException();
from = (char)Integer.parseInt(m.group(1), 16);
if (m.group(2) != null) {
    to = (char)Integer.parseInt(m.group(2), 16);
} else {
    to = from;
}

That allows for 1 to 4 hex digits for each character, the .. may have space around it, and the to part of the range may be omitted and assumed to be equal to from.

Sign up to request clarification or add additional context in comments.

5 Comments

Looks good thanks. In addition to your sub-strings, I will need more string manipulation code, as a (Unicode) input could be 4 or 5 chars in length.
@Damo It won't fit in a char then, since char is limited to \uFFFF.. you would have to make it an int...
Just testing out above, and not sure if its correct approach. Just to go over rqmts, I want to convert a string (that represents a char) to said char. Whereas (char)Integer.parseInt("0100") will resolve to the char 'd' - i want it to resolve to '\u0100'.
@Damo Integer.parseInt("0100", 16);
+! for fancy flexible regular expressions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.