0

This line of code is written in java:

c = (char) ( (Integer.decode(thisElement).intValue() & 0xFF00) >> 8 );

To convert it to C#, I could find Convert.ToInt32 C# equivalent for the Integer.decode

but I do not have any idea how to convert intValue() in this code.

this is what I have done in C#:

 c = (char) ( (Convert.ToInt32(thisElement). intValue()  & 0xFF00) >> 8);

Does anyone have any idea?

Thanks in advance

6
  • Just remove .intValue() and see if that works Commented May 29, 2014 at 12:26
  • 2
    What makes you think you need an equivalent of intValue() in C#? Are you sure that Convert.ToInt32 will actually do what you want? What is your input, and what do you want your output to be? Just transliterating code from one language to another is a bad idea, generally... Commented May 29, 2014 at 12:26
  • Have you tried casting it to int? I mean Convert.ToInt32((int)thisElement) Commented May 29, 2014 at 12:27
  • I do not get any error in that case but it is really the correct way? Commented May 29, 2014 at 12:27
  • I do not get any error Are you sure? your code won't compile when you have .intValue() . Commented May 29, 2014 at 12:28

2 Answers 2

1

Try this:

  c = (Char) ((Convert.ToInt32(thisElement) & 0xFF00) >> 8);

Convert.ToInt32 returns int (as you can see from method's name) so you can immediately do bitwise operations like & and >>

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know Java well enough to know if this is right, but it compiles as C#, and does something.

c = (char)((byte)((int.Parse(thisElement) & 0xFF00) >> 8));

3 Comments

(byte) is incorrect in the code: you're narrowing the answer down to 0..255 range when char is of 0..65535 range
Is the Java char 8 bits, or 16? I don't honestly know. either way, the & 0xFF00 followed by >> 8 pretty much guarantees an 8-bit answer anyway... right?
Java char (like C# one) is 16 bit; ">> 8" is just a shift and it alone guarantees 24-bit answer (int32 = 32 bit - 8 bit for shift = 24 bit). However, the combination of "& 0xFF00" and ">> 8" yes, in fact, guarantee 8-bit answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.