简体   繁体   中英

How can I convert String to Int? will this value hex or dex?

in java i have

color_value="FFAAFF";

I have tried:

int color = Integer.parseInt(color_value);`

How can I convert it to int(hex) to add to im_view.setBackgroundColor(int color); ?

As we have to guess the lang you're using, i'll assume it's java, what you actually need is to convert from HEX to RGB, if you are comfortable with AWT then you could use a built in func:

String color_value = "FFAAFF";
im_view.setBackgroundColor(Color.decode(color_value));

If don't want to use AWT, then you could do it like this :

/**
 * 
 * @param colorStr e.g. "#FFFFFF"
 * @return 
 */
public static Color hex2Rgb(String colorStr) {
    return new Color(
            Integer.valueOf( colorStr.substring( 1, 3 ), 16 ),
            Integer.valueOf( colorStr.substring( 3, 5 ), 16 ),
            Integer.valueOf( colorStr.substring( 5, 7 ), 16 ) );
}



im_view.setBackgroundColor(hex2Rgb(color_value));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM