Converting an int to an address in an array

This is a discussion on Converting an int to an address in an array within the C Programming forums, part of the General Programming Boards category; Hello. This is my first post and I welcome any and all advice on this particular program I am writing. ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Converting an int to an address in an array

    Hello. This is my first post and I welcome any and all advice on this particular program I am writing. I am trying to take any int value and convert it to a constant address off an array. For instance, if I enter the value 8, the output should be 0x08 as a string. I suspect there maybe a simpler way of doing this, but I am at a loss. I get the following errors when I compile:

    main.c:54: warning: passing arg 1 of `ConvertStringToTable' makes integer from pointer without a cast
    main.c:57: error: syntax error before '(' token
    main.c:57:17: warning: multi-character character constant
    main.c: In function `ConvertStringToTable':
    main.c:61: error: syntax error at end of input

    Eventually I want to take micro controller output as an int value and produce a matching address which I will use for an on screen display, but I'd like to get this simpler version working first.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    // 
    // defining constants
    const unsigned char charcodetable[] =
    {
        0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
    };
    
    
    const unsigned char ascii_table[] =
    {
        '1','2','3','4','5','6','7','8','9','0',
    };
    
    unsigned char ConvertStringToTable (unsigned char data)
    {
    	unsigned char outchar=0x99;
    	unsigned int i;
    
        for (i=0; i< sizeof(ascii_table); i++)
        {
            if (data == ascii_table[i])
            {
                outchar = charcodetable[i];
               break; 
            }
        }
    	
        if (outchar!=0x99)
    	{
    		return outchar;	
    	}
    	else
    	{
    		return data;
    	}
    
    int main() 
    {
        int num = 1;
        unsigned char buf[15];
        unsigned char dummy;
        
         itoa (num, buf, 10);
    
    	printf("%s\n", buf);
    	
        do 
    	{
    	dummy = ConvertStringToTable (buf);
    	printf ("%s\n",dummy);
    	
        } (dummy != '0x99');
        
        System ("Pause");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    If you want to convert the integer to a hexadecimal value you can use sprintf or snprintf.

    The compiler errors tell you where to look.

    Line 57, it's a do while loop.
    Code:
    do 
    	{
    	dummy = ConvertStringToTable (buf);
    	printf ("%s\n",dummy);
    	
        } (dummy != '0x99');
    Should be:
    Code:
    do 
    	{
    	dummy = ConvertStringToTable (buf);
    	printf ("%s\n",dummy);
    	
        } while (dummy != '0x99');
    Line 54 should tell you to look at the data type you're passing the the function ConvertStringToTable. When you look at the function there's quite a few issues.. If you don't want to use sprintf etc then you need to take a look at the function.

    Some questions you might want to try answering:
    1. Why are you using the size of the ascii_table to parse the string data.
    2. Is the function parameter correct - expecting a single character?
    3. Is the function return value really meant to be a character - if so why are you printing a string in main.
    4. '0x99' refers to a character (http://www.ieee.li/computer/ascii.htm

    Apologies if this isn't very helpful.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not clear why you don't simply use an array of pointers. Aren't regular ptr addresses what you want?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    There is not function called System either.
    Only system.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Quote Originally Posted by saeculum View Post

    Some questions you might want to try answering:
    1. Why are you using the size of the ascii_table to parse the string data.
    2. Is the function parameter correct - expecting a single character?
    3. Is the function return value really meant to be a character - if so why are you printing a string in main.
    4. '0x99' refers to a character (http://www.ieee.li/computer/ascii.htm

    Apologies if this isn't very helpful.

    1. I just want it cycle through numbers 0-9 for each decimal place. I don't want it to loop more than the number of digits in the original int value.

    2. I want it to convert one character at a time within the function. This is so I can just do a SPI_WRITE for each address within the function itself. This will be implemented later.

    3. This is just an arbitrary statement. I wanted to see what the output of the string is.

    4. These aren't the real ASCII characters, this chip I am using has a different address associated with each number. For instance 7, is actually 0x07.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,318
    I am a little confused. Why not state what is the expected input and the corresponding expected output?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Quote Originally Posted by Adak View Post
    I'm not clear why you don't simply use an array of pointers. Aren't regular ptr addresses what you want?
    I have very limited experience working with pointers. Is the address associated with an integer match to that of the table of ascii_table[] and charcodetable[]? If so, it would be much simpler to just output the pointer address.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    Quote Originally Posted by bommy View Post
    I have very limited experience working with pointers. Is the address associated with an integer match to that of the table of ascii_table[] and charcodetable[]? If so, it would be much simpler to just output the pointer address.
    what is acii_table which addresses you want to get?

    could you show several inputs and coresponding outputs you want to receive?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    I have attached the figure that contains where the characters are stored on the MAX7456 On screen display chip.
    [img=http://img254.imageshack.us/img254/3286/cforum.th.jpg]

    I must take an integer say 1234 and convert it to 0x01 then 0x02, then 0x03, and 0x04 (hex values) to output to the display registers on the MAX7456. I would then output the hex value 0x01 to the DISPLAY_DATA_IN register and another character placement register. Once 1 is outputted to the screen, then put 0x02 in the DISPLAY_DATA_IN register and then increment the character placement register.

    I hope I have clarified everything. If you have anymore questions, let me know.
    Last edited by bommy; 03-19-2009 at 02:36 PM.

  10. #10
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    There is no difference between 0x01 and 1. You only run into difficulties when you reach 0x0a, or 10. 1-9 should be easy, and simply replace 0 with 0x0a . . .

    Does that make sense?
    Last edited by Nightowl; 03-19-2009 at 02:39 PM.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    so the 'n' should be outputed as 0x32?

    or your input is limited to digits?

    also - the result should be string or array of integer values? What will be sent to display registers?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Or just create a custom hash table. C++ has this implemented as map. Retrieve stored strings using ASCII values . . . would be quite a simple operation, no? Or am I just missing out on the whole purpose here?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    So instead of making tables I could just send values 0-9 to the display registers directly? Say my integer is num = 1234, could I send num[3] to the display register? would num [3] be equal to 1 or 1000 in this case? I thought I would have to convert everything to a string first because I want to interpret the number 1 in the number 1234 as 0x01 and not 3E8 (which is 1000).

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    Quote Originally Posted by bommy View Post
    So instead of making tables I could just send values 0-9 to the display registers directly? Say my integer is num = 1234, could I send num[3] to the display register? would num [3] be equal to 1 or 1000 in this case? I thought I would have to convert everything to a string first because I want to interpret the number 1 in the number 1234 as 0x01 and not 3E8 (which is 1000).
    you have not answers my questions - is input only digits?
    shoudl output be string "0x01" or integer 0x01?

    Could you tell the differencee between two?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    What you do to isolate a digit:

    Code:
    digit = originalnumber % 100;
    digit /= 10;
    That isolates the tens column.

    Simply send that digit through, and it should work, I believe, as long as it's not 0.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21