0

I need this line of Java code:

Integer.toString(256 + (0xFF & arrayOfByte[i]), 16).substring(1)

converted to C# since I'm not sure how to work with "0xFF".

EDIT This is the full code:

MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-256");
      localMessageDigest.update(String.format(Locale.US, "%s:%s", new Object[] { paramString1, paramString2 }).getBytes());
      byte[] arrayOfByte = localMessageDigest.digest();
      StringBuffer localStringBuffer = new StringBuffer();
      for (int i = 0; ; i++)
      {
        if (i >= arrayOfByte.length)
          return localStringBuffer.toString();
        localStringBuffer.append(Integer.toString(256 + (0xFF & arrayOfByte[i]), 16).substring(1));
      }
6
  • It should just be 0xFF as AFAIK it's too small and doesn't have MSB set so shouldn't encounter any need for unchecked. Commented Jul 14, 2013 at 1:07
  • Are you at least able to follow the code. You'd see that 0xFF is a mask for whatever is in arrayOfByte[i]. Unless what's in the array is larger than a byte I don't see the reason to use it. Commented Jul 14, 2013 at 1:10
  • I updated it...maybe it will make more sense in the context of the whole code snippet Commented Jul 14, 2013 at 1:20
  • I'm betting the 0xFF is doing some implicit casting since 0xFF is probably treated as an int. I'm almost certain you could change that 0xFF to -1 and get the same result. Commented Jul 14, 2013 at 1:24
  • @asafreedman - In the big picture, you are correct. But (0xFF & arrayOfByte[i]) and (-1 & arrayOfByte[i]) are not the same. Commented Jul 14, 2013 at 1:43

2 Answers 2

7

On that note, the actual way you can do this in C# is as follows.

String.Format("{0:x2}", arrayOfByte[i]);

Which is very similar to the Java

String.format("%02x", arrayOfByte[i]);

Which is a simpler way to do what they are doing above.

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

Comments

1

That Java expression is converting a signed byte to unsigned and then to hexadecimal with zero fill.

You should be able to code that in C#.


FWIW, the Java code gives the same answer as this:

  Integer.toString(256 + arrayOfByte[i], 16).substring(1)

or

  String.format("%02x", arrayOfByte[i])

Here's how the original works. The subexpression

  (0xFF & arrayOfByte[i])

is equivalent to

  (0xFF & ((int) arrayOfByte[i]))

which converts a signed byte (-128 to +127) to an unsigned byte (0 to +255). The purpose of the magic 256 + in the original is to ensure that the result of toString will be 3 hex digits long. Finally, the leading digit is removed, leaving you with a zero padded 2 digit hex number.

1 Comment

Also gives the same answer as this. String.format("%02x",arrayOfByte[i])

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.