Search
 
SCRIPT & CODE EXAMPLE
 

C

change base int in c

// C program to convert a number from any base
// to decimal
#include <stdio.h>
#include <string.h>
 
// To return value of a char. For example, 2 is
// returned for '2'.  10 is returned for 'A', 11
// for 'B'
int val(char c)
{
    if (c >= '0' && c <= '9')
        return (int)c - '0';
    else
        return (int)c - 'A' + 10;
}
 
// Function to convert a number from given base 'b'
// to decimal
int toDeci(char *str, int base)
{
    int len = strlen(str);
    int power = 1; // Initialize power of base
    int num = 0;  // Initialize result
    int i;
 
    // Decimal equivalent is str[len-1]*1 +
    // str[len-2]*base + str[len-3]*(base^2) + ...
    for (i = len - 1; i >= 0; i--)
    {
        // A digit in input number must be
        // less than number's base
        if (val(str[i]) >= base)
        {
           printf("Invalid Number");
           return -1;
        }
 
        num += val(str[i]) * power;
        power = power * base;
    }
 
    return num;
}
 
// Driver code
int main()
{
    char str[] = "11A";
    int base = 16;
    printf("Decimal equivalent of %s in base %d is "
           " %d
", str, base, toDeci(str, base));
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: While loop output 
C :: <fileset joomla 
C :: Tensorflow: What are the "output_node_names" for freeze_graph.py in the model_with_buckets model? 
C :: block quote in lua 
C :: What does x = (a<b)? A:b mean in C programming? 
C :: rand in c 
C :: solidity signature r s v 
C :: C static libraries (creating object files) 
C :: arrow keys gaming keyboard 
C :: extended euclidean algorithm to find x and y 
C :: What keyword covers unhandled possibilities? 
C :: asasz 
C :: how to make an integer value equal to character 
C :: gnunet 
C :: Implement N-Queen Problem 
C :: garbage collection and dangling reference 
C :: C Nested if...else 
C :: C #define preprocessor 
C :: anticonstitutionnellement 
C :: how to turn off bash 
C :: time to apply pmfby 
C :: programmation c 
C :: calculate max of three numbers using ternary operator in c 
C :: pre and post increment in c 
Dart :: flutter remove debug flag 
Dart :: flutter keyboard overflow when opens 
Dart :: multi dex flutter 
Dart :: how to print in the same line in dart 
Dart :: flutter chip label 
Dart :: dart string empty or null 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =