Link to home
Start Free TrialLog in
Avatar of kasiencja
kasiencjaFlag for Canada

asked on

Char handling wih ^

I have something in php which I am trying to get to work in java, but unfortunately I’m failing miserably.

PHP:
charX = “I”  //Capital I
charY = “Í”  // Capital I, Acute Accent
result = (charX ^ charY);

JAVA:
char charX = “I”  //Capital I
char charY = “Í”  // Capital I, Acute Accent
result = (charX ^ charY);

In PHP I get the desired result of “,,” however that is not the case in Java, below I’ve listed a couple more examples:

[U] [Í] = [˜]  // [capital U] [Capital I, Acute Accent] = [Tilde]
[ ] [Û] = [û]  // [space] [Capital U, Circumflex Accent] = [Small u, Circumflex Accent]
[I] [Å] = [Œ]  // [capital I] [Capital A, Ring] =
[N] [Û] = [•]  // [capital N] [Capital U, Circumflex Accent] =

I'm sure tht the Java part is incorect since I'm getting different results that in PHP.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>In PHP I get the desired result of “,,”

Don't quite understand - i see two commas. I may be being thick, but how do you get two commas from letter Is?
Avatar of aozarov
aozarov

replace
char charX = “I”  //Capital I
char charY = “Í”  // Capital I, Acute Accent
result = (charX ^ charY);

with
char charX = 'I' //Capital I
char charY = 'Í'  // Capital I, Acute Accent
result = (charX ^ charY);



Avatar of kasiencja

ASKER

Its a bitwise XOR with left associativity.  I think that the problem lies in PHP and Java handling the variables as different types. I'm new to Java, and I just came across an explanation of "^" which states that this only works for integers.  So, since its a bitwise comparison maybe I should first convert the characters to binary get the result and then convert the result to character. Hmm... this maybe crazy but will see...
You can bitwise XOR it:

char xored = (char)'I' ^ 'Í';  // Capital I, A
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> result = (charX ^ charY);  
If result is of type int (can't tell from your code) then there is no need to cast to char.
:-)
I've also found out that Eclipse's console does not support certain characters, "„" (html "„") being one of them, so when you have "I" and "Í" it just spits out "?", one of the cofigurations I tried that worked was  [!] [Å] = [ä].

Even when I try to cut and paste "Œ", "ƒ" or "Ÿ" as a character in the code, it is replaced with . (a dot).  That kind of sucks!
132 is not defined in iso8859-1

The other characters you mention print fine for me in Eclipse, both in the editor pane and in the console