2012-05-24 27 views
1

公共类Hexcolor {转换串色( “0x5b9f18”)在Java hexcolor

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

}

错误消息

异常在线程 “主” 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 $ FormatSpec ifier.print(未知源) at java.util.Formatter.format(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.lang.String.format(Unknown Source) at Hexcolor。主(Hexcolor.java:6)

回答

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); 
} 
+0

非常感谢你。第二个建议对我有用。 – eagerToLearn