The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare array contents with file orahi001 UNIX for Dummies Questions & Answers 0 03-25-2008 01:44 PM
A final question! Compare character with each array element rorey_breaker Shell Programming and Scripting 1 09-28-2007 06:26 AM
Multidimension character array janemary.a High Level Programming 1 05-15-2007 03:50 AM
Can I assign the contents of file into an array? murtaza Shell Programming and Scripting 10 03-28-2007 12:58 AM
converting character string to hex string axes High Level Programming 5 09-20-2006 10:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-19-2006
Registered User
 

Join Date: Oct 2001
Location: Bangalore
Posts: 93
Stumble this Post!
converting contents of a character array to int

Hi,

I have character array and i need to convert the content to int.

program snipet:

char array[10] = {"1","2","3","4","5","6","7","8","9"}.

I need to to extract each of these array fields and store them into an integer variable.

Casting the contents to (int), gives the ascii value of the array fields but not the actual value.

atoi function doesnt work here as it expects a const char*.

Any help would be appreciated.

Jyoti
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-19-2006
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,513
Stumble this Post!
A snippet to get you started:

Code:

    char a = '0';
    switch (a)
    {
    case '0': 
        value = 0;
        break;
    default: 
       /* non-numeric value */
    }
Reply With Quote
  #3 (permalink)  
Old 07-19-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,812
Stumble this Post!
char datatype is treated as a limited range int (-127 to 127) by the compiler.
Code:
int i=0;
int irray[10]={0};
char array[10] = {'0','1','2','3','4','5','6','7','8','9'};  <- use ' not "
for(i=0;i<10;i++)
    irray[i]=array[i];
Reply With Quote
  #4 (permalink)  
Old 07-19-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,430
Stumble this Post!
Assuming you use the correct syntax: char array[10] = {'1','2','3','4','5','6','7','8','9'};
There is a relationship between the index of the array element and the value you need:
k=3;
chr=array[k];
ival=k+1;
Now chr would be '4' and ival would be 4. There is another trick that I've used from time to time:
ival = (int)chr - (int)'0';
It assumes ascii or some other character set that behaves this way. But I have used it anyway.
Reply With Quote
  #5 (permalink)  
Old 07-19-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Stumble this Post!
jim, i tried the code you've posted, its printing out the ascii values of the numbers (48, 49, and so on).

Won't this be as simple?
Code:
#include<stdio.h>

int main(void) {
        char a[]={'0','1','2','3','4','5','6','7','8','9'};
        int b[10];
        int i;
        for(i=0;i<10;i++) {
                b[i]=(int)(a[i]-48);
        }
}
I think that's what Perderabo has done...
Reply With Quote
  #6 (permalink)  
Old 07-19-2006
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,513
Stumble this Post!
It is indeed, he just didn't hardcode the value for '0', so any character set where the numbers ascend sequential will work.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0