Golang: convert rune to int


If you want to convert a rune into an int with go you can try to use int(rune), but doing it will not return the expected result. Since you want the integer value of the ASCII (or UTF-8) representation of the number you want to convert.

For example suppose you want to convert the rune with the value 56 to an int, doing int(rune) will return 56. Buy 56 is the code of the integer 8.

ASCII table for integers

To solve this problem you will need to substract 48 to 56 and you will get the 8. Why 48? Well 48 is the representation of the "0" in the ascii table and since all the integers are sequential in the ASCII representation, this will return the desired value.

int(r - '0') if(typeof __ez_fad_position != 'undefined'){__ez_fad_position('div-gpt-ad-tutorials_technology-medrectangle-3-0')};if(typeof __ez_fad_position != 'undefined'){__ez_fad_position('div-gpt-ad-tutorials_technology-medrectangle-3-0_1')};if(typeof __ez_fad_position != 'undefined'){__ez_fad_position('div-gpt-ad-tutorials_technology-medrectangle-3-0_2')}; .medrectangle-3-multi-114{border:none !important;display:block !important;float:none;line-height:0px;margin-bottom:2px !important;margin-left:0px !important;margin-right:0px !important;margin-top:2px !important;min-height:250px;min-width:300px;text-align:center !important;}