mardi 30 juin 2020

C problem: Trying to convert numbers to binary and hexadecimal, but not getting the right output

I thought my logic was sound, but when I input a number I just get a row of zeroes for the binary conversion and a lot of AAAAs for the hexadecimal conversion. I was originally gonna make hexLetter a character array, but couldn't get it working right. When I tried debugging, I noticed that loop in the binary part completely skips to the printing phase and the if statement goes through the A part multiple times, even if the remainder isn't 10. This happens even if I change the number and character.

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h> 

/*
 * 
 */
int main(int argc, char** argv) {
    
    int base10;
    int conversion;
    int i=8;
    int bits[32]={};
    char hex[9];
    int quotient=base10;
    int remainder;
    char hexLetter ='A';
    
    
    printf("Enter a number in base 10: \n");
    scanf("%d", &base10);
    printf("Base 10 is: %d\n", base10);
    
    
    
    for(i<9; i=0; --i) { 
        conversion = pow(2,i); //converts each possible 1 to its decimal equivalent for analysis
        if (base10 < conversion) { 
            bits[i]=0;  }
            else{ (bits[i]=1);
            base10 = base10 - conversion; } } //subtracts converted amount from original number} } 
               
           printf("%d %d %d %d %d %d %d %d %d is the decimal number in binary\n", bits[8], bits[7], 
           bits[6], bits[5], bits[4], bits[3], bits[2], bits[1], bits[0]); //backwards so binary number evaluates from right to left
           
           
           
           
           
           //divide by 16 with remainders
           //keep track of remainders
           //divide quotient by 16 until quotient is 0
           //print remainders as one string
           
     printf("Hexadecimal is: ");   
    while(quotient!=0) {
        remainder = quotient % 16; //divide for remainder
            if ( remainder = 10 ) {
            hexLetter ='A';
             printf("%c", hexLetter); } //print hexadecimal equivalent of remainder
        
            else if( remainder =11 ) {
               hexLetter ='B';
                printf("%c", hexLetter); }
        
            else if( remainder =12 ) {
               hexLetter ='C';
               printf("%c", hexLetter); }
        
            else if( remainder =13 ) {
               hexLetter ='D';
               printf("%c", hexLetter); } 
        
            else if( remainder =14 ) {
               hexLetter ='E';
               printf("%c", hexLetter); }
        
            else if( remainder = 15 ) {
               hexLetter ='F';
               printf("%c", hexLetter); }         
                    
            else printf("%d ", remainder); //print integer otherwise
        quotient = quotient /16; } //divide for new quotient   
           
    return (EXIT_SUCCESS);
}

Aucun commentaire:

Enregistrer un commentaire