There is such a problem. It is necessary to translate the number into the hexadecimal number system. Replacing the numbers 10-16 with letters made in the subroutine through the switch. But after the execution instead of the desired letter, the program returns the code of the letter from ASCI.

How to make it return exactly the letter?

int iftool(char t) { switch (t) { case 0: return t = 0; break; // // ... // case 9: return t = 9; break; case 10: return t = 'A'; break; case 11: return 'B'; break; case 12: return t = ('C'); break; case 13: return t = ('D'); break; case 14: return t = ('E'); break; case 15: return t = ('F'); break; } } 

the part that translates from 2nd to 16th system.

 l=0; r=1; m=1; n=0; while (l<=k) { n=(n+(number[l]*r)); if ( m%4==0 || l==k) { r=1; m=1; t=n; t=iftool(t); mas[x]=t; n=0; printf ("lol-%d",mas[x]); x++; } else { m++; r=r*2; } l++; } 
  • Do you have a function returns what type? and what you need - rojaster
  • then there is literal for char even though the code is the same, it is intu all the same, but you return int, then the code is stored in t, and when outputting it is desirable then to do (char) t, then do char iftool ( char) - rojaster
  • now fixed the char iftool (char t) but the problem did not go away - Frank
  • There is an error (s) in your code. You write return t = 0; ... t = 9; ... t = 'A'; - as a result, it turns out that for values ​​from 0 to 9 you return not the code of the desired character, but the value itself. You would write return t = '0'; ... t = '9'; ... - then the function will return the character you need ... But still, the switch is too cumbersome and inefficient solution. Even if / else (if (t <= 9) return t + '0'; else return t-10 + 'A'; - without checking for validity, of course) look more efficient ... Although, maybe, modern optimizing compilers and will generate a good code ... - gote

3 answers 3

cast to char

  • but a better piece of this code, I don’t know how others are, but I’m not a telepath personally) - rojaster
  • indeed, the output in hex is solved in one line: printf ("% x", 15); I think the result is clear! - rojaster
  • if everything is so simple. The program must use a subroutine. as a subroutine, I decided to use a switch to change the value - Frank
  • do a dirty hack)) of type void iftool (int i) {printf ("% x", i);} - rojaster
  • But is there a question, is the task to display the entered number in the 16 system ?? - rojaster

Frank, I would advise you to use a simple array instead of switch:

char x16trans [] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

and further (in a - a number from 0 to 15):

char x16(int a) { return x16trans[a]; }

    source:

     #include<iostream> #include<string> using namespace std; int main() { int a[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; string b[] = {"0","1","2","3","4","5","6","7","8","9"}; string str = ""; int x1,x2; cin >> x1; int z[8]; for (int i = 0; i < 8;i++) { x2 = x1/16; z[i] = x1 - (x2*16); // cout << "x: " << x2 << "\t"<< "z: " << z[i]< x1 = x2; } for (int j = 8, j2 = 0; j > 0; j--,j2++) { for(int n = 0; n < 16; n++) if (z[j] == a[n]) { str = str + b[n]; } } cout << str; cin.get(); return 0; } 
    • a solution has already been found, but thanks anyway - Frank
    • one
      You are a little mistaken ... Apparently the array b should look like this: string b [] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; - gote