Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: int conversion from char

Reply
Thread Tools

Re: int conversion from char

 
 
Nitin
Guest
Posts: n/a
 
      07-16-2005
Alex Maceda wrote:
> In writing a function that take as it's arguments a string and the length
> of the string. I need to change the char input array to an integer array
> for the operation to succed. For some reason I can't get atoi, sscanf, or
> an extremly clumsy "while" loop to work. What is the standard method for
> doing this?
>
> Alex Maceda
> -----BEGIN PGP PUBLIC KEY BLOCK-----
> Version: GnuPG v1.4.1 (Darwin)


<snip>
>


Firstly, you cannot pass a string to a function , only the base address
is what really gets passed ( unless u wrap it in a struct object ).

Secondly, what do you mean by "convert to integer" ? chars in C are
really ints - corresponding to the numerical value of the character-set
on your platform. e.g. if your platform uses ASCII, a char '0' is
really stored as 65 !

If u intend to 'print' those numerical values instead of character
representation, use sprintf(intarray,"%d",chararray[i]);

For more clarification see documentation of getchar( ) , putchar( ).
You'll see their args r really 'int' and not 'char' !

HTH

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      07-16-2005
"Nitin" <(E-Mail Removed)> writes:
> Alex Maceda wrote:
>> In writing a function that take as it's arguments a string and the length
>> of the string. I need to change the char input array to an integer array
>> for the operation to succed. For some reason I can't get atoi, sscanf, or
>> an extremly clumsy "while" loop to work. What is the standard method for
>> doing this?

[...]
>
> Firstly, you cannot pass a string to a function , only the base address
> is what really gets passed ( unless u wrap it in a struct object ).


Please don't use abbreviations like 'u'; they only make it more
difficult to read your article.

> Secondly, what do you mean by "convert to integer" ? chars in C are
> really ints - corresponding to the numerical value of the character-set
> on your platform. e.g. if your platform uses ASCII, a char '0' is
> really stored as 65 !


It's important to distinguish between the terms "int" and "integer".
"int" refers to a specific type; "integer" is a more generic term that
covers signed and unsigned types including char, int, long long, and
so forth.

To the OP, it's difficult to tell just what you're tring to do. As
Nitin points out, a string is already an array of integers.

Are you trying to convert a string such as "123 234 345" to an array
of int with the values 123, 234, and 345? If so, show us what you've
tried so far, and maybe we can help. You should also define precisely
what you're trying to do. For example, how are the integers delimited
in the string? Single space? One or more spaces? Commas? Do you
want to allow '-' signs? '+' signs? Hexadecimal or octal formats?
How do you want to handle overflow? The size of the result array is
going to depend on the value of the string; how are you going to
allocate it?

I don't think there's necessarily a "standard way" to do this.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      07-20-2005
Nitin wrote:
> Alex Maceda wrote:
>
>>In writing a function that take as it's arguments a string and the length
>>of the string. I need to change the char input array to an integer array
>>for the operation to succed. For some reason I can't get atoi, sscanf, or
>>an extremly clumsy "while" loop to work. What is the standard method for
>>doing this?
>>
>>Alex Maceda
>>-----BEGIN PGP PUBLIC KEY BLOCK-----
>>Version: GnuPG v1.4.1 (Darwin)

>
>
> <snip>
>
>
> Firstly, you cannot pass a string to a function , only the base address
> is what really gets passed ( unless u wrap it in a struct object ).
>
> Secondly, what do you mean by "convert to integer" ? chars in C are
> really ints - corresponding to the numerical value of the character-set
> on your platform. e.g. if your platform uses ASCII, a char '0' is
> really stored as 65 !
>
> If u intend to 'print' those numerical values instead of character
> representation, use sprintf(intarray,"%d",chararray[i]);
>
> For more clarification see documentation of getchar( ) , putchar( ).
> You'll see their args r really 'int' and not 'char' !
>
> HTH
>

I think you'll find in ASCII, that '0' is 48. The value 65 is reserved
for 'A'. The decimal value and corresponding glyph of the 95 'printing'
ASCII characters follow.

32 33 ! 34 " 35 # 36 $ 37 % 38 & 39 '
40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 /
48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7
56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ?
64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G
72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O
80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W
88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _
96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g
104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o
112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w
120 x 121 y 122 z 123 { 124 | 125 } 126 ~

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int urldecode(char *src, char *last, char *dest) gert C Programming 20 02-16-2007 11:28 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments