0

So I'm trying to convert a number from different bases to 10. I tried debugging and I'm 100% sure that the problem is here : nr[i]=j; What it does is that it takes the integer j and converts it into ASCII, regardless of the fact that nr[i] is a char array. What can I do to fix this??

    int baza,cifra,nrB10,i,j,x,corect;
    char nr[32];
    char conversie[36]={'0','1','2','3','4','5','6','7','8','9','10','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    printf("Base between 2 and %d):",BAZA_MAX);
    scanf("%d",&baza);
    printf("Nr. in base %d: ",baza);
    scanf("%31s",nr);
    nrB10=0;
    corect=0;
    x=strlen(nr)-1;
    for (i=0;i<=x;i++) {
        corect=0;
        for (j=0;j<baza;j++) {
            if (nr[i]==conversie[j]) {
                corect=1;
                nr[i]=j;
            }
        }
        if (corect==0) {
            printf("Numar gresit!");
            break;
        }
    }
    for (i=0;i<=3;i++) {
        printf("%c",nr[i]);
    }
    return 0;
Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
manubmxnsb
  • 151
  • 1
  • 14
  • 1
    Because of %c. Try %d instead. But generally you should optimize your program and for example get rid of the loop over j and also conversie array. There is a big room for improvements – rhaport Jan 08 '20 at 21:25
  • 2
    I am pretty sure `'10'` is not a single character in any base. – KamilCuk Jan 08 '20 at 21:26
  • 1
    @KamilCuk base 11 perhaps ... ;-) – Support Ukraine Jan 08 '20 at 21:27
  • What I'm trying to do in that loop is that I'm taking the numbers from other bases and convering them to Integers into that string. So If I have a string '3A' it should output nr[0]=3 and nr[1]=A. Not sure if I'm having any confusions here or not – manubmxnsb Jan 08 '20 at 21:29
  • Problem is ,instead of taking nr[0]=3 it takes nr[0]=3 (IN ASCII) which is 'EXT -end of text' in that case – manubmxnsb Jan 08 '20 at 21:30
  • @AndreyTimofeev where do you mean? – manubmxnsb Jan 08 '20 at 21:30
  • @manubmxnsb printf("%c",nr[i]) – rhaport Jan 08 '20 at 21:31
  • Yes, it does work now. But if i'm still trying to use that variable in further calculus, how can i do that? – manubmxnsb Jan 08 '20 at 21:32
  • Note that _everything_ in your computer is bits that we interpret as numbers. There is no "conversion" to characters, there are no chars in your computer, only numbers. What you're seeing is the _presentation_ of that data. The ASCII char `'a'` is the base 10 number 97, is the base 2 number 0b1100001, is the base 16 number 0x61, etc. You can display this data any way you like, but the intrinsic value stored in the computer is still the same. Same goes for file types. Word docs, media files, etc.. they're all just strings of bits. The only difference is in these is how the bits are interpreted. – yano Jan 08 '20 at 21:44
  • 3
    It is not at all clear what your problem is because you are conflating the code error with your misunderstanding of data types and character representation. Add to the question an example of _input_, your _expected or required output_ and _the actual output_. That will make it much clearer what you are asking, and you will probably get an solution and and have your misunderstanding cleared up. – Clifford Jan 08 '20 at 22:48

1 Answers1

1

When you store a number to a variable, it's just a number. It's a pattern of bits, and the computer does not really know (or care) what it represents. When you specify an int, or a char, or whatever, you are telling the compiler how to expect it to be used. That's so it can catch mistakes. In your case, the number "becomes" what you think it might be, when you print it. The specifier in the printf is what dictates this. Here are two examples:

printf("%c", 65);    // It will print an A

printf("%d", 'A');    // It will print 65

Similarly, if you could look inside where the computer was holding the 'A' and the 65, they would look exactly the same. Looking at a memory dump, you could not tell which was which.

Here, I'll do the same kind of thing, and throw in hexadecimal for added entertainment:

int n = 65;
printf("%c, %d, %x", n, n, n);     // It prints, A, 65, 41
gbarry
  • 10,352
  • 6
  • 33
  • 43