0

I am trialling a function that converts decimal numbers to binary, and I want to see if it is working or not. The problem is I am writing it in C for a Teensy micro controller, and I don't have many of the basic operations like printf. I am using a library that can only send information to the LCD screen as a string or double, so my only way to check if this function is working is to send the binary number to the LCD screen as a string or integer (if the binary number was 1010, the integer number would be 1010, not 10).

The libraries I am able to use are:

stdint
stdio
avr/io
avr/interrupt
util/delay
stdlib

Does anyone know how this can be done using the above libraries only?

EDIT: Included code as per comment request. The code I have used to convert the decimal to binary is:

uint8_t dec_to_bin(void){
    int n = 100;
    long long binaryNumber = 0;
    int remainder, i = 1;

    while (n!=0){
        remainder = n%2;
        n /= 2;
        binaryNumber += remainder*i;
        i *= 10;
    }
    return binaryNumber;
}

Then in the main function I have:

uint8_t a = dec_to_bin();
sprintf(a, "%u");

This returns the error: error: passing argument 1 of 'sprintf' makes pointer from integer without a cast

david_10001
  • 492
  • 1
  • 6
  • 22
  • Sure you have `printf()`. You just need to make your own `FILE`-like. – Ignacio Vazquez-Abrams May 24 '18 at 03:10
  • If you have `stdio` you should have `sprintf()`. – Barmar May 24 '18 at 03:11
  • I don't know how to do that, I started to learn C only weeks ago. I just want a way to convert it to a string somehow. Then I can display it fine. Barmar when I try to use sprintf I get "passing argument 1 of 'sprintf' makes pointer from integer without a cast" – david_10001 May 24 '18 at 03:12
  • @david_10001 You're using it incorrectly. If you post your code we can help you fix it. – Barmar May 24 '18 at 03:16
  • Thanks Barmar. I have included the code in my question. – david_10001 May 24 '18 at 03:20
  • Please, have a look into doc. [sprintf](http://en.cppreference.com/w/cpp/io/c/fprintf). (It's a C++ doc. but these functions are "borrowed" from standard C I/O library.) The first argument of `sprintf()` is the output buffer (which is missing in `printf()`. The following arguments are identical.) The next argument is the formatter. For each formatting sequence (e.g. `%u`) you have to provide an additional argument. So, this would work: `char buffer[10]; sprintf(buffer, "%u", a);` or `printf("%u", a);`. – Scheff's Cat May 24 '18 at 05:51
  • "...so my only way to check if this function is working..." Why don't you simply launch the program in your debugger? – Lundin May 24 '18 at 07:29
  • The dec_to_bin() function doesn't make sense. – Rev May 24 '18 at 07:32
  • I don't have any sort of debugger for this unfortunately, or I don't know about one. – david_10001 May 24 '18 at 09:49

1 Answers1

1

The definition for sprintf is

int sprintf ( char * str, const char * format, ... );

So if you want to convert your number into a string (and that is what sprintf does) you have to give it an array of char where to put the string:

char output[9];
uint32_t a = dec_to_bin(100);

sprintf(output, "%08lu", a);

That should solve your compile error.

The second problem is you dec_to_bin function. The return values does not matches the value you are returning.

uint32_t dec_to_bin( uint32_t n ){

    uint32_t binaryNumber = 0;
    uint32_t remainder, i = 1;

    //prevent overflow
    if ( n > 255) {
        return 0;
    }

    while (n!=0){
        remainder = n%2;
        n /= 2;
        binaryNumber += remainder*i;
        i *= 10;
    }
    return binaryNumber;
}
theSealion
  • 1,082
  • 7
  • 13
  • Since uint32_t has 10 digits, returning up to 10-bit "binary" is possible. So the overflow check can be (n > 1023) instead of (n > 255). In the original example, "long long", thus uint64_t was chosen to allow up to 20 digits. Obviously the sprintf code should/could be adjusted accordingly. – Rev May 24 '18 at 08:47
  • Thank you. I no longer have the compile error. However I can't see any text relating to the numbers in the terminal anywhere – david_10001 May 24 '18 at 11:18