Thread: Convert String type to Integer type

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    20

    Convert String type to Integer type

    Working on a project and I have hit a wall. I created a RNG for a microcontroller where users can choose the upper limit of the RNG, so it would pick a number between 1 and upper limit. However, I am having to print this info into an LCD module. All the code works, except it will not print the actual generated number onto the LCD. I believe it is because due to the way the code works, the type for the variable is a String rather than an Integer (but I am not certain). Is there any way to convert a String type to an Integer type?

    I have included some of the code below, this is not everything. This project is a combination of C language and Assembly.

    Code:
    int lower;
    char upper;
    int count;
    char die;
    char dieResult[4];
    
    
    
    void printRandoms(int numFaces, int count)
    {
        int i;
        int num = 1;
        
        for (i = 0; i < count; i++) {
            num = rand() % numFaces + 1;
            ASCII = '0' + num;
        }
        memset(dieResult, 0, 4);            //memset(result) is a char type
        i = log10(num);
        while (num != 0) {
            dieResult[i] = '0' + (num % 10);
            num /= 10;
            i--;
        }
    }
    
    void RNG()
    {
        UART_Puts(MS7);
        UART_Puts(MS6);
        UART_Get();
        while (ASCII != '.') {
            die = die * 10 + (ASCII - '0');
            UART_Get();
        }
        upper = die;
        char *result;
        char buffer[20];
        printRandoms(upper, 1);
        sprintf(buffer, "Rolled d%d: %s", upper, dieResult);
        
        //int value;
        //value = atoi(dieResult);    //this was an attempt at converting string to int
        
        result = buffer;
        UART_Puts(result);
        DATA = 0x34;
        LCD_Write_Command();
        DATA = 0x08;
        LCD_Write_Command();
        DATA = 0x02;
        LCD_Write_Command();
        DATA = 0x06;
        LCD_Write_Command();
        DATA = 0x0f;
        LCD_Write_Command();
        char lcdOut[16];
        sprintf(lcdOut,"d%d: %s", upper, dieResult); //can replace %s with %p to print in hexadecimal
           //I think this doesnt print the dieResult because its a string and not an int
        //sprintf(lcdOut,"d%d: %s", upper, dieResult);
        LCD_Puts(lcdOut);        //this doesn't work. Seems like dieResult has some memory error
        //free(result);            //cant have this free() as it isnt a pointer allocated via malloc()
        //die = 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you've already got sprintf, why make things complicated with printRandoms ?

    Additionally, the loop generates count random numbers in num, but uses only the last one.

    What is that ASCII variable that you keep reading from and writing to?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    20
    Honestly I have no idea, it is a group project and we cant seem to figure out sprintf for the dieResult. We could probably consolidate it into a single block but we just wanted to get it working first.

    Those variables are used in other portions of the code. Same with ASCII, it is apart of the Assembly code that I didnt include.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        die = 0;
        while (ASCII != '.') {
            die = die * 10 + (ASCII - '0');
            UART_Get();
        }
    So at the end of this, you sent say "12." down the UART serial line and the value stored in die is now 12.

    > upper = die;
    A distraction, remove it.

    > printRandoms(upper, 1);
    In other words, printRandoms(die, 1);

    So you want 1 random roll of the 12 sided dice here - right?
    Is it always 1?

    You may as well delete the whole function and just do
    Code:
    int roll = (rand() % die) + 1;
    sprintf(buffer, "Rolled d%d: %d", die, roll);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    20
    You may as well delete the whole function and just do
    Code:
    int roll = (rand() % die) + 1;
    sprintf(buffer, "Rolled d%d: %d", die, roll);
    When doing this, I no longer receive a random generated number it always outputs the same number.

    And the dice is not always 12 sided, the user gets to choose how many sides the dice will have.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > And the dice is not always 12 sided, the user gets to choose how many sides the dice will have.
    And how is that different to what you have already?
    12 was just my example for what YOU type in somewhere that gets received by UART_Get();
    Type in something else, and you'll get a different upper limit.

    > When doing this, I no longer receive a random generated number it always outputs the same number.
    Then you're calling srand() repeatedly with some constant.

    There was no change to the number of times rand() was called, so whatever you were seeing was already broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert type using string with switch error
    By gabeperron in forum C# Programming
    Replies: 1
    Last Post: 04-21-2019, 07:05 PM
  2. 'char' integer type or character type?
    By password636 in forum C Programming
    Replies: 5
    Last Post: 09-26-2012, 09:50 AM
  3. Convert String to enum type
    By c9dw2rm8 in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 07:00 PM
  4. How to convert type string to char/double ?
    By Hermitsky in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2004, 03:25 PM
  5. Convert type string to a number
    By ejholmes in forum C# Programming
    Replies: 2
    Last Post: 04-21-2004, 01:33 AM

Tags for this Thread