Computing.Net > Forums > Programming > c: convert char a[] to int b[]

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

c: convert char a[] to int b[]

Reply to Message Icon

Name: nadeem
Date: November 25, 2002 at 07:42:13 Pacific
OS: HP-UX
CPU/Ram: HP-UX
Comment:

here's my C coding problem:

how does one convert a char array to an int array?

for example, say if one has the pattern = "3 3 3 3 3 4" stored as chars in an array (with spaces)
how can i convert this to an int array like int_pattern = [3, 3, 3, 3, 3, 4]

initial declarations are:

char sz_pattern[max]
int sz_pattern[max]

ive already tried:

sz_pattern[2*n_belt+1] = NULL;

for (i=0; i < (2*n_belt+1); ++i)
{
sz_pattern_int[i] = atoi(sz_pattern[i]);
}

result: nothing but zeroes stored in int array

and also:

for (i=0; i < max; ++i)
{
*((int *)sz_pattern_int[i]) = *((char *)sz_pattern[i])
}

result: core dump.



Sponsored Link
Ads by Google

Response Number 1
Name: JDNeal
Date: November 25, 2002 at 08:39:17 Pacific
Reply:


1. "max" can't be the same thing for both the integer array and char array - the character array will be TWICE as big as the integer array (one character and one space for each integer!).

2. The incrementer in the loop cannot be the same for both. Again, you are hitting every other character in the character array - thus need a different count.

***********************************

int stringoffset;
char *intermediate;

for (i = 0; i < max; i++)
{
stringoffset = i * 2;

intermediate = &sz_pattern_char[stringoffset];

sz_pattern_int[i] = atoi(intermediate);
}

Is how I worked it.


0

Response Number 2
Name: SN
Date: November 25, 2002 at 08:41:13 Pacific
Reply:

I'm no C buff, but in Java I'd do something like


int intPattern[max/2+1];
char charPattern[max];
for (int i=0; i(less than)intPattern.length; i++)
{
if charPattern[i]!=' ';
intPattern[i]==(int)charPattern[i];
}



0

Response Number 3
Name: nadeem
Date: November 25, 2002 at 09:23:06 Pacific
Reply:

god, i was stupid! been at this all morning.

thanks for the help! JDNEAL...! it worked.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


MAC address delphi Trap an int!



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: c: convert char a[] to int b[]

re: converting char array into int www.computing.net/answers/programming/re-converting-char-array-into-int/11348.html

copy files from folder A to folder B .... www.computing.net/answers/programming/copy-files-from-folder-a-to-folder-b-/20177.html

C++ programming www.computing.net/answers/programming/c-programming/13713.html