converting contents of a character array to int


 
Thread Tools Search this Thread
Top Forums Programming converting contents of a character array to int
# 1  
Old 07-19-2006
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
# 2  
Old 07-19-2006
A snippet to get you started:

Code:

    char a = '0';
    switch (a)
    {
    case '0': 
        value = 0;
        break;
    default: 
       /* non-numeric value */
    }

# 3  
Old 07-19-2006
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];

# 4  
Old 07-19-2006
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.
# 5  
Old 07-19-2006
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...
# 6  
Old 07-19-2006
It is indeed, he just didn't hardcode the value for '0', so any character set where the numbers ascend sequential will work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Looping through the contents of array for output name

Hi all, I am trying to loop through the string contents of an array, to add it during the saving of the output files. I am trying this code to print each column and save it to unique file name, but it doesn't work. Thanks for any help. fnam=(japan usa uk) alldata.dat contained sample data... (1 Reply)
Discussion started by: ida1215
1 Replies

2. Programming

Character pointer to Character array

how to copy content of character pointer to character array in c programming.. char *num; char name=num; (1 Reply)
Discussion started by: zinat
1 Replies

3. Shell Programming and Scripting

Display array contents on a new line

ksh eg arrayname=(1 2 3 4 5) I'm trying to display the individual contents of an array on a new line without using a loop, using one line of code. output 1 2 3 4 5 (3 Replies)
Discussion started by: squrcles
3 Replies

4. Shell Programming and Scripting

Grab the contents with in special character

I have a file which contains below kind of lines 2013-05-21 00:00:03 INFO moved to unprocessed, as doesn't exist in masklist and modified at 2013-05-20@21:21:21.000000000. 2013-05-21 00:00:03 INFO moved to unprocessed, as doesn't exist in masklist and modified at... (1 Reply)
Discussion started by: manas_ranjan
1 Replies

5. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

6. UNIX for Advanced & Expert Users

Converting byte array in c?

My project is based on GSM. Some parameters are represented as bytearray.Can any one tell me wats is the equivalent for it in c (1 Reply)
Discussion started by: carolsanjeevi
1 Replies

7. Shell Programming and Scripting

Shell script for converting file contents into CSV

Hi, I am new in unix, I just want to replace some values from text file according to column numbers. Like, I am having a table as given below: val1 val2 val3 val4 val5 val6 val7 val8 val9 val10 val11 val12 val13 Now i want... (5 Replies)
Discussion started by: rish_max
5 Replies

8. UNIX for Dummies Questions & Answers

Display the contents of an array

Hi i have just registered So i am at university studying forensic computing and we have to learn c++ i have never done anything with c++ before and i am abit stuck i need to create a programme to display the contents of an array of characters forwards and in reverse Can anyone help me... (1 Reply)
Discussion started by: RossMc
1 Replies

9. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

10. Shell Programming and Scripting

Can I assign the contents of file into an array?

I am trying to assign the contents of file e.g ls "$HOME" into an array. If it is possible then please guide me without using the concept of awk,sed, and perl16 Thanks (10 Replies)
Discussion started by: murtaza
10 Replies
Login or Register to Ask a Question