# [racket-users] Re: How to convert String to Integer

```In C I would would do some very simple pointer arithmetic, but racket
leaves me into the blue.
Documentation, which is fine, compared to other lisps, fyi chez, leaves me
into the blue.
0) Given a string,
1) convert to  a list of characters,
2) allow me to iterate,
3) convert a character to an int ,
4) subtract corresponding value of zero,
5) allow me to some positional stuff and addition.
But first i need a list of characters. :)
Does the language has a conversion operator ?```
```
On Tuesday, February 11, 2020 at 8:50:33 PM UTC+1, Alain De Vos wrote:
>
> Very basic question, first step first, how do i convert a string astring
> to a list of characters,
> #lang typed/racket
> (require typed/racket/gui)
> (: astring String)
> (define astring "1234567890")
>
>
> On Tuesday, February 11, 2020 at 11:34:16 AM UTC+1, Alain De Vos wrote:
>>
>> I tried the following function to conver a String to an Integer.
>>
>> #lang typed/racket
>> (: myconversion (-> String Integer))
>> (define (myconversion str)
>>   (string->number str))
>>
>> The error given is :
>> Type Checker: type mismatch
>>   expected: Integer
>>   given: (U Complex False) in: (string->number str)
>>
>> I guess this is because a number is not an Integer.
>>
>> How to proceed?
>>
>> I found the following code on internet , but this looks like a real
>> overkill for this simple problem ,
>>
>> (: numerical-char->integer (-> Char
>>                                Integer))(define (numerical-char->integer
>> char)
>>   (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
>>     (if
>>      (or (< num 0) (> num 9))
>>      (raise 'non-numerical-char #t)
>>      num)))
>> (: string->integer (-> String
>>                        Integer))(define (string->integer str)
>>   (let ([char-list (string->list str)])
>>     (if (null? char-list)
>>         (raise 'empty-string #t)
>>         (foldl
>>          (λ([x : Integer] [y : Integer])
>>            (+ (* y 10) x))
>>          0
>>          (map numerical-char->integer char-list)))))
>>
>>

--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email