eagerToLearn
eagerToLearn

Reputation: 429

Convert String Color ("0x5b9f18") to hexcolor in Java

public class Hexcolor {

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

}

Error Message

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

Upvotes: 1

Views: 195

Answers (1)

Stefan Haustein
Stefan Haustein

Reputation: 18793

Not sure what you are asking for...

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

or

public static void main(String[] args) {
   String a="0x5b9f18";
   String hexColor = "#" + a.substring(2);
   System.out.println(hexColor);
}

Upvotes: 2

Related Questions