mark.warren
2018-11-15 14:42:14

Hi, I’m looking at converting numeric characters to integers i.e. #\1 to 1 (not 49 which is the character code) I have not seen a standard function for this so I have tried 2 alternative appraches to this (define (char->int c) (- (char->integer c) 48)) and (define (char->int c) (string->number (~a c))) is one of these methods the ‘preferred’ way, or is there another way?


jerome.martin.dev
2018-11-15 14:43:09

@mark.warren (string->number) I think


mark.warren
2018-11-15 14:43:24

Does that work on single characters


jerome.martin.dev
2018-11-15 14:43:46

I don’t think so, but you can make a string out of one character


mark.warren
2018-11-15 14:44:19

That’s what (~a c) does


jerome.martin.dev
2018-11-15 14:44:36

(string->number (string #\1))


mark.warren
2018-11-15 14:45:47

Yeah, that’s what my second approach does, but using ~a instead of string


jerome.martin.dev
2018-11-15 14:47:07

Yes. It’s not that frequent to need exactly one character as an integer, so I guess any of those approaches are good. The only thing would be about performance if it goes into a really tight loop or something.


samth
2018-11-15 14:47:31

@mark.warren the first one is the way it’s usually done


mark.warren
2018-11-15 14:49:13

@samth Thanks, I’m just doing a checksum test on a numeric string, the check it’s very basic but it needs you to multiply each digit of the number by a weight and then sum them.


jerome.martin.dev
2018-11-15 14:49:45

I knew it :smile:


jerome.martin.dev
2018-11-15 14:50:14

I was trying to find some usecase, and this is exactly what came to my mind first :stuck_out_tongue:


mark.warren
2018-11-15 14:50:28

@jerome.martin.dev Hehe


jerome.martin.dev
2018-11-15 14:51:22

I think you can also do that with a (for) loop … something like (in-char)


jerome.martin.dev
2018-11-15 14:52:53

nope, (in-string)


mark.warren
2018-11-15 14:53:16

Yeah, that’s how I’m doing it, I do a string->list on the string which gives me the characters, which I then needed to convert to integer values.


mark.warren
2018-11-15 14:57:05

(define (char->int c) (- (char->integer c) 48)) (define (check-remainder check-number) (modulo (for/sum ([weight (range 10 1 -1)] [digit (string->list check-number)]) (* weight (char->int digit))) 11))


jerome.martin.dev
2018-11-15 14:57:28

what I mean is that you can probably make a one-liner: (for/sum ([c (in-string "1234")]) (- (char->integer c) 48))


jerome.martin.dev
2018-11-15 14:57:38

oh yeah, right


jerome.martin.dev
2018-11-15 14:57:42

you used that :stuck_out_tongue:


jerome.martin.dev
2018-11-15 14:58:12

nevermind my comment then, I was too slow!


jerome.martin.dev
2018-11-15 14:59:07

so yeah the only difference is that I use (in-string) instead of (string->list) which has an optimization in the for loop


mark.warren
2018-11-15 15:06:59

Thanks for the help


mark.warren
2018-11-15 15:08:50

@jerome.martin.dev I’ll give the in-string way a go


shu--hung
2018-11-15 16:21:43

@mark.warren if the indended use is for byte streams, how about using bytes directly?


mark.warren
2018-11-16 07:26:11

@shu—hung It is an input from a user, so it’s a string, I could convert it to a byte string.