0

I am trying to convert a number from base b to base 10 using the function base_b_to_10 as below:

long int base_b_to_10(char* B, long int base) //take in the number as string returns it converted to base 10 as long int
{
    long int N; //base 10 number:
    N = strtol(B, NULL, base); //
    return(N);
}

In the main program, the user will input the base of the input number and the input number itself. The length and the size of the string is unknown and is up to the user to define This is a simplified version of a big program I am trying to build in which the size of the string B is dynamically allocated in another function so I cannot do something like:

char B[100];

How to solve this? the compiler is returning nothing.

Fadi EID
  • 15
  • 3
  • “The compiler is returning nothing” is essentially meaningless. If you mean the compiler completes compilation without reporting an error, then say that, and show an example of the program running—show the exact text of input that reproduces the problem, show the exact text of the observed output, and show the output desired instead. If you mean something else, show the text of the compiler error message or whatever else you observe. – Eric Postpischil Aug 21 '22 at 15:03
  • does [this](https://stackoverflow.com/questions/59436781/c-using-malloc-and-realloc-to-dynamically-increase-string-length) question help – Anthony L Aug 21 '22 at 15:04
  • You don't need to assume the string is of arbitrary length. If the string is longer than some specific size, it can no longer fit in `long int`. You will need to deal with that fact anyway. – Cheatah Aug 21 '22 at 15:17
  • @Cheatah: Strings of arbitrarily long length may represent values that fit in a `long int` because they may start with leading spaces and leading zeros. – Eric Postpischil Aug 21 '22 at 15:22
  • Yes, them compiler completes compilation without an error but I see no outputs. – Fadi EID Aug 21 '22 at 15:37
  • Does this answer your question? [How can I read an input string of unknown length?](https://stackoverflow.com/questions/16870485/how-can-i-read-an-input-string-of-unknown-length) – Mark Rotteveel Aug 21 '22 at 16:43

3 Answers3

-1

Input string (here: test) can be of any reasonable size. Anyway, there might be some limitations, see: Strtol() and atol() do not convert strings larger than 9 digits

Output string is a pointer (here: *str) to position within input string (here: test). According to this, the size of the string to which str points does not matter. (Note: In this example, str is only valid as long as test is valid).

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

long int base_b_to_10(char* B, long int base, char **ret_str)
{
    long int N;
    N = strtol(B, ret_str, base);
    return N;
}

int main()
{
   long int val;
   char *str;
   char test[50] = "43724325HelloWorld";
   
   val = base_b_to_10(test, 10, &str);
   
   printf("val = %ld\n", val);
   printf("str = %s\n", str);
   
   return 1;    
}

Notes: I adapted your example just to be working (not improving or simplifying anything). It is probably clear that you could also directly use strtol function in the main routine. Moreover, it does not matter, how the input string was allocated (fixed size or dynamic size) - just needs to be allocated and needs to end by \0 (as usual).

sidcoder
  • 460
  • 2
  • 6
-1
  • If cross platforming is not an issue, and assuming you are running on a linux system, you can make use of asprintf().

    • It basically formats your string and allocates the memory for you (remember to free it afterwards).

    • This is not part of the standard library so it has its limitations.


  • Another approach is to use snprintf() to preddict the size of the output buffer then and use malloc()
#include <stdio.h>

int main ()
{
  int cx = snprintf ( NULL, 0, "18 characters long" );

  printf("%d", cx);

  return 0;
}

// OUTPUT:
// 18
-1

Ok so I solved this problem by first using a buffer string with size 50 (pretty sufficient), than I used the strlen() function to determine the size of the string given by the user, and lastly I allocate this number to the string I will be using for the conversion:

    char *str, buffer[50];

    printf("enter the input value: ");
    gets(buffer);

    int i;

    i = strlen(buffer);
    str = (char*)malloc(i*sizeof(char));
    strcpy(str, buffer);
    fflush(stdin);

    N = base_b_to_10(str, base_in);
Fadi EID
  • 15
  • 3
  • regaarding: `gets(buffer);` The function: `gets()` has been depreciated for years and completely removed from the C language circa 2009. If your compiler did not warn you of this problem, then strongly suggest obtaining a modern C compiler. – user3629249 Aug 23 '22 at 19:22