1

公共类 Hexcolor {

public static void main(String[] args) {
    String a="0x5b9f18";
    String hexColor = String.format("#%06X", a);
    System.out.println(hexColor);
}

}

错误信息

线程“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 Hexcolor .main(Hexcolor.java:6)

4

1 回答 1

2

不知道你在问什么...

public static void main(String[] args) {
  int a = 0x5b9f18;
  String hexColor = String.format("#%06X", a);
  System.out.println(hexColor);
}

或者

public static void main(String[] args) {
   String a="0x5b9f18";
   String hexColor = "#" + a.substring(2);
   System.out.println(hexColor);
}
于 2012-05-24T00:23:32.690 回答