Thread: string into int

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    string into int

    I am trying to obtain data from a serial port. I managed to obtain the correct numbers to display on the screen with the following code:

    Code:
    char sbuf[256];
    			for(unsigned int i=0; i<count; )
    			{
    				int len=0;
    				int first_byte = i;
    				for(int j=0;(j<24)&&(i<count);j++,i++)
    				{
    				len += sprintf(&sbuf[len], "%02X ", data[i]);
    				if (i==1)
    				{
    					printf("\n%s", sbuf);
    				}
    				}
    
    			}
    So my output(sbuf) looks like this:
    Hex value:
    FF 00
    01 24
    44 FD
    02 47........
    Now I want to get this sbuf value and store into an unsigned int. Eg: FF 00 would become 65280.
    But everytime I try to convert sbuf into a char or int I get the same number over and over again: FF 00 returns 100023 and 01 24 and all numbers return 100023.
    I am really lost as to why this is happening can anyone enlighten me please?

  2. #2
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    ok. so I changed my sprintf statement to :
    Code:
    len += sprintf(&sbuf[len], "%i ", data[i]);
    				if (i==1)
    				{
    					printf("\n%s", sbuf);
    					int isavar;
    					isavar = atoi(sbuf);
    					printf("\n dec %i",isavar);
    				}
    Only problem now is that atoi(sbuf) only gets the first part of my number. So:
    if sbuf is: 136 34
    isavar returns 136. And what I really want isavar to be is:8738
    136 34 ->1000100 0100010 ->8738
    Last edited by doia; 04-02-2010 at 03:19 PM.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    It's because sbuf is "FF00", the characters 'F', 'F', '0', '0', and not the data 0xff00. You'll have to read it back in using scanf("%02X", &result) [ie, the same way you wrote the data].

    EDIT: actually the spaces are gonna screw you up on this - you'll have to read in 2 values anyway. go with the second solution...

    Or better yet, since you have the original data anyway, just put it together yourself:
    Code:
    /*bit-shift example*/
    int low = 0x00;
    int high = 0xFF;
    int word = low + (high << 8); //or high*256 if you prefer
    
    /*now word = 0xFF00*/
    Last edited by bernt; 04-02-2010 at 03:02 PM.
    Consider this post signed

  4. #4
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    I am still confused, how would I go about getting the "low" part. All I can seem to get is the high

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you can use shorts instead of ints, perhaps you could use this:

    Code:
    unsigned short num = (short)strtol(buf, NULL, 16);

  6. #6
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    unsigned short still doesn't fix my problem:
    Code:
    if (i==1)
    				{
    					printf("\n%x",  data[i]);
    					int isavar = (int)(data[i]);
    					printf("\n dec %i",isavar);
    				}
    If I don't use my sbuf I don't know how to get the correct values out of data.
    When I run the code above to just printf my original data (data[i]) I get the same value everytime ... and it's the wrong one. Meanwhile when I printf sbuf like I was doing before (see code in my last post) I get the correct ones.
    So unless I can obtain the correct values from data[i] as a number I need to get the second part of what sbuf is holding (my 'low' part of the number)

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I'm not sure I get you, but what datatype is data then?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do you mean something like that:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        unsigned int x = 0xabcdFF23;
        unsigned char data[sizeof (unsigned)];
        int i;
        unsigned temp = x;
        char buf[256];
        int len = 0;
        char* pEnd = buf;
        printf ("%X\n", x);
        for(i = 0; i < sizeof (unsigned); i++)
        {
            data[i] = temp & 0xFF;
            temp >>= 8;
        }
        while(i> 0)
        {
            len += sprintf(buf+len,"%02X ", data[i-1]);
            i--;
        }
        printf("%s\n", buf);
    
        temp = 0;
        while(*pEnd)
        {
            unsigned char res = strtol(pEnd,&pEnd,16);
            temp <<= 8;
            temp |= res;
            pEnd++;
        }
    
         printf("%X\n",temp);
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        unsigned int x = 0xabcdFF23;
        unsigned char data[sizeof (unsigned)];
        int i;
        unsigned temp = x;
    Is there any particular reason that you specify 'unsigned' some times, and 'unsigned int' others?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by quzah View Post
    Is there any particular reason that you specify 'unsigned' some times, and 'unsigned int' others?
    laziness
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    Talking

    Quote Originally Posted by vart View Post
    do you mean something like that:
    yes that works great! thanks for all the help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM