Posted By:
Rob_Eamon
Posted On:
Wednesday, August 28, 2002 08:25 PM
Here's a function that will convert unicode literals, and escaped characters, to the char equivalents.
/**
* Converts Java and Unicode character escape literals to
* their character equivalent.
*
* Various services may require the use of special
* characters that cannot normally be typed at the
* keyboard, such as a newline or carriage return. These
* characters can be supported through the use of escape
* sequences. These escape sequences are interpreted and
* converted to their character equivalent.
*
* The following escaped characters can be specified as
* the input variable and will be converted as described:
*
* - Backspace
* - Horizontal tab
*
- Newline
* f - Form feed
*
- Carriage return
* uXXXX - The Unicode character with encoding XXXX,
* where XXXX is four hexadecimal digits. For example,
* u000a represents a newline character.
*
* If the input variable unitCode contains any other
* values, the first character is returned. If unitCode is
* null or empty, the returned character is 0 (nul - not
* the character '0').
*/
public static char toChar(String unitCode)
{
char theChar = 0;
if(unitCode != null)
{
unitCode = unitCode.trim();
if(unitCode.length() > 0)
{
theChar = unitCode.charAt(0); // default to return 1st character
if(unitCode.charAt(0) == '\')
{
if(unitCode.length() == 1)
{
theChar = '\';
}
else if(unitCode.length() == 2)
{
switch(unitCode.charAt(1))
{
case 'r':
theChar = '
';
break;
case 'n':
theChar = '
';
break;
case 'b':
theChar = '';
break;
case 't':
theChar = ' ';
break;
case 'f':
theChar = 'f';
break;
}
}
else if((unitCode.charAt(1) == 'u') && (unitCode.length() >= 6))
{
theChar = (char) Integer.parseInt(unitCode.substring(2, 6), 16);
}
}
}
return theChar;
}
HTH