Solved

# Char handling wih ^

Posted on 2005-04-21
221 Views
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.
0
Question by:kasiencja

LVL 86

Expert Comment

>>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?
0

LVL 15

Expert Comment

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);

0

Author Comment

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...
0

LVL 86

Expert Comment

You can bitwise XOR it:

char xored = (char)'I' ^ 'Í';  // Capital I, A
0

LVL 86

Accepted Solution

That should have been

char xored = (char)('I' ^ 'Í');
0

LVL 15

Expert Comment

>> result = (charX ^ charY);
If result is of type int (can't tell from your code) then there is no need to cast to char.
0

LVL 86

Expert Comment

:-)
0

Author Comment

I've also found out that Eclipse's console does not support certain characters, "„" (html "&#132;") 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!
0

LVL 86

Expert Comment

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
0

## Featured Post

### Suggested Solutions

java continue statement 10 50
sumDigits  challenge 7 38
powerN  challenge 3 34
word0 challenge 4 37
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…