Thread: Takin last digits of a string and converting to a number

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    6

    Takin last digits of a string and converting to a number

    I am trying to write a program which takes a string, saves the last 4 digit as a 16-radix number and convert it to 10-radix number.

    In sum, I will just use it to measure distance by Blutooth module, works theorically :/

    I successfully get last digits of a defined string, and I don't think I will have a problem about make this work on the microcontroller
    Code:
    #include <stdio.h>
    
    int main()
    {
        char c[13]="kttt0FDH";
        int i;
    
    
    int m=0;
            printf("---\n");
    
    
        while(c[i]!=0)
        {
            printf("%c", c[i]);
            i++;
            m++;
        }
        //printf("\n%d", m);
    //    printf("\n%d", i);
        int y=m-4;
        int f;
        char k[4];
        i=0;
        for(f=y;f<=y+3;f++)
        {
        k[i]=c[f];
        i++;    
        }
            printf("\n\n%s", k);
    
    
        
        
        
        
        
        return 0;
    }
    But when I add the rest of the code, the computer stops it.
    Code:
    #include <stdio.h>#include <math.h>
    int main()
    {
        double RSSI;
        char c[13]="kttt0FDF";
        int i;
    
    
    int m=0;
            printf("---\n");
    
    
        while(c[i]!=0)
        {
            printf("%c", c[i]);
            i++;
            m++;
        }
        //printf("\n%d", m);
    //    printf("\n%d", i);
        int y=m-4;
        int f;
        char k[4];
        i=0;
        for(f=y;f<=y+3;f++)
        {
        k[i]=c[f];
        i++;    
        }
        //    printf("\n\n%s", k);
        
                
        for(i=0;i<=3;i++)
        {
            
        if(k[i]=='0')
               RSSI=RSSI+0*pow(16,3-i);
            
          if(k[i]=='1')
                RSSI=RSSI+1*pow(16,3-i);
               
          if(k[i]=='2')
                RSSI=RSSI+2*pow(16,3-i);
                
          if(k[i]=='3')
                RSSI=RSSI+3*pow(16,3-i);
                
            if(k[i]=='4')
                RSSI=RSSI+4*pow(16,3-i);
                
          if(k[i]=='5')
                RSSI=RSSI+5*pow(16,3-i);
                
          if(k[i]=='6')
                RSSI=RSSI+6*pow(16,3-i);
                
          if(k[i]=='7')
                RSSI=RSSI+7*pow(16,3-i);
                
              if(k[i]=='8')
                RSSI=RSSI+8*pow(16,3-i);
                
          if(k[i]=='9')
                RSSI=RSSI+9*pow(16,3-i);
                
          if(k[i]=='A')
                RSSI=RSSI+10*pow(16,3-i);
                
          if(k[i]=='B')
                RSSI=RSSI+11*pow(16,3-i);
          
            if(k[i]=='C')
                RSSI=RSSI+12*pow(16,3-i);
                
          if(k[i]=='D')
                RSSI=RSSI+13*pow(16,3-i);
                
          if(k[i]=='E')
                RSSI=RSSI+14*pow(16,3-i);
                
          if(k[i]=='F')
                RSSI=RSSI+15*pow(16,3-i);
                
          
            
            
            
            
            
            
        }
        
        
        
        
        return 0;
    }
    It results in:
    Adsız - Hızlı Resim
    The language is Turkish. It says as:"İsimsiz10.exe stopped working. A problem stopped the programs proper working.Windows will close it and tell if there is a solution." It says nothing then..
    I used switch-case, it did not work either.
    Code:
    for(i=0;i<=3;i++)    {
        
     
    
    
       switch(k[i]) {
          case '0' :
                RSSI=RSSI+0*pow(16,3-i);
                break;
          case '1' :
                RSSI=RSSI+1*pow(16,3-i);
                break;
          case '2' :
                RSSI=RSSI+2*pow(16,3-i);
                break;
          case '3' :
                RSSI=RSSI+3*pow(16,3-i);
                break;
            case '4' :
                RSSI=RSSI+4*pow(16,3-i);
                break;
          case '5' :
                RSSI=RSSI+5*pow(16,3-i);
                break;
          case '6' :
                RSSI=RSSI+6*pow(16,3-i);
                break;
          case '7' :
                RSSI=RSSI+7*pow(16,3-i);
                break;
               case '8' :
                RSSI=RSSI+8*pow(16,3-i);
                break;
          case '9' :
                RSSI=RSSI+9*pow(16,3-i);
                break;
          case 'A' :
                RSSI=RSSI+10*pow(16,3-i);
                break;
          case 'B' :
                RSSI=RSSI+11*pow(16,3-i);
                break;
            case 'C' :
                RSSI=RSSI+12*pow(16,3-i);
                break;
          case 'D' :
                RSSI=RSSI+13*pow(16,3-i);
                break;
          case 'E' :
                RSSI=RSSI+14*pow(16,3-i);
                break;
          case 'F' :
                RSSI=RSSI+15*pow(16,3-i);
                break;
          
          
          
          
          
          
          
          
          
       }
    }
        
        printf("\n\n%f", RSSI);
    My best guess is it is about the code after I seperated the last digits, but I could not solve it by myself after some trials and search.
    This works by itself:
    Code:
    #include <stdio.h>#include <math.h>
    int main()
    {
    	
    	char k[3];
    	scanf("%s", &k);
    	printf("%s\n", k);
    	double RSSI;
    int i;
    for (i=0;i<=2;i++)
    
    
    {
    	if(k[i]=='0')
    	RSSI=RSSI+5*pow(16,3-i);
    	
    	
    	
    
    
    		
    	
    }
     printf("%f", RSSI);
    
    
    
    
    
    
    
    
    
    
    return 0;
    }
    Thanks...

  2. #2
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Radix 16 is just base 16. So use if/switch statements to test for A to H. If it is 0 to 9 then multiply appropriately by the powers. For instance the first position is multiply by 1. So A x 1 = 10. The second position is multiply by 16. So B x 16 = ... Once all the positions are multiplied just add them together for a base 10 total. Radix 10. As far as strings are concerned use char data type. If you need further help I'll write the program. However it may take some time.

  3. #3
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    char *stringx;
    
    int length, endposition, valueHEX, tempx;
    
    unsigned long sum;
    
    sum = 0;
    
    stringx = "driverTN0010";
    
    length = 12;
    
    endposition = length;
    
    for(endposition = 12-4; endposition < length; endposition++)
    {
    
    if(stringx[endposition] == 'A')
    {
    valueHEX = 10;
    }
    
    if(stringx[endposition] == 'B')
    {
    valueHEX = 11;
    }
    
    if(stringx[endposition] == 'C')
    {
    valueHEX = 12;
    }
    
    if(stringx[endposition] == 'D')
    {
    valueHEX = 13;
    }
    
    if(stringx[endposition] == 'E')
    {
    valueHEX = 14;
    }
    
    if(stringx[endposition] == 'F')
    {
    valueHEX = 15;
    }
    
    if((stringx[endposition] >= 48)&&(stringx[endposition] < 58))
    {
    valueHEX = stringx[endposition] - 48;
    }
    
    if(endposition == 11)
    {
    sum = sum + (1*valueHEX);
    }
    
    if(endposition == 10)
    {
    sum = sum + (16*valueHEX);
    }
    
    if(endposition == 9)
    {
    sum = sum + ((16*16)*valueHEX);
    }
    
    if(endposition == 8)
    {
    sum = sum + ((16*16*16)*valueHEX);
    }
    
    }
    
    printf("\n HEX value: %lu", sum);
    
    return(0);
    }

  4. #4
    Registered User
    Join Date
    Jun 2016
    Posts
    6
    Quote Originally Posted by Tien Nguyen View Post
    Radix 16 is just base 16. So use if/switch statements to test for A to H. If it is 0 to 9 then multiply appropriately by the powers. For instance the first position is multiply by 1. So A x 1 = 10. The second position is multiply by 16. So B x 16 = ... Once all the positions are multiplied just add them together for a base 10 total. Radix 10. As far as strings are concerned use char data type. If you need further help I'll write the program. However it may take some time.
    Thanks for "base 16" info. My mother languge is not english so I miss some points.
    I could not get the main point though. So can I use A, B c etc.. s numbers even though they are char values and I did not include anythink about base 16? Is the mistake about data types?

  5. #5
    Registered User
    Join Date
    Jun 2016
    Posts
    6
    So I should try with pointers..
    Thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Barış
    I am trying to write a program which takes a string, saves the last 4 digit as a 16-radix number and convert it to 10-radix number.
    From what I see of your example, you have a string like "kttt0FDH", you want to focus on the last four characters, i.e., "0FDH", and then interpreting this as a text string in hexadecimal notation, you want to convert to it an integer, e.g., of type int. Is that right?

    The "10-radix number" seems to be a red herring because given that you seem to be trying to grapple with pow and what not, you are not looking to convert "0FDF" (I assume the H should have been F since H is not valid in typical hexadecimal notation) to "4063" (as in a text string with content in decimal notation), but rather to the integer value 4063.

    If so, then you should break this down into steps, and then write functions that do one thing and do them well. For example, you could declare:
    Code:
    const char *extract_hex_number(const char *input);
    The above function would examine input for the last four digits, then return a pointer to the first of the last four digits, if they form a valid hexadecimal string. Otherwise, it would return a null pointer.

    Then, you could declare:
    Code:
    int convert_hex_string_to_int(const char *hex);
    The above function would take the known hexadecimal string and convert it to an int. To do this conversion, you could write another function to convert a hexadecimal character to int:
    Code:
    int convert_hex_char_to_int(char hex);
    This is quite trivially implemented as a lookup table or even as a switch:
    Code:
    if (hex >= '0' && hex <= '9')
    {
        return hex - '0';
    }
    else
    {
        switch (hex)
        {
        case 'A':
            return 10;
        case 'B':
            return 11;
            /* etc */
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What you should be doing when you run into "the program has stopped working" is run the code in the debugger.

    So instead of the exception being caught by windows, it is instead caught by the debugger.
    This gives you the opportunity to look around the 'frozen' program state (stack trace, variables etc), where you might discover what is actually wrong.

    Further, the debugger allows you to set breakpoints, single step lines of code, change variables on the fly.


    Consider this compact solution.
    Code:
    #include <stdio.h>
    
    int main()
    {
      char c[13]="kttt0FDF";
      int value;
      if ( sscanf(&c[4],"%x",&value) == 1 ) {
        printf("Value=%d\n",value);
      }
      return 0;
    }
    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.

  8. #8
    Registered User
    Join Date
    Jun 2016
    Posts
    6
    Quote Originally Posted by laserlight View Post
    From what I see of your example, you have a string like "kttt0FDH", you want to focus on the last four characters, i.e., "0FDH", and then interpreting this as a text string in hexadecimal notation, you want to convert to it an integer, e.g., of type int. Is that right?

    The "10-radix number" seems to be a red herring because given that you seem to be trying to grapple with pow and what not, you are not looking to convert "0FDF" (I assume the H should have been F since H is not valid in typical hexadecimal notation) to "4063" (as in a text string with content in decimal notation), but rather to the integer value 4063.

    If so, then you should break this down into steps, and then write functions that do one thing and do them well. For example, you could declare:
    Code:
    const char *extract_hex_number(const char *input);
    The above function would examine input for the last four digits, then return a pointer to the first of the last four digits, if they form a valid hexadecimal string. Otherwise, it would return a null pointer.

    Then, you could declare:
    Code:
    int convert_hex_string_to_int(const char *hex);
    The above function would take the known hexadecimal string and convert it to an int. To do this conversion, you could write another function to convert a hexadecimal character to int:
    Code:
    int convert_hex_char_to_int(char hex);
    This is quite trivially implemented as a lookup table or even as a switch:
    Code:
    if (hex >= '0' && hex <= '9')
    {
        return hex - '0';
    }
    else
    {
        switch (hex)
        {
        case 'A':
            return 10;
        case 'B':
            return 11;
            /* etc */
        }
    }

    I have continuosly prıduced strings like these


    +INQ:E4F8:EF:C17131,5A020C,FFC4
    +INQ:E4F8:EF:C17131,5A020C,FFC0
    +INQ:E4F8:EF:C17131,5A020C,FFB7
    +INQ:E4F8:EF:C17131,5A020C,FFBF
    +INQ:E4F8:EF:C17131,5A020C,FFBC
    +INQ:E4F8:EF:C17131,5A020C,FFB7
    +INQ:E4F8:EF:C17131,5A020C,FFC1
    +INQ:E4F8:EF:C17131,5A020C,FFBE
    +INQ:E4F8:EF:C17131,5A020C,FFC6






    Only thing I want is to get a number equal to last 4 digit, considering last digits are 16 base numbers.
    So I can use these functions to convert unknowns types to use? Wow, I should learn this


    What you should be doing when you run into "the program has stopped working" is run the code in the debugger.

    So instead of the exception being caught by windows, it is instead caught by the debugger.
    This gives you the opportunity to look around the 'frozen' program state (stack trace, variables etc), where you might discover what is actually wrong.

    Further, the debugger allows you to set breakpoints, single step lines of code, change variables on the fly.


    Consider this compact solution.
    Code:
    [COLOR=white !important]?

    Code:
    1 2 3 4 5 6 7 8 9 10 11
    #include <stdio.h> int main() { char c[13]="kttt0FDF"; int value; if ( sscanf(&c[4],"%x",&value) == 1 ) { printf("Value=%d\n",value); } return 0; }
    [/COLOR]


    My friend showed me that just recently I did not know that either. :/ The result was like that
    C++ code
    - 96 lines - codepad




    As a result, by the report here and Tien Nguyen's code, I concluded that I should sum the value after the loop and changed the code to this.
    Code:
    #include <stdio.h>#include <math.h>
    int main()
    {
        double RSSI;
        char c[13]="kt000011";
        int i;
        int carpan[3];
    int m=0;
            printf("---\n");
    
    
        while(c[i]!=0)
        {
            printf("%c", c[i]);
            i++;
            m++;
        }
        //printf("\n%d", m);
    //    printf("\n%d", i);
        int y=m-4;
        int f;
        char k[4];
        i=0;
        for(f=y;f<=y+3;f++)
        {
        k[i]=c[f];
        i++;    
        }
        //    printf("\n\n%s", k);
        
                
        for(i=0;i<=3;i++)
        {
            
        if(k[i]=='0')
              // RSSI=RSSI+0*pow(16,3-i);
            carpan[i]=0;
          if(k[i]=='1')
               // RSSI=RSSI+1*pow(16,3-i);
               carpan[i]=1;
          if(k[i]=='2')
               // RSSI=RSSI+2*pow(16,3-i);
                carpan[i]=2;
          if(k[i]=='3')
               // RSSI=RSSI+3*pow(16,3-i);
                carpan[i]=3;
            if(k[i]=='4')
               // RSSI=RSSI+4*pow(16,3-i);
                carpan[i]=4;
          if(k[i]=='5')
               // RSSI=RSSI+5*pow(16,3-i);
                carpan[i]=5;
          if(k[i]=='6')
               // RSSI=RSSI+6*pow(16,3-i);
                carpan[i]=6;
          if(k[i]=='7')
               // RSSI=RSSI+7*pow(16,3-i);
                carpan[i]=7;
              if(k[i]=='8')
               // RSSI=RSSI+8*pow(16,3-i);
                carpan[i]=8;
          if(k[i]=='9')
                //RSSI=RSSI+9*pow(16,3-i);
                carpan[i]=9;
          if(k[i]=='A')
               // RSSI=RSSI+10*pow(16,3-i);
                carpan[i]=10;
          if(k[i]=='B')
               // RSSI=RSSI+11*pow(16,3-i);
          carpan[i]=11;
            if(k[i]=='C')
               // RSSI=RSSI+12*pow(16,3-i);
              carpan[i]=12;  
          if(k[i]=='D')
               // RSSI=RSSI+13*pow(16,3-i);
              carpan[i]=13;   
          if(k[i]=='E')
               // RSSI=RSSI+14*pow(16,3-i);
              carpan[i]=14;   
          if(k[i]=='F')
               // RSSI=RSSI+15*pow(16,3-i);
              carpan[i]=15;   
          
            
            
            
            
            
            
        }
        for(i=0;i<=3;i++)
        {
        RSSI=RSSI+carpan[i]*pow(16,3-i);
        }
        printf("\n%f", RSSI);
        return 0;
    }
    It works now
    Thank you for all, I learned like 3-4 thing about proggamming today

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I guess there's no room for other solutions, but I want to say, even if you wanted to write this yourself for some reason, you could do better than what Tim showed. His solution is rather silly because it eschews even basic library functions, and unless that is required, it's wasteful of people's time and energy. Plus, there is no reason to use pow() for small powers.

    I assume the double variable is significant even though the number is only going to be so big?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    int main()
    {
        double RSSI;
        char c[13]="kttt0FDF";
        char *pc = c + strlen(c) - 4;
        long int v = 0;
        v += (isdigit(pc[0])? pc[0] - '0' : 10 + pc[0] - 'A') * 4096;
        v += (isdigit(pc[1])? pc[1] - '0' : 10 + pc[1] - 'A') * 256;
        v += (isdigit(pc[2])? pc[2] - '0' : 10 + pc[2] - 'A') * 16;
        v +=  isdigit(pc[3])? pc[3] - '0' : 10 + pc[3] - 'A';
        RSSI = v;
        printf("%g\n", RSSI);
        return 0;
    }
    Last edited by whiteflags; 06-25-2016 at 03:32 PM.

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Here is another solution for this problem:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main(void)
    {
        double RSSI;
        char c[13]="kttt0FDF";
        char *pc = c + strlen(c) - 4;
        long int v = 0;
        int i;
        for (i = 0 ; i < 4 ; i++) {
            v <<= 4;
            v |= isdigit(pc[i]) ? pc[i] - '0' : 10 + pc[i] - 'A';
        }
        RSSI = (double) v;
        printf("%g\n", RSSI);
        return 0;
    }
    Or this one:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    int main(void)
    {
        double RSSI;
        char c[13]="kttt0FDF";
        char *pc = c + strlen(c) - 4;
        long int v = 0;
        v = strtol (pc, NULL, 16);
        RSSI = (double) v;
        printf("%g\n", RSSI);
        return 0;
    }
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-29-2013, 11:05 PM
  2. help program c, converting numbers to digits, string.h
    By Joshua Rhee in forum C Programming
    Replies: 1
    Last Post: 02-19-2013, 04:19 PM
  3. Converting Digits to Words Help!
    By McZiploc in forum C Programming
    Replies: 6
    Last Post: 02-16-2013, 08:30 PM
  4. String manipulation and converting to a number
    By gregm in forum C Programming
    Replies: 7
    Last Post: 04-26-2011, 03:13 PM
  5. converting string to number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-12-2002, 06:11 AM

Tags for this Thread