0
is this by luck ?
hello, i solved the into hexa challenge and it worked. but is this code really valid or it's just by luck ? and is there a better way ? *Edit : Okay i tried to just take integer values and print it in hexa values and it worked -_- This is awkward 😐. But if there was not a "%x" , how could it be ? https://code.sololearn.com/cJuVNoc7ZBH9/?ref=app
5 Antworten
+ 2
Hexadecimal number system (hex), easy way..
What about HSL().. The key advantage of HSL(Hue, Saturate, Lightness) is the ability to specify color characteristics independently of each other. It's will next level for you.
+ 1
Smith Welder
That's a higher level 😃
I am still behind that .
But what i am talking about is actually how to convert rgb intger values to hexa one and print it . ( I am still that level 🥲😅)
+ 1
int values are internally stored as binary, not decimal, so in a sense the question itself is incoherent; you'd be doing a lot of work just to convert a binary number to the same binary number. %x and %d are BOTH just outputting options, so %x is the most correct solution.
That said, if some niche situation arose where you did need or want to do these operations manually, you'd do it with the modulus operator %, eg
take int x
a = malloc an array to store the digits (since, again, storing the result as an int will just convert the whole thing back to binary); this will be an array of char if you want a string output
while loop to check if x is zero yet
current digit: d= x%16
x= (x-d)/16
if you're doing a string, convert d to the appropriate char
store d to the array
0
just divide by 16 and take the remainder if has, so it's will char %c , cause hex uses letters
This is simple example made to couple minutes, just show you that
https://code.sololearn.com/c8K40bfxF3nS/?ref=app
0
Shimaa
you could skip calculations and just use print formatting.
printf("#%02x%02x%02x", red, green, blue);
where red, green and blue are int values from 0 to 255.
%02x will convert to 2-digit hex lowercase.