Re: int conversion from char

Discussion in 'C Programming' started by Nitin, Jul 16, 2005.

  1. Nitin

    Nitin Guest

    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);

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

    HTH
    Nitin, Jul 16, 2005
    #1
    1. Advertising

  2. "Nitin" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 16, 2005
    #2
    1. Advertising

  3. Nitin

    Joe Wright Guest

    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);
    >
    > 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 ---
    Joe Wright, Jul 20, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    403
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,626
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,036
    Ian Collins
    May 9, 2006
  5. gert
    Replies:
    20
    Views:
    1,155
Loading...

Share This Page