• AVR Freaks

Hot!Cast array to int

Author
Tecnosuono
New Users
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/11/06 00:47:00
  • Location: 0
  • Status: offline
2019/11/07 07:45:19 (permalink)
0

Cast array to int

Premise: I am new to the programming of PICs.
I use MPLAB X and XC8 and I'm programming a 16f887A.
I created this function to acquire a 5-digit number using a keypad keyboard. To do this I use an array of 5.
 
 
I need to convert the value of the array to int to be able to use it for mathematical operators. Example:
using the keyboard I enter the radius of a circle and want to calculate the circumference.
thanks a lot
#1

10 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 18033
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Cast array to int 2019/11/07 08:37:29 (permalink)
    0
    5 binary numbers, or 5 ASCII ones?
     
    assuming Binary and that the lowest number is in array 0
    just make a for loop.
    unsigned int total - 0;
    signed char  X;
    for(X = 5; X > 0; X--)
    {
       total = total * 10;
       total = total + array[X];
    }
     
     
    Something like this.
    Note this is just one way, there are others.
    #2
    1and0
    Access is Denied
    • Total Posts : 9982
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Cast array to int 2019/11/07 08:46:02 (permalink)
    +1 (1)
    ...or, let's say abcde are the 5 digits, then
    uint16_t num = 10*(10*(10*(10*a + b) + c) + d) + e;

    which is same thing as
    uint16_t num = 10000*a + 1000*b + 100*c + 10*d + e;

    post edited by 1and0 - 2019/11/07 08:48:50
    #3
    ric
    Super Member
    • Total Posts : 24579
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cast array to int 2019/11/07 12:30:54 (permalink)
    +3 (3)
    and note, "cast" is not the correct verb to use here. You actually need to do calculations to convert 5 digits into a number, which is way beyond what a "cast" can do.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    1and0
    Access is Denied
    • Total Posts : 9982
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Cast array to int 2019/11/07 13:04:16 (permalink)
    +1 (1)
    and keep in mind that an "int" is 16 bits. That is, an unsigned int maxes out at 65535. For larger numbers, use unsigned long.
    #5
    Tecnosuono
    New Users
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2019/11/06 00:47:00
    • Location: 0
    • Status: offline
    Re: Cast array to int 2019/11/07 23:30:03 (permalink)
    0
     
    Thanks for the valuable answers. For completeness I attached the code used to read the data from the keyboard and create the array.
     
    #6
    PStechPaul
    Super Member
    • Total Posts : 2437
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Cast array to int 2019/11/08 00:19:16 (permalink)
    0
    Looks mostly OK, but a few points to mention:
    #include <xc.h>
    #include "lcd_pad.h"
    ........
    int Key = 'n';// Not necessary to initialize
    int buffer[5]={0}; // Not necessary to initialize
    int index = 0;// Not necessary to initialize

    Lcd_Set_Cursor (2,1);
    for (index = 0; index<5; index++) //This is a more usual form
    {
    Key = GetKey ();
    buffer[index] = Key;
    Lcd_Write_Chr ( buffer[index] );
    //index++; //See above
    }
    __delay_ms(500);
    Lcd_Clear();

    Actually, you don't need the buffer - you can just use:
    Lcd_Write_Chr ( GetKey() );

    Initialization may take a few extra lines of code.

     
    #7
    PStechPaul
    Super Member
    • Total Posts : 2437
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Cast array to int 2019/11/08 00:22:32 (permalink)
    0
    Note that I had to fudge the function so the post would be accepted:
     
    Lcd_Write_Chr ();

     
    #8
    pcbbc
    Super Member
    • Total Posts : 1381
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Cast array to int 2019/11/08 03:50:57 (permalink)
    0
    PStechPaul
    Note that I had to fudge the function so the post would be accepted:
     
    Lcd_Write_Chr ();

    Whitespaces before the open bracket are fine.  Not a usual coding style I'll grant you...
     
    Here's my preferred alternative workaround...
    Lcd_Write_Char​();

    #9
    1and0
    Access is Denied
    • Total Posts : 9982
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Cast array to int 2019/11/08 07:00:08 (permalink)
    +1 (1)

    int Key = 'n';
    int buffer[5]={0};
    int index = 0;

    If GetKey() returns an ASCII value instead of a binary value, you'll have to modify the above suggested snippets to convert it to a binary value (by subtracting '0') before calculation, and its data type should be "char".  If it is a binary value, use "unsigned char" type instead. In fact, all three variables should be "unsigned char"; the reason is always use the smallest type possible.  That being said, I suggest you to #include <stdint.h> and use "uint8_t" type.
    #10
    PStechPaul
    Super Member
    • Total Posts : 2437
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Cast array to int 2019/11/08 12:00:04 (permalink)
    0
    pcbbc
    PStechPaul
    Note that I had to fudge the function so the post would be accepted:
     
    Lcd_Write_Chr ();

    Whitespaces before the open bracket are fine.  Not a usual coding style I'll grant you...
     
    Here's my preferred alternative workaround...
    Lcd_Write_Cha r​();



    That didn't seem to work for me. But I don't know if the whitespace was needed or "Chr" in place of "Char". I would hit "submit" but nothing happened until I made the edits. The offending code seemed to be:
    Lcd_Write_Cha r(buffer[index]);

    Maybe it will work today? NOPE!

     
    #11
    Jump to:
    © 2019 APG vNext Commercial Version 4.5