on 02-22-2007 9:59 AM
Hi,
I need a function to convert unicode to Japanese characters. I have a unicode string in my syncBO and it needs to be converted to the "strange" Japanese characters.
When I read the unicode String from the MAMText files it is automatically done by the MI framework.
Unicode string example: \u6A5F\u80FD\u5834\u6240\u4E00\u89A7
Are there standard MI functions for me which are converting these kind of Strings? Because for the MAMText files it is working via the getString method of the com.sap.ip.me.api.services.MEResourceBundle class.
I hope someone knows more about this conversion.
Thanks in advance! Kind regards,
Bart Elshout
hi bart,
MI doesn't have any API to do as you described. Those mentioned by Sivakumar
are basically for encoding the URL and parameters.
we usually use the nativetoascii tool of the JDK to convert resource bundles from
unicode to ascii text. if you just want to generate the ascii representation of a
double byte characters, and want it inside your program, here's a simple code.
just customize it to your needs.
//Listing 1: Native to Ascii
//e.g. ABC機能def場所123一覧
//will result to --> ABCu6a5fu80fd defu5834u6240123u4e00u89a7
//if you want the ascii chars ABC to be encoded, remove the ==4 check
//
String n2a(String nString){
if(nString == null) return null;
StringBuffer s = new StringBuffer();
char[] c = nString.toCharArray();
for(int q=0; q<c.length; q++){
String n = Integer.toHexString(c[q]);
if(n.length() == 4)
s.append("\u").append(n);
else
s.append(c[q]);
}
return s.toString();
}
if you need to opposite i.e. ascii to native, just let me know...
regards
jo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi bart,
here you go.
-
String a2n(String uString){
if(uString == null) return null;
int iPos = 0;
int sLen = uString.length();
StringBuffer s = new StringBuffer();
while (iPos < sLen){
char c = uString.charAt(iPos++);
if (c == '\'){
c = uString.charAt(iPos);
switch(c){
case 'n': s.append('n'); break;
case 't': s.append('t'); break;
case 'r': s.append('r'); break;
case 'u': if (iPos + 4 <= sLen)
s.append((char) Integer.parseInt(uString.substring(iPos+1, iPos+=5), 16));
break;
default: s.append(c);
}
}else{
s.append(c);
}
}
return s.toString();
}
-
jo
hi bart,
it would be nice if you could feedback me with the error problems.
i know the code might not cater to all of the requirements, you might need to add
other escaped characters if you need to in the switch scope. i forgot to tell you
that to use the a2n function you should provide it with the string in its ISO format.
try running on the MyTest program below with the argument as follows:
MyTest "u6A5Fu80FD u5834 u6240u4E00u89A7"
public class MyTest {
public static void main(String[] args){
System.out.println(a2n(args[0]));
}
}
if you use it like this way, it will not work, since the String class will detect this
and automatically create the native representation of the characters. there's no
need for the a2n function.
String s = "u6A5Fu80FD u5834 u6240u4E00u89A7";
System.out.println(a2n(s));
if you are reading text from an ascii file, you need to specify that the input stream
should be ISO-8859-1 i.e.something like
new InputStreamReader(inStream, "ISO-8859-1")
hope this helps.
jo
Hi Bart Elshout
The<i><b> com.sap.ip.me.api.services.MEResourceBundle</b></i> is an API provided by MI which applications can use to display language dependent texts in the UI based on the users logon language. The language dependent texts are stored in property files with a key value pair. Given a KEY, you can get the corresponding value of the users language using the <i><b>getString()</b></i> method by passing the KEY as the parameter. To the best of my knowledge this does not take care of any any encoding or decoding of characters.
To achieve the functionality requested by you, try using the API present in <i><b>IOUtils</b></i> class under package <i><b>com.sap.ip.me.api.services.IOUtils</b></i>. In this class there is mehtod called <i><b>utf8AndUrlEncode(String)</b></i>. Corresponding there is a method to decode <i><b>utf8AndUrlDecode(bytes, int, int)</b></i> which is used for decoding. Please see the MI API doucmentation for more details https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/21eba3a7-0601-0010-b982-92f1fca3258a. To the <i><b>utf8AndUrlEncode(String)</b></i> method, try passing the input unicode string and this may return the Japanese equivalent of the same. I have personally not tried this as yet, but may work since there are no other APIs exposed in MI which take care of encoding or decoding. Do let me know your results and hope this is informative.
Best Regards
Sivakumar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
95 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.