在Java中将字符串颜色(“0x5b9f18”)转换为hexcolor

时间:2012-05-24 00:17:40

标签: java

公共类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)     在java.lang.String.format(未知来源)     在Hexcolor.main(Hexcolor.java:6)

1 个答案:

答案 0 :(得分: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);
}