Skip to content

Mustafa Ateş UZUN Blog

mustafauzun.co

  • About
  • Blog
  • Home
  • »
  • C#
  • »
  • How to convert a string value to a number
  • »
    • September 14, 2022September 15, 2022
    • by mustafauzun
  • C#

How to convert a string value to a number

Every programming language has the function of converting string to number.

But do you know how this is done?

Actually, it’s a very simple process.

For example to the “12” string

In a loop, first, get elements in string convert to ascii code, second, ascii code – 48 (because, code 48 represents 0 in ascii table), after then result, multiplied by 10 and assigned to variable.

For that case;

10 * variable + (49 - 48)
10 * 0 (first) + 1
1

And here is another example, write with C#

(Source: My Gist)

static int intParse(string value)
{
   int newVal = 0;
   foreach (char letter in value)
   {
       newVal = 10 * newVal + (letter - 48);
   }
   return newVal;
}
Tags: convert int, convert number, string to number
Share

C#, turn the DataTable to string based calculator

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Javascript, Task Queue
  • Javascript, Thread Pool
  • Javascript, Lexical Environment
  • Javascript, Execution Context
  • Javascript, Event Delegation

Recent Comments

No comments to show.

Archives

  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • January 2022

Categories

  • C#
  • CMD
  • CSS
  • HTML
  • JavaScript
  • MongoDB
  • Node.js
  • Podcast
  • Powershell
  • Uncategorized
Theme: Puskar by Template Sell.