Re: [go-nuts] "Effective Go" error

220 views
Skip to first unread message

chris dollin

unread,
May 23, 2013, 11:22:57 AM5/23/13
to Tony Hsu, golang-nuts
What's strange about it?

["it" being: x = x*10 + int(b[i])-'0'
]

Chris

--
Chris "allusive" Dollin

Jan Mercl

unread,
May 23, 2013, 11:30:33 AM5/23/13
to Tony Hsu, golang-nuts
On Thu, May 23, 2013 at 3:56 PM, Tony Hsu <gosha...@gmail.com> wrote:
> The first example in the "Function" section, there is a strange looking
> expression.
>
> x = x*10 + int(b[i])-'0'

Take the value of x. Multiply it by 10. Take the value of b at index
i. Convert it to integer. Add with the previous result. From the new
result subtract the unicode codepoint value of character '0', which is
0x30, which is 48 decadic. Put the new result into variable x.

-j

peterGo

unread,
May 23, 2013, 12:55:29 PM5/23/13
to golan...@googlegroups.com
Tony,

For ASCII character code (byte) digits 0 thru 9 (hexadecimal ASCII codes 0x30 though 0x39), we have

    binary(digit) = ascii(digit) - ascii(zero)

Rewrite the expression:


    x = x*10 + int(b[i])-'0'

    x = x*10 + int(b[i]-'0')

If x is int(12) and b[i] is byte('3') then, by assignment,

    x = int(12)*10+int(byte(0x33)-byte(0x30)) = int(123)

Peter

Tony Hsu

unread,
May 24, 2013, 7:02:08 AM5/24/13
to golan...@googlegroups.com
Thanks! The Go community is far more friendlier and helpful than I had expected.
I own a GAE service called "GoChild", but it's related to another meaning of Go:)
Reply all
Reply to author
Forward
0 new messages