Base58Check encoding: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 18:
{{trans|Python}}
<
F convertToBase58(=num)
Line 35:
L(num) hash_arr
b = convertToBase58(BigInt(num, radix' 16))
print(‘0x#<54 -> #.’.format(num, b))</
{{out}}
Line 53:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Collections.Generic;
using System.Numerics;
Line 123:
}
}
}</
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 137:
=={{header|D}}==
<
import std.stdio;
Line 185:
return sb.data.idup;
}
}</
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 200:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<
' compile with: fbc -s console
' uses GMP
Line 259:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>OkobppXBkab(58) --> RosettaCode
Line 276:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 338:
fmt.Printf("%-56s -> %s\n", hash, b58)
}
}</
{{out}}
Line 356:
=={{header|Groovy}}==
{{trans|Java}}
<
private static final String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
private static final BigInteger BIG0 = BigInteger.ZERO
Line 404:
}
}
}</
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 418:
=={{header|Haskell}}==
<
chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Line 436:
0x572e4794,
0xecac89cad93923c02321,
0x10c8511e]</
{{out}}
<pre>
Line 453:
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists:
{{Trans|Python}}
<
import Numeric (showHex, showIntAtBase)
Line 524:
<*> ((" -> " <>) . fxShow . f)
)
xs</
{{Out}}
<pre>Base 58 encoding:
Line 542:
{{trans|Kotlin}}
{{works with|Java|9}}
<
import java.util.List;
Line 593:
}
}
}</
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 609:
{{works with|Julia|0.6}}
<
function encodebase58(hsh::AbstractString, base::Integer=16)
Line 628:
"0x10c8511e"]
println("# $s\n -> ", encodebase58(s))
end</
{{out}}
Line 653:
=={{header|Kotlin}}==
<
import java.math.BigInteger
Line 692:
println("${hash.padEnd(56)} -> $b58")
}
}</
{{out}}
Line 711:
{{libheader|bignum}}
This version takes in account the leading zeroes in hexadecimal representation. It accepts also arrays or sequences of bytes as input, taking in account the leading zero bytes as described in https://en.bitcoin.it/wiki/Base58Check_encoding#Base58_symbol_chart
<
import bignum
Line 766:
for h in Hashes:
echo h.alignLeft(MaxLength), " → ", h.toBase58</
{{out}}
Line 781:
=={{header|Perl}}==
<
sub encode_base58 {
Line 813:
0xecac89cad93923c02321
0x10c8511e
);</
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 829:
Slight variation from [[Bitcoin/public_point_to_address#Phix]] in that it accepts any length string (which can be binary or text).<br>
Includes leading zeroes, if you don't want that just comment out the three lines defining/using the integer lz.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">b58</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"</span>
Line 863:
<span style="color: #0000FF;">?</span><span style="color: #000000;">base58</span><span style="color: #0000FF;">(</span><span style="color: #008000;">x"ecac89cad93923c02321"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">base58</span><span style="color: #0000FF;">(</span><span style="color: #008000;">x"10c8511e"</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 879:
=={{header|Picat}}==
<
Tests = [[25420294593250030202636073700053352635053786165627414518,"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"],
["0x61","2g"],
Line 904:
X := X // 58,
Out := Out ++ [CodeS[Rem+1]]
end.</
{{out}}
Line 920:
=={{header|PicoLisp}}==
<
(chop "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") )
(de b58 (S)
Line 934:
(println (b58 "25420294593250030202636073700053352635053786165627414518"))
(println (b58 "0x626262"))
(println (b58 "0x636363"))</
{{out}}
<pre>
Line 946:
===Works with Python 3.9
{{trans|C#}}
<
def convertToBase58(num):
Line 963:
for num in hash_arr:
b = convertToBase58(num)
print("0x%-54x -> %s" % (num, b))</
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 978:
===Composition of pure functions===
{{Works with|Python|3.7}}
<
from functools import reduce
Line 1,138:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Base 58 check encoding:
Line 1,155:
=={{header|Quackery}}==
<
$ "123456789ABCDEFGHJKLMNPQRSTUV"
Line 1,181:
witheach [ base58$ echo$ cr ]
</syntaxhighlight>
{{out}}
Line 1,200:
=={{header|Racket}}==
(Examples from some other task)
<
(define ((base-n-alphabet-encode alphabet) hash-string (in-base 16))
Line 1,226:
"0x572e4794"
"0xecac89cad93923c02321"
"0x10c8511e")))</
{{out}}
<pre>"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
Line 1,233:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang=raku
constant @codes = <
1 2 3 4 5 6 7 8 9
Line 1,259:
is encode_Base58(.key), .value, "{.key} encodes to {.value}";
}
</syntaxhighlight>
=={{header|REXX}}==
Line 1,267:
I get the result expected there.
Apart for the leading 1 the program works also for the inputs shown above.
<
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Numeric Digits 100
Line 1,279:
End
o=o||substr(s,1,1)
Say reverse(o)</
{{out}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
===version 2===
does what the others do
<
s="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Numeric Digits 1000
Line 1,319:
Say 'found :' o
End
Return</
{{out}}
<pre>expected: check_error_handlimng
Line 1,331:
The algorithm used doesn't need to ''reverse'' the residual string (it uses ''prepend'' instead of ''append'').
<
/* 0─────────────────I─────O────────────────────l──────────────── ◄───omit.*/
@= space(" 123456789ABCDEFGH JKLMN PQRSTUVWXYZabcdefghi jkmnopqrstuvwxyz", 0)
Line 1,351:
return right(oz, 60) "───►" left('', L1, 0)$ /*for showing arg and the residual. */
</syntaxhighlight>
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,367:
=={{header|Ruby}}==
<
nums = [25420294593250030202636073700053352635053786165627414518,
0x61,
Line 1,380:
puts nums.map{|n| n.digits(58).reverse.map{|i| ALPHABET[i]}.join}
</syntaxhighlight>
{{out}}
<pre>6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 1,396:
=={{header|Scala}}==
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/GMcrlBB/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org Scastie (remote JVM)].
<
object Base58 extends App {
Line 1,448:
}
}</
=={{header|Seed7}}==
Line 1,454:
the function [http://seed7.sourceforge.net/libraries/encoding.htm#toBase(in_bigInteger,in_string) toBase],
which encodes a number with a positional numeric system. No external library is needed.
<
include "encoding.s7i";
Line 1,471:
writeln("16#" <& num radix 16 rpad 53 <& " -> " <& b);
end for;
end func;</
{{out}}
Line 1,487:
=={{header|Sidef}}==
{{trans|Raku}}
<
static chars = %w(
1 2 3 4 5 6 7 8 9
Line 1,507:
printf("%56s -> %s\n", num, encode_base58(num))
assert_eq(encode_base58(num), enc)
}</
{{out}}
<pre>
Line 1,524:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Imports System.Text
Line 1,581:
End Sub
End Module</
{{out}}
<pre>25420294593250030202636073700053352635053786165627414518 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Line 1,598:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,633:
var b58 = convertToBase58.call(hash, 16)
Fmt.print("$-56s -> $s", hash, b58)
}</
{{out}}
Line 1,651:
=={{header|zkl}}==
Uses libGMP
<
src="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv",
dst="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
Line 1,663:
0x516b6fcd0f, "bf4f89001e670274dd", 0x572e4794,
"ecac89cad93923c02321", 0x10c8511e);
ns.pump(Console.println,'wrap(n){ BN(n,16).toString(58).translate(src,dst) });</
{{out}}
<pre>
|