New answers tagged

2

Rust macro, 121 bytes macro_rules!f{($a:expr;)=>{$a};($a:expr;$c:tt$(,$r:tt)*)=>{f!($a*10+$c as i64-48;$($r),*)};($($c:tt)*)=>{f!(0;$($c),*)};} Try it online! This is a macro that takes a sequence of characters and returns an expression like ((0 * 10 + '1' as i64 - 48) * 10 + '0' as i64 - 48) * 10 + '9' as i64 - 48. The expression $a is used as an ...


1

Haskell, 23 bytes foldl(\a x->10*a+x-48)0 Try it online! A simple fold.


2

SNOBOL4 (CSNOBOL4), 84 bytes I =INPUT N I LEN(1) . C REM . I :F(O) O =O * 10 + ORD(C) - 48 :(N) O OUTPUT =O END Try it online!


2

Whitespace, 88 bytes [S S S N _Push_0][N S S N _Create_Label_LOOP][S S S N _Push_0][S N S _Duplicate_top][T N T S _Read_STDIN_as_character][T T T _Retrieve][S N S _Duplicate][S S S T N _Push_1][T S S T _Subtract][N T S S N _If_0_Jump_to_Label_EXIT][S S S T T S S S S N _Push_48][T S S T _Subtract][S N T _Swap_top_two][S S S T S T S N ...


3

GolfScript, 13 12 bytes {48-}%10base Try it online! { }% # For each byte in the input 48- # Subtract 48 10base # Convert to base 10


2

Lua, 55 bytes i=0(...):gsub('.',load'i=i*10+(...):byte()-48')print(i) Try it online! Fairly simple: executes given algorithm for every character in given string, then prints the result. TIO includes test suite. Note: input is taken as an argument. This doesn't conform to question as currently written, but is an input method allowed by default I/O rules.


3

Clojure, 41 bytes #(reduce(fn[x y](+(* 10 x)-48(int y)))0%) Try it online! Or alternatively, for the same byte count with a different order of full and shorthand function notation: (fn[x](reduce #(+(* 10%)-48(int %2))0 x)) Try it online!


4

Brachylog, 13 bytes ạ-₄₈ᵐ{×₁₀ʰ+}ˡ Try it online! For single-"digit" inputs, the output is wrapped in a list, since the fold isn't actually executed. If this isn't acceptable, two 15-byte alternatives exist: ạ-₄₈ᵐ,0↻{×₁₀ʰ+}ˡ ạ↔-₄₈ᵐ{i×₁₀ⁱ⁾}ᶠ+ ạ Convert to codepoints. -₄₈ᵐ Subtract 48 from each. { }ˡ Left fold: ...


3

Assembly (NASM, 32-bit, Linux), 221 185 bytes Edit: -36 bytes + bugfix mov eax,0 mov esi,48 push 48 mov ecx,esp l:mov edx,10 mul edx mov edi,esi sub edi,48 add eax,edi mov si,[ecx] push eax mov dx,1 mov ebx,0 mov eax,3 int 128 or ax,0 pop eax jnz l pop edi Try it Online! The result is in eax.


6

JavaScript (ES6/2015), 51 bytes ([...s],r=0)=>s.map(c=>r=r*10+c.charCodeAt(0)-48)|r let f = ([...s],r=0)=>s.map(c=>r=r*10+c.charCodeAt(0)-48)|r console.log(f("123456789" )) // 123456789 console.log(f("0123456789")) // 123456789 console.log(f("Lorem" )) // 350191 console.log(f("Ipsum" )) // 321451 console.log(f("AAAAAAAA" )) // ...


3

><>, 15 bytes 0l1=?na*{68*-+! Try it Online! Explanation 0 ! Accumulator initialisation, skipped on subsequent loops l1=?na* Print-and-terminate condition a* Decimal shift { Next character of input 68*-+ Subtract 48 and add


7

MATL, 8 bytes As long as we're doing things wrong, why not turn a string into a polynomial? 48-[X]ZQ Try it online! Evaluates the polynomial where p is the input string (minus '0') and x is 10.


3

Ruby, 38 bytes ->s{s.bytes.inject(0){|a,b|b-48+a*10}} 35 bytes with the new Ruby 2.7 syntax: ->s{s.bytes.inject(0){_2-48+_1*10}} Try it online!


3

Rust, 42 bytes |s|s.chars().fold(0,|n,x|n*10+x as i32-48) Try it online! Takes input as an &str and outputs the number based on utf-8 codepoints.


3

Elixir, 36 bytes &List.foldl&1,0,fn x,y->y*10+x-48end Try it online! Anonymous function that performs a simple left fold over its argument (&1) using the specified accumulating function. Relies on the fact that Elixir charlists are the same thing as the lists of integer codepoints, so that the transformation can be applied directly without ...


4

Java 8, 46 bytes a->{int r=0;for(var c:a)r=r*10+c-48;return r;} Input as a character-array. Try it online. Explanation: a->{ // Method with character-array parameter and integer return-type int r=0; // Result-integer, starting at 0 for(var c:a) // Loop over the input-characters: r= // Change the result to: ...


4

05AB1E, 6 bytes Ç48-Tβ Similar as some other answers. Try it online or verify all test cases. Explanation: Ç # Convert the (implicit) input-string to a list of codepoint integers 48- # Subtract 48 from each value Tβ # Convert this from a base-10 list to a base-10 integer # (after which it is output implicitly as result)


3

Haskell, 45 43 bytes n#(h:t)=(n*10+fromEnum h-48)#t n#_=n f=(0#) Try it online! n#(h:t)= - # -> infix function taking a number and a string(head:tail) (n*10+fromEnum h-48)#t - compute first char and call recursively on tail n#_=n - end condition f=(0#) - apply 0 to #


8

Poetic, 153 bytes decoding a num got WRONG I/O digits i get?converting fails,o darn i am bummed if the I/O fails i have a number i got based on counting it badly,i suppose Try it online!


4

K (ngn/k), 7 bytes 10/-48+ Try it online!


4

brainfuck, 43 38 bytes ,[<[>++++++++++<-]>+++>+[<->+++++],]<. Try it online! Thanks to JosiahRyanW for -5 bytes. Since by our consensus, output can be given as character code, this is pretty straightforward in brainfuck. Note that the brainfuck interpreter on tio.run has 8-bit wrapping cells, so the maximum value is 255. ,[ ...


4

Python 3, 46 45 42 bytes f=lambda s:s>[]and ord(s.pop())-48+10*f(s) Try it online! -1 thanks to Sisyphus -3 taking a list of characters as input thanks to ovs


3

Gol><>, 10 bytes iEh$a*+`0- Try it online! i Read one char from the input Eh If last char read was EOF halt and output current sum as number $a* Else swap new input and current sum, then multiply sum by 10 + Add the new char value to current sum `0- Subtract 48 (value of '0') and wrap around implicitly ...


10

Shakespeare Programming Language, 258 252 bytes -6 bytes thanks to the default. ,.Ajax,.Puck,.Act I:.Scene I:.[Exeunt][Enter Ajax and Puck] Ajax:Open mind. Puck:Is I nicer zero?If soYou is the sum ofI twice the difference betweenthe product ofyou the sum ofa cat a big big cat the factorial ofa big big cat. If soLet usAct I.Open heart Try it ...


2

Vyxal, 5 bytes C48-I Wow it's actually ASCII.


2

Keg, -hr, 15 bytes 0&?⑷86*-⑸( ⑾⑼)⑻ Try it online! Keg is very special. Like very special.


4

Python 3, 53 bytes def f(x): o=0 for i in x:o=o*10+ord(i)-48 return o Try it online!


3

J, 12 bytes 10#._48+3&u: Try it online!


5

PowerShell Core, 35 bytes $args[0]|% t*y|%{$r=$r*10+$_-48};$r Try it online! Takes input as a string


2

C (gcc), 48 44 bytes Usually using a temporary variable to store the current character is shorter than doing array processing, but not this time! i;f(char*s){for(i=0;*s;i=i*10+*s++-48);s=i;} Try it online!


2

Charcoal, 10 bytes I↨ES⁻℅ι⁴⁸χ Try it online! Link is to verbose version of code. Explanation: S Input string E Map over characters ι Current character ℅ Ordinal ⁴⁸ Literal 48 ⁻ Subtract χ Literal 10 ↨ Base conversion I Cast to string Implicitly print


4

JavaScript (Node.js),  39  38 bytes Saved 1 byte thanks to @Shaggy s=>Buffer(s).map(c=>p=p*10+c-48,p=0)|p Try it online!


2

mlochbaum/BQN, 21 bytes 10{+⟜(𝕨⊸×)´⌽𝕩}'0'-˜¨ Try it! The online BQN REPL does not have a method of taking input(⍞ in APL), so append the string to the end of the program.


5

Scala, 18 bytes _./:(0)(_*10+_-48) Try it online! Explanation: _./:(0)(_*10+_-48) /: Fold left _ the input string (0) with an initial value of 0 (_*10+_-48) Multiply the accumulator by 10, and add the next character - 48


4

Befunge-98 (PyFunge), 14 12 bytes Thanks to Gegell for -2 bytes! 2j@.~'0-\a*+ Try it online! Explanation: Initially the instruction pointer (IP) is moving right and the stack is filled with 0's. 2j skips the next two instruction (@.). ~ reads one character of input. '0- subtracts 48 from the input. \ swaps the two elements at the top of the stack. Now the ...


4

Perl 5, 40 bytes sub{my$r;map$r=$r.0-48+ord,pop=~/./g;$r} Try it online!


5

APL (Dyalog Unicode), 11 bytes 10⊥48-⍨⎕UCS Try it online! ⎕UCS converts string to a vector of ASCII values, 48-⍨ subtracts 48 from each value and 10⊥ converts from base 10.


3

Husk, 9 7 bytes Edit: -2 bytes thanks to Razetime dmo-48c Try it online! How? d # interpret digits as base-10 number: m # map function over each element of list (each character of string) o # combine 2 functions into one c # convert character to Utf8 value -48 # subtract 48


4

Stax, 7 bytes ëk╜ΓOíU Run and debug it Explanation: {48-mA|E Unpacked source, implicit input { m Map over code points of input string 48- Subtract 48 from each A|E Convert from base 10


2

Japt, 8 bytes Takes input as an array of characters. £48nXcÃì Try it (Header splits string input to array)


10

Jelly, 5 bytes O_48Ḍ Try it online! How it works O_48Ḍ - Main link. Takes a string s on the left O - Convert to ordinal code points _48 - Subtract 48 from each Ḍ - Convert from base-10


5

R, 51 48 bytes Edit: -3 bytes thanks to Robin Ryder function(s)(utf8ToInt(s)-48)%*%10^(nchar(s):1-1) Try it online! Multiplies Utf8 value of each character minus 48 by powers-of-ten from the length of the string minus one, down to zero, and outputs the sum.


Top 50 recent answers are included