193030

Hex string to hex int and rgb

Apr 24th, 2020
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6.  
  7. {
  8.  
  9. int red, green, blue;
  10.  
  11. unsigned long rgb = 0xff0000;
  12. string s = "#ff00ff";
  13. s.erase(0,1);
  14. int num = stoi(s, 0, 16);
  15.  
  16. cout << rgb << endl;
  17. cout << "num: " << num << endl;
  18. rgb = num;
  19. red = rgb >> 16;
  20.  
  21. green = (rgb & 0x00ff00) >> 8;
  22.  
  23. blue = (rgb & 0x0000ff);
  24.  
  25. rgb = 0;
  26. rgb |= red <<16;
  27. rgb |= blue <<8;
  28. rgb |=green;
  29. cout << "red: " << red << "green " << green<< "blue: " << blue<<  endl;
  30.  
  31.  
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment