0

I was trying to convert the below C code to Java. I am getting the below exception while doing so. What am I doing wrong here? Please advice.

Exception in thread "main" java.util.IllegalFormatConversionException: x != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at com.kube.rfidscannertest.MainScannerTest.printEpc(MainScannerTest.java:68)
at com.kube.rfidscannertest.MainScannerTest.main(MainScannerTest.java:42)

Below is the C code.

printf("%04X", (((epc[i] & 0xFF00) >> 8) | ((epc[i] & 0xFF) << 8)));

Below is how I am trying to covert it into Java.

String.format("%04X ", Integer.toHexString((((data[i] & 0xFF00) >> 8) | ((data[i] & 0xFF) << 8))).replace(' ', '0')).concat(strData);

Please note that epc and data are integer arrays.

AnOldSoul
  • 4,017
  • 12
  • 57
  • 118

1 Answers1

0

The formatter manual states that the %x format delegate "Formats the argument as an integer in base sixteen."

In other words, String.format expects %04X to correspond to an integer argument. Your argument, however, is a string... and that discrepancy is what your exception is trying to tell you.

Perhaps you meant:

String.format("%04X", (((data[i] & 0xFF00) >> 8) | ((data[i] & 0xFF) << 8)))

This still behaves obviously differently, mind you, because the C version prints the output straight to stdout, whereas the Java version doesn't.

I suspect you meant to use Java.io.Console.format, instead of String.format, for example:

import java.io.Console;
System.console().format("%04X", (((data[i] & 0xFF00) >> 8) | ((data[i] & 0xFF) << 8)));
autistic
  • 1
  • 3
  • 35
  • 80
  • format is not a static method in Console. Therefore I created it like, Console c =null; c=System.console(); But the object c remains null and therefore I am getting a null pointer exception. How do I call that method? – AnOldSoul Nov 19 '15 at 06:30
  • I dont think console is going to work for me as it seem to require a console when JVM is started. Are there any other alternatives? – AnOldSoul Nov 19 '15 at 06:32
  • @mayooran As you can probably tell, Java is not my forte; C is. Having said that, I have modified the answer to correct that error. As for the error that `System.console()` is `null`, well, that isn't a part of the question you asked but if you check out [this question](http://stackoverflow.com/questions/4203646/system-console-returns-null) you might find an answer... – autistic Nov 19 '15 at 07:35
  • @mayooran It seems to imply that `System.console()` is `null` because you're launching it as a Windows program (not a console program) in Eclipse. The same problem would exist in C if you compiled as a Windows program and attempted to use `printf`... except it wouldn't be as an exception; `printf` would just return an error code. – autistic Nov 19 '15 at 07:37