Hello :D I would like this to be explain.
What is meaning of this following codes and why are they in there? I really don't understand.

void Binary2Decimal()

total=0
"%16s"
len=strlen(bin2);
value=bin2[i]-'0';
total=(total<<1|value);

void Octal2Decimal()

for(i=0;i<len;++i)
value=buffer[i] - '0';
if (value < 0 && value > 7)

void Hexa2Decimal()

digit=toupper(buffer[i]);
if (digit >= '0' && digit <= '9')
value = digit - '0';
else if (digit >= 'A' && digit <= 'F')
value = digit - 'A' + 10;
else
printf("Invalid hexadecimal data\n");
return;

    void Binary2Decimal()
    {
    char bin2[17];
    int len,i,total=0,value;
    gotoxy(1,13);printf("[BINARY TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter a Binary number: "); //Accept a maximum of 15 digits only in range of 32768.
    scanf("%16s", bin2);
    printf("\n");

    len=strlen(bin2);
    for(i=0;i<len;++i)
     {
       value=bin2[i]-'0';
        if (value!=0 && value!=1)
           {
              printf("Invalid binary data\n");
                 return;
          }
         total=(total<<1|value);
       }
      printf("Decimal equivalent is: %d\n", total);
     }

   void Octal2Decimal()
    {
    char buffer[6];
    int len,i,total=0,value;
    gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter an Octal number: "); //Accepts a maximum of 5 digits only in range of 32767.
    scanf("%5s", buffer);
    printf("\n");
    len=strlen(buffer);
    //Convert and check if we have a valid data
    for(i=0;i<len;++i)
          {
            value=buffer[i] - '0';
             if (value < 0 && value > 7)
             {
                printf("Invalid octal data\n");
              return;
           }
         total = (total*8)+value;
         }
        printf("Decimal equivalent is: %d\n", total);
      }

   void Hexa2Decimal()
    {
    char buffer[5],digit;
    int len,i,total=0,value;
    gotoxy(1,13);printf("[HEXADECIMAL TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter a Hexadecimal number: "); //Accepts a maximum of 4 digits only in range of 32767.
    scanf("%4s", buffer);
    printf("\n");
    len=strlen(buffer);
    //Convert and check if we have valid data
    for (i=0;i<len;++i)
           {
             digit=toupper(buffer[i]);
               if (digit >= '0' && digit <= '9')
              {
                 value = digit - '0';
                    }
                      else if (digit >= 'A' && digit <= 'F')
                          {
                    value = digit - 'A' + 10;
                   }
                   else
                {
              printf("Invalid hexadecimal data\n");
              return;
            }
         total=(total*16)+value;
        }
       printf("Decimal equivalent is: %d\n", total);
    }

Recommended Answers

All 5 Replies

where does the code come from?
what were you looking for when you found it?

Turbo C++ .Convertion from any base to base 10.

Then the codes are probably (and the function names would appear to support) that they convert a particular base to base 10)

Hey John,

if you really wish to learn to code in C++ and / or C, you have one really great thing going for you, that all programmers need ...

i.e. great persistance :)

Once you have a good grasp on some basics ... things like how to keep the stdin buffer flushed (or in C++ ... the cin stream ... flushed) ... and input validation ... and getting a feel for scanf and fgets (or in C++, cin and getline) ... and a few other basics ... you WILL BE WELL on your way.

Just write a lot of code ... bottom up ... in small steps.

Make sure, that at each step, your code compiles and gives all the EXACT expected output, that you wanted that program code to produce ...

Then add a small next block of code,
compile,
debug,
etc until you get the EXACT output desired.

(Or if you always keep a copy of your previous working step, and you simply can-not find the bug(s) in this next step, just go back and start coding fresh from the last working stage of developement.)

With all the helpful info on the web so accesible these days, once you have a few basics well understood, you can then make rapid progress ... but only if you UNDERSTAND what each code snippet is doing and why it is used.

The joy of computing, these days, once you get some basics inderstood, is that you can try code out for your self ... and 'see' ... when it works :)

And of couse, there are several here at Dani who are very very gracious in their help.

Are you aware that Dani has a personal messaging facility?

Thanks sir David W and I'm really thankful to this site because my program was accepted by my prof and she was so impress about it. I explain it every codes. Thank a lot for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.