char to int?

552 views
Skip to first unread message

ggggg

unread,
Nov 26, 2013, 8:32:23 PM11/26/13
to julia...@googlegroups.com
julia> parseint("4")
4

julia> parseint('4')
no method parseint(Char,)

julia> int('4')
52

So how do I turn '4' into 4? I feel like parseint should work, but maybe I'm just missing something.

ggggg

unread,
Nov 26, 2013, 8:35:47 PM11/26/13
to julia...@googlegroups.com
julia> a = '4'
'4'

julia> parseint("$a")
4

This works. Is this a good way?

Jacob Quinn

unread,
Nov 26, 2013, 8:36:36 PM11/26/13
to julia...@googlegroups.com
In  [2]: parseint(string('4'))

Out [2]: 4

Though I remember being a little baffled by this at first as well. I think my gotcha was trying to iterate over a string of numbers and each one was returning a Char, so trying to do any numeric operations was totally whack. I think the important thing here is to recognize that Char is is it's own type, with distinct behaviors from a String or Int (as opposed to just being a String subset).

-Jacob

Kevin Squire

unread,
Nov 26, 2013, 8:54:00 PM11/26/13
to julia...@googlegroups.com
Here's another solution (which only works if you know your characters are numbers):


julia> a = '4'
'4'

julia> a-'0'
4

Where are your chars coming from in the first place?

Kevin

Stefan Karpinski

unread,
Nov 26, 2013, 8:58:17 PM11/26/13
to julia...@googlegroups.com
Maybe parseint(::Char) should be the common locus for subtracting '0' and 'a' or 'A' or whatever. We have that logic repeated in a number of places around the system.

ggggg

unread,
Nov 26, 2013, 9:56:14 PM11/26/13
to julia...@googlegroups.com, quinn....@gmail.com
I managed to wrap my head around char/string being different, which was a change from python. But I tried

julia> a = '4'
'4'

julia> String(a)
type cannot be constructed

Didn't think to try string. Thanks for the answers.  Kevin the numbers are coming from a string I'm parsing that represents a sudoku. I'm implementing a solver like Norvig's but using IntSets for fun.

I think I saw some discussion about how int and Int are confusing, and relics of when DataType wasn't a thing.  It seems that string and String are similar.

Milan Bouchet-Valat

unread,
Nov 27, 2013, 3:40:53 AM11/27/13
to julia...@googlegroups.com
Le mardi 26 novembre 2013 à 20:58 -0500, Stefan Karpinski a écrit :
Maybe parseint(::Char) should be the common locus for subtracting '0' and 'a' or 'A' or whatever. We have that logic repeated in a number of places around the system.
What do you mean?

I was going to suggest that parseint(::Char) should behave just like parseint(::String), and that int(::Char) should keep its current behavior. Reading the docs for parseint() and int(), it seems that the latter is intended mainly to convert between numbers, which seems consistent with the idea of getting the character code as an integer. (Though in this scenario, int(::String) should maybe not exist, to reduce confusion. Not sure.)


My two cents

Stefan Karpinski

unread,
Nov 27, 2013, 4:01:45 AM11/27/13
to Julia Users
I mean this:

parseint(c::Char) =
    '0' <= c <= '9' ? c-'0' :
    'a' <= c <= 'z' ? c-'a'+10 :
    'A' <= c <= 'Z' ? c-'A'+10 :
        error("invalid digit: $(repr(c))")

It's the only sensible definition if we're going to give this a meaning and it kind of makes sense to factor this out since it's used in a number of places.

Jacob Quinn

unread,
Nov 27, 2013, 8:02:58 AM11/27/13
to julia...@googlegroups.com

+1 to that definition Stefan. I think that's exactly what people would expect calling parseint on a Char.

-Jacob

Stefan Karpinski

unread,
Nov 27, 2013, 1:00:09 PM11/27/13
to Julia Users

Milan Bouchet-Valat

unread,
Nov 27, 2013, 1:18:58 PM11/27/13
to julia...@googlegroups.com
Le mercredi 27 novembre 2013 à 04:01 -0500, Stefan Karpinski a écrit :
> I mean this:
>
>
> parseint(c::Char) =
> '0' <= c <= '9' ? c-'0' :
> 'a' <= c <= 'z' ? c-'a'+10 :
> 'A' <= c <= 'Z' ? c-'A'+10 :
> error("invalid digit: $(repr(c))")
>
>
> It's the only sensible definition if we're going to give this a
> meaning and it kind of makes sense to factor this out since it's used
> in a number of places.
Ah, indeed, I hadn't thought about hexadecimal notation.

Reply all
Reply to author
Forward
0 new messages