将十六进制颜色值(#ffffff)转换为整数值 [英] Convert hex color value ( #ffffff ) to integer value

查看:858
本文介绍了将十六进制颜色值(#ffffff)转换为整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器接收到十六进制颜色值(以这种形式, #xxxxxx ,示例#000000 黑色)



如何将它转换为整数值?



我尝试过 Integer.valueOf(0x+ passedColor.substring(1,passedColor.length()))得到更多的六分法 0x000000 结果,但这不是作为 int



我收到错误: 08-03 21:06:24.673:ERROR / AndroidRuntime(20231):java.lang.NumberFormatException:无法解析'0x00C8FBFE'为整数



我使用Android SDK为他们的 setBackgroundColor / code>函数,这是你可能已经猜到的整数颜色值。



这是这个问题的OPPOSITE:如何在Android中将颜色整数转换为十六进制字符串?

解决方案

真正的答案是使用:



Color.parseColor(myPassedColor)在Android中, myPassedColor hex 值如#000 #000000 #00000000



但是,此函数不支持缩写十六进制值,例如#000


I am receiving hex color values from a server (in this form, #xxxxxx , example #000000 for black)

How do I convert this to an integer value?

I tried doing Integer.valueOf("0x" + passedColor.substring(1, passedColor.length())) to get an even more hextastic 0x000000 result, but this isn't intepreted as an int here, any other suggestions?

I receive an error: 08-03 21:06:24.673: ERROR/AndroidRuntime(20231): java.lang.NumberFormatException: unable to parse '0x00C8FBFE' as integer

I am using the Android SDK for their setBackgroundColor(int color) function, which takes - as you might have guessed - an integer color value.

This is the OPPOSITE of this question: How to convert a color integer to a hex String in Android?

解决方案

The real answer is to use:

Color.parseColor(myPassedColor) in Android, myPassedColor being the hex value like #000 or #000000 or #00000000.

However, this function does not support shorthand hex values such as #000.

这篇关于将十六进制颜色值(#ffffff)转换为整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆