2

I am porting c# code to Java and is having troubles with conversion of the following code so that the string format being written to the console is exactly the same. What is the java equivalent for the following c# code?

byte b = 0xFF; Console.Write("{0,04:X2}", b); 
2
  • Could you edit the question to include what that C# code would emit? Commented Aug 3, 2011 at 9:00
  • @Adam Paynter: It outputs [space][space]FF, where spaces are padding. Commented Aug 3, 2011 at 9:03

2 Answers 2

4

Try this:

byte b = (byte) 0xFF; System.out.printf("%4X", b); 

Output:

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

Comments

0

You need to type-cast the assignment value i.e. byte b = (byte)0xFF; looks to work. to print it into console, simply use System.out.printf("%X", b);

1 Comment

I don't think this answers his question. He is trying to format that byte.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.