80,490
社区成员
发帖
与我相关
我的任务
分享int a = Color.alpha(-16777216);
int r = Color.red(-16777216);
int g = Color.green(-16777216);
int b = Color.blue(-16777216);
byte[] colorBytes = new byte[4];
colorBytes[0] = (byte) (a & 0xff);
colorBytes[1] = (byte) (r & 0xff);
colorBytes[2] = (byte) (g & 0xff);
colorBytes[3] = (byte) (b & 0xff);
StringBuilder builder = new StringBuilder();
builder.append("#");
builder.append(bytesToHexString(colorBytes));
String colorString = builder.toString();
public static String bytesToHexString(byte[] b) {
StringBuffer str = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF).toUpperCase();
if (hex.length() == 1) {
hex = '0' + hex;
}
str.append(hex);
}
return str.toString().toUpperCase();
}