1

I am trying to convert string to hebrew encoding (windows 1255) so I need to substract from the value of any char 1264 and put here in new string.

this is the code in javascript that I am trying to convert:

strText = strText.replace(/[א-ת]/ig, function(a,b,c) {
        return escape(String.fromCharCode(a.charCodeAt(0)-1264));
    });

And this is what I made in Java but I am not getting the expected value:

String test = "שלום";
byte[] testBytes = test.getBytes();
String testResult = "";
for (int i = 0;i < testBytes.length;i++)
     {
        testResult += (char)((int)testBytes[i]-1264);
     }

What am I doing wrong?

Haim127
  • 585
  • 2
  • 8
  • 29

2 Answers2

0

As you are using a byte array, the maximum number that can be stored is 255, and the minimum is 0, so it can only store extended ASCII characters (afaik it doesn't cover hebrew characters). What you need is a char array (can store any unicode character).

So, change this

byte[] testBytes = test.getBytes();

to this

char[] testBytes = test.toCharArray();
SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
  • Thanks its helped, now how can I convert it to url style encoding.I need to convert only 4 chars, and not full url. – Haim127 Jan 11 '15 at 17:11
0

You need to pass the encoding when you call String.getBytes(String). Something like

public static void main(String[] args) {
    String test = "שלום";
    try {
        byte[] testBytes = test.getBytes("UTF-8");
        String testResult = new String(testBytes, "UTF-8");
        System.out.println(testResult);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Output is

שלום
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • But I want to convert it to windows-1255.the substraction change the text to the encoding. – Haim127 Jan 11 '15 at 16:51
  • No. You would never subtract like that to change the encoding. You can pass a different encoding to `new String` or `getBytes()`. – Elliott Frisch Jan 11 '15 at 16:52
  • What can I do?this is the only way I found to change it.do you know other way to convert utf-8 to windows-1255 that support android?Thanks. – Haim127 Jan 11 '15 at 16:55