We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
String to hex (Read 590 times)
String to hex
Mar 8th, 2008, 10:15pm
 
I've got an string of an color value like this "40240F". How can I turn it in real hex value to use with color()?
Re: String to hex
Reply #1 - Mar 8th, 2008, 10:46pm
 
int col=Integer.parseInt("40240F",16); should do what you want.
You may have to set the alpha part as well, but I'm not sure.
Re: String to hex
Reply #2 - Mar 9th, 2008, 12:20am
 
Mhmm I've found this solution but as you supposed the alpha value is missing. How can I add it.
Re: String to hex
Reply #3 - Mar 9th, 2008, 12:41am
 
You could simply do Integer.parseInt("FF"+"40240F",16); (the 40240F can be a String variable of course.
Or you could add the alpha in after:
Code:
int i=Integer.parseInt("40240F",16);
i&=0xFF000000;
Re: String to hex
Reply #4 - Mar 9th, 2008, 1:16am
 
http://processing.org/reference/unhex_.html
Re: String to hex
Reply #5 - Mar 9th, 2008, 7:38pm
 
Thanks John and Fry, the solution is Long.parseLong("FF"+color, 16) as in the unhex() method cause Integers are little bit to short.
Re: String to hex
Reply #6 - Mar 10th, 2008, 8:41am
 
no, that's not correct, just use unhex() with an int.

Code:
String s = "66FFCC";
// if you have six digits, you'll need to prefix with FF,
// or the two digit hex value for the alpha
color c = unhex("FF" + s);

// prints FF66FFCC
println(hex(c));
Re: String to hex
Reply #7 - Mar 10th, 2008, 11:42am
 
Sure, but if  I haven't the PApplet object ...
Re: String to hex
Reply #8 - Mar 10th, 2008, 6:58pm
 
eskimoblood wrote on Mar 10th, 2008, 11:42am:
Sure, but if  I haven't the PApplet object ...

then you use PApplet.unhex(). but now you're changing the question--how are you gonna use it with color() if you aren't working with a PApplet
Re: String to hex
Reply #9 - Mar 11th, 2008, 9:17am
 
Its a class which only converts the strings from a external source, but sure I can import processing.core to have PApplet there. I'm not  a programmer, so sometimes I'm not sure what's the right way to do something.
Page Index Toggle Pages: 1