Shiva
Shiva

Reputation: 551

convert particular array elements as integer value

I have declared an array char Buffer[100]="01 05 01 4A 63 41"; now the array looks like this

Buffer[0]='0'
Buffer[1]='1'
Buffer[2]=' '
Buffer[3]='0'
Buffer[4]='5'

i just want to convert these value to int `eg.:

atoi()cannot be used since it converts all the Buffer value as integer.

How to convert a particular space delimited value sub-string to an integer?

Upvotes: 1

Views: 147

Answers (5)

Some programmer dude
Some programmer dude

Reputation: 409176

You can treat Buffer as a string (which it is), and use e.g. strtok to "tokenize" the numbers on space boundary. Then use strtol to convert each "token" to a number.

But do note that strtok modifies the string, so if you don't want that you have to make a copy of the original Buffer and work on that copy.

Also note that as the numbers seems to be hexadecimal you can't use atoi because that function only parses decimal numbers. You have to use strtol which can handle any base from 2 to 36.

Upvotes: 1

Clifford
Clifford

Reputation: 93476

Consider this:

#include <stdio.h>

int main()
{
    char Buffer[100] = "01 05 01 4A 63 41" ;
    const char* h = &Buffer[0] ;
    int i ;

    while( *h != 0 )
    {
        if( sscanf( h, "%2x", &i  ) == 1 )
        {
            printf( "0x%02X (%d)\n", i, i ) ;
        }

        h += 3 ;
    }

   return 0;
}

The output from which is:

0x01 (1)
0x05 (5)
0x01 (1)
0x4A (74)
0x63 (99)
0x41 (65)

I have assumed that all the values are hexadecimal, all two digits, and all separated by a single space (or rather a single non-hex-difgit character), and that the array is nul terminated. If either of these conditions are not true, the code will need modification. For example if the values may be variable length, then the format specifiers need changing, and, you should increment h until a space or nul is found, and if a space is found, increment once more.

You could write similar code using strtol() instead of sscanf() for conversion, but atoi() is specific to decimal strings, so could not be used.

If you are uncomfortable with the pointer arithmetic, then by array indexing the equivalent is:

#include <stdio.h>

int main()
{
    char Buffer[100] = "01 05 01 4A 63 41" ;
    int c = 0 ;
    int i ;

    while( *h != 0 )
    {
        if( sscanf( &Buffer[c], "%2x", &i  ) == 1 )
        {
            printf( "0x%02X (%d)\n", i, i ) ;
        }

        c += 3 ;
    }

   return 0;
}

and the strtol() version if you prefer:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char Buffer[100] = "01 05 01 4A 63 41" ;
    const char* h = &Buffer[0] ;

    while( *h != 0 )
    {
        int i = strtol( h, 0, 16 ) ; 
        printf( "0x%02X (%d)\n", i, i ) ;

        h += 3 ;
    }

   return 0;
}

Upvotes: 0

Jerry
Jerry

Reputation: 4408

You can cast Buffer[i] to int. Then check its value, which will be in ASCII. 48->0 . . 57->9

You can even compare the char to its ASCII value without casting

int CharToDigit(char c)
{
    if(c>=48 && c<=57) // or as suggested if(c>='0' && c <='9')
        return (int)c - 48; // subtract the ascii of 0
    return -1; // not digit
}

For digits from A to F you'll have to subtract 55 from uppercase letters (65-10, 65 is ascii of A)

Then loop through the chars in Buffer sending them to the function: CharToDigit(Buffer[i]) and check the returned int.

Upvotes: 0

Igor Popov
Igor Popov

Reputation: 2620

My first solution works only for integers, and the following one works also for hexadecimal numbers. I wrote down the function which converts string representation of a hexadec. number into a decimal number. Then, as suggested by Jochim Pileborg, I used strtok to parse the given Buffer array.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int hexToInt(char *tok)
{
  int i,out=0, tens=1, digit;
  for(i=strlen(tok)-1; i>=0; i--)
  {
    switch(tok[i])
    {
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9': digit=tok[i]-'0';
                break;
      case 'A': digit=10; break;
      case 'B': digit=11; break;
      case 'C': digit=12; break;
      case 'D': digit=13; break;
      case 'E': digit=14; break;
      case 'F': digit=15; break;
    }
    out+=digit*tens;
    tens*=16;
  }
//  printf("hex:%s  int:%d ", tok, out);
  return out;
}

int main()
{
  char Buffer[100]="01 2A 10 15 20 25";
  int intarr[100],current=0;
  char *tok=malloc(20*sizeof(char));
  tok=strtok(Buffer," ");
  while(tok!=NULL)
  {
    intarr[current]=hexToInt(tok);
    current++;
    tok=strtok(NULL," ");
  }
  printf("\n");
}

Upvotes: 1

Igor Popov
Igor Popov

Reputation: 2620

You can use sscanf for this, e.g.:

int intarr[6];
sscanf(Buffer,"%d %d %d %d %d %d",&intarr[0],&intarr[1],&intarr[2],&intarr[3],&intarr[4],&intarr[5]);

Upvotes: 0

Related Questions