Advertisement
Guest User

c++ problem

a guest
Sep 16th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. int hex_to_int(unsigned char input);
  8.  
  9. int main()
  10. {
  11.     char hex_input[6]; // tyto tri radky jsem vygooglil :-D
  12.     puts("Zadejte HTML kod barvy (bez hashtagu): ");
  13.     fgets(hex_input, 6, stdin);
  14.  
  15.     int out[6];
  16.  
  17.     out[0] = hex_to_int(hex_input[0]); // tady jsem mel for cyklus, ale kdyz to nefungovalo, zkusil jsem to rucne
  18.     cout << out[0] << endl;
  19.     cout << hex_input[0] << endl; // tady to pokazde vypise -1
  20.  
  21.  
  22.     return 0;
  23. }
  24.  
  25. int hex_to_int(unsigned char input) {
  26. int result;
  27.  
  28. switch(input) {
  29. case '0': result = 0;
  30. case '1': result = 1;
  31. case '2': result = 2;
  32. case '3': result = 3;
  33. case '4': result = 4;
  34. case '5': result = 5;
  35. case '6': result = 6;
  36. case '7': result = 7;
  37. case '8': result = 8;
  38. case '9': result = 9;
  39. case 'a': result = 10;
  40. case 'A': result = 10;
  41. case 'b': result = 11;
  42. case 'B': result = 11;
  43. case 'c': result = 12;
  44. case 'C': result = 12;
  45. case 'd': result = 13;
  46. case 'D': result = 13;
  47. case 'e': result = 14;
  48. case 'E': result = 15;
  49. case 'f': result = 16;
  50. case 'F': result = 16;
  51. default: result = -1;
  52. }
  53.  
  54. return result;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement