cancel
Showing results for 
Search instead for 
Did you mean: 

Unicode to Japanese characters

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Jo,

I need the other solution. I have a string like this:

- ABC\u6a5f\u80fd def\u5834\u6240123\u4e00\u89a7

and I want to convert it to:

- ABC&#27231;&#33021;def&#22580;&#25152;123&#19968;&#35239;

It would be great if you can help me with this.

Thanks in advance and very kind regards!!

Bart

Former Member
0 Kudos

hi bart,

which one do you need,

1) ascii -> native conversion

2) native -> ascii conversion into a &#xxxxx; format

jo

Former Member
0 Kudos

Hi Jo,

I need the 1) ascii -> native conversion

because I have (ascii): \u6240\u4E00\u89A7 and I want to convert it to the Japanese characters (native) and not the ascii

It would be great if you know something for this, thanks in advance,

Bart

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Jo,

Thank you very much, this is working perfectly!

I hope I have you rewarded with the full ten points because there went something wrong with it.... I got errors so I hope you have all points.

Very kind regards,

Bart Elshout

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Jo,

I am sorry my statement was not clear.... but the code is working perfectly. The only thing what went wrong was rewarding you with the full 10 points for your help....

So no error everything is working as I wished/hoped.

Thanks again and kind regards,

Bart

Former Member
0 Kudos

hi bart,

great and thanks. take note those pointers i described in my last post.

just curious if you're doing a project for a japanese client?

regards

jo

Former Member
0 Kudos

Hi Jo,

We are doing a project at a global company and Japan is one of the countries we will support.

Kind regards,

Bart

Answers (1)

Answers (1)

Former Member
0 Kudos

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