CRC-32: Difference between revisions
m
→{{header|11l}}: `(-)` -> `~`
(Added Pike implementation) |
Alextretyak (talk | contribs) m (→{{header|11l}}: `(-)` -> `~`) |
||
(43 intermediate revisions by 24 users not shown) | |||
Line 21:
=={{header|11l}}==
{{trans|C}}
<
L(i) 256
UInt32 rem = i
L 8
I rem [&] 1 != 0
rem >>= 1
rem (+)= EDB8'8320
Line 33:
F crc32(buf, =crc = UInt32(0))
crc =
L(k) buf
crc = (crc >> 8) (+) :crc_table[(crc [&]
R
print(hex(crc32(‘The quick brown fox jumps over the lazy dog’)))</
{{out}}
Line 45:
</pre>
=={{header|6502 Assembly}}==
<syntaxhighlight lang=6502 Assembly>PRHEX EQU $FDDA ; <= REPLACE THIS WITH THE PRHEX ROUTINE FOR YOUR MACHINE
string EQU $EC
length EQU $EE
crc0 EQU $FA
crc1 EQU $FB
crc2 EQU $FC
crc3 EQU $FD
table0 EQU $9200
table1 EQU $9300
table2 EQU $9400
table3 EQU $9500
ORG $9114
LDA #<text
STA string
LDA #>text
STA string+1
LDA #$2b ; length of text
STA length
LDA #$00
STA length+1
STA crc0
STA crc1
STA crc2
STA crc3
JSR crc32
LDA crc3
JSR PRHEX
LDA crc2
JSR PRHEX
LDA crc1
JSR PRHEX
LDA crc0
JMP PRHEX
text
ASC 'The quick brown fox jumps over the lazy dog'
; ORG $916E
crc32
JSR start
LDY string
STX string
loop
LDA length
BNE no_borrow
LDA length+1
BEQ ones_complement
DEC length+1
no_borrow
DEC length
LDA (string),Y
EOR crc0
TAX
LDA table0,X
EOR crc1
STA crc0
LDA table1,X
EOR crc2
STA crc1
LDA table2,X
EOR crc3
STA crc2
LDA table3,X
STA crc3
INY
BNE loop
INC string+1
BNE loop
start
have_table
LDX #$00
BNE loop4 ; LDX #$04 BNE ones_complement
loop256
LDA #$00
STA table3,X
STA table2,X
STA table1,X
TXA
STA table0,X
LDY #$08
loop8
LSR table3,X
ROR table2,X
ROR table1,X
ROR table0,X
BCC no_xor
LDA table3,X
EOR #$ED
STA table3,X
LDA table2,X
EOR #$B8
STA table2,X
LDA table1,X
EOR #$83
STA table1,X
LDA table0,X
EOR #$20
STA table0,X
no_xor
DEY
BNE loop8
INX
BNE loop256
ones_complement
LDX #$04
STX have_table+1 ; self-modify
loop4
DEX
LDA crc0,X
EOR #$FF
STA crc0,X
TXA
BNE loop4
RTS</syntaxhighlight>
=={{header|Ada}}==
{{works with|GNAT}}
<
with GNAT.CRC32; use GNAT.CRC32;
with Interfaces; use Interfaces;
Line 60 ⟶ 174:
num := Get_Value (crc);
IIO.Put (num, Base => 16); New_Line;
end TestCRC;</
{{out}}
<pre>16#414FA339#</pre>
=={{header|Arturo}}==
<syntaxhighlight lang=rebol>print crc "The quick brown fox jumps over the lazy dog"</syntaxhighlight>
{{out}}
<pre>414FA339</pre>
=={{header|ALGOL 68}}==
<
[0:255]BITS crc_table;
BOOL crc_table_computed := FALSE;
Line 120 ⟶ 242:
STRING s = "The quick brown fox jumps over the lazy dog";
print(("CRC32 OF ", s, " is: ", hex (crc (s)), newline))
</syntaxhighlight>
{{out}}
<pre>CRC32 OF The quick brown fox jumps over the lazy dog is: 0414fa339</pre>
=={{header|Applesoft BASIC}}==
===Implementation using Binary ASCII===
<syntaxhighlight lang=Applesoft BASIC> 0 DIM D$(1111):D$(0)="0":D$(1)="1":D$(10)="2":D$(11)="3":D$(100)="4":D$(101)="5":D$(110)="6":D$(111)="7":D$(1000)="8":D$(1001)="9":D$(1010)="A":D$(1011)="B":D$(1100)="C":D$(1101)="D":D$(1110)="E":D$(1111)="F"
1 Z$ = CHR$ (8) + CHR$ (8) + CHR$ (8) + CHR$ (8) + CHR$ (8) + CHR$ (8) + CHR$ (8) + CHR$ (8) + CHR$ (8) + CHR$ (8)
100 C$ = "00000000000000000000000000000000"
110 S$ = "The quick brown fox jumps over the lazy dog"
120 GOSUB 200"CRC32
130 PRINT D$( VAL ( MID$ (C$,1,4)))D$( VAL ( MID$ (C$,5,4)))D$( VAL ( MID$ (C$,9,4)))D$( VAL ( MID$ (C$,13,4)))D$( VAL ( MID$ (C$,17,4)))D$( VAL ( MID$ (C$,21,4)))D$( VAL ( MID$ (C$,25,4)))D$( VAL ( MID$ (C$,29,4)))" ";
140 END
200 IF LEN (S$) = 0 THEN RETURN
210 GOSUB 280"XOR #$FFFFFFFF
220 FOR I = 1 TO LEN (S$)
230 R$ = "00000000" + MID$ (C$,1,24)
235 PRINT D$( VAL ( MID$ (C$,1,4)))D$( VAL ( MID$ (C$,5,4)))D$( VAL ( MID$ (C$,9,4)))D$( VAL ( MID$ (C$,13,4)));
236 PRINT D$( VAL ( MID$ (C$,17,4)))D$( VAL ( MID$ (C$,21,4)))D$( VAL ( MID$ (C$,25,4)))D$( VAL ( MID$ (C$,29,4)))" " MID$ (S$,I,1)Z$;
240 C = ASC ( MID$ (S$,I,1)):O$ = "": FOR B = 1 TO 8:K = INT (C / 2):O$ = STR$ (C - K * 2) + O$:C = K: NEXT B
250 A = ( MID$ (C$,25,1) < > MID$ (O$,1,1)) * 128 + ( MID$ (C$,26,1) < > MID$ (O$,2,1)) * 64 + ( MID$ (C$,27,1) < > MID$ (O$,3,1)) * 32 + ( MID$ (C$,28,1) < > MID$ (O$,4,1)) * 16
251 A = ( MID$ (C$,29,1) < > MID$ (O$,5,1)) * 8 + ( MID$ (C$,30,1) < > MID$ (O$,6,1)) * 4 + ( MID$ (C$,31,1) < > MID$ (O$,7,1)) * 2 + ( MID$ (C$,32,1) < > MID$ (O$,8,1)) + A: GOSUB 300
260 C$ = STR$ (( MID$ (R$,1,1) < > MID$ (T$,1,1))) + STR$ (( MID$ (R$,2,1) < > MID$ (T$,2,1))) + STR$ (( MID$ (R$,3,1) < > MID$ (T$,3,1))) + STR$ (( MID$ (R$,4,1) < > MID$ (T$,4,1)))
261 C$ = C$ + STR$ (( MID$ (R$,5,1) < > MID$ (T$,5,1))) + STR$ (( MID$ (R$,6,1) < > MID$ (T$,6,1))) + STR$ (( MID$ (R$,7,1) < > MID$ (T$,7,1))) + STR$ (( MID$ (R$,8,1) < > MID$ (T$,8,1)))
262 C$ = C$ + STR$ (( MID$ (R$,9,1) < > MID$ (T$,9,1))) + STR$ (( MID$ (R$,10,1) < > MID$ (T$,10,1))) + STR$ (( MID$ (R$,11,1) < > MID$ (T$,11,1))) + STR$ (( MID$ (R$,12,1) < > MID$ (T$,12,1)))
263 C$ = C$ + STR$ (( MID$ (R$,13,1) < > MID$ (T$,13,1))) + STR$ (( MID$ (R$,14,1) < > MID$ (T$,14,1))) + STR$ (( MID$ (R$,15,1) < > MID$ (T$,15,1))) + STR$ (( MID$ (R$,16,1) < > MID$ (T$,16,1)))
264 C$ = C$ + STR$ (( MID$ (R$,17,1) < > MID$ (T$,17,1))) + STR$ (( MID$ (R$,18,1) < > MID$ (T$,18,1))) + STR$ (( MID$ (R$,19,1) < > MID$ (T$,19,1))) + STR$ (( MID$ (R$,20,1) < > MID$ (T$,20,1)))
265 C$ = C$ + STR$ (( MID$ (R$,21,1) < > MID$ (T$,21,1))) + STR$ (( MID$ (R$,22,1) < > MID$ (T$,22,1))) + STR$ (( MID$ (R$,23,1) < > MID$ (T$,23,1))) + STR$ (( MID$ (R$,24,1) < > MID$ (T$,24,1)))
266 C$ = C$ + STR$ (( MID$ (R$,25,1) < > MID$ (T$,25,1))) + STR$ (( MID$ (R$,26,1) < > MID$ (T$,26,1))) + STR$ (( MID$ (R$,27,1) < > MID$ (T$,27,1))) + STR$ (( MID$ (R$,28,1) < > MID$ (T$,28,1)))
267 C$ = C$ + STR$ (( MID$ (R$,29,1) < > MID$ (T$,29,1))) + STR$ (( MID$ (R$,30,1) < > MID$ (T$,30,1))) + STR$ (( MID$ (R$,31,1) < > MID$ (T$,31,1))) + STR$ (( MID$ (R$,32,1) < > MID$ (T$,32,1)))
270 NEXT I
280 B$ = "": FOR B = 1 TO 32:B$ = B$ + STR$ (( MID$ (C$,B,1) < > "1")): NEXT B:C$ = B$
290 RETURN
300 IF NOT T THEN DIM T$(255): FOR T = 0 TO 38: READ J: READ T$(J): NEXT T
310 IF LEN (T$(A)) THEN T$ = T$(A): RETURN
320 R = A:T$ = "": FOR B = 1 TO 8:N = INT (R / 2):T$ = MID$ ("01",R - N * 2 + 1,1) + T$:R = N: NEXT B:T$ = "000000000000000000000000" + T$
330 FOR J = 0 TO 7
340 X = VAL ( MID$ (T$,32,1))
350 T$ = "0" + MID$ (T$,1,31)
360 IF X THEN B$ = "": FOR B = 1 TO 32:B$ = B$ + MID$ ("01",( MID$ (T$,B,1) < > MID$ ("11101101101110001000001100100000",B,1)) + 1,1): NEXT B:T$ = B$
370 NEXT J
380 T$(A) = T$:T = T + 1
390 RETURN
600 DATA171,01000001000001000111101001100000
610 DATA247,00100011110110010110011110111111
620 DATA95,11111011110101000100110001100101
630 DATA217,11111111000011110110101001110000
640 DATA213,11110110101110010010011001011011
650 DATA179,01010010011010001110001000110110
660 DATA141,10010011000010011111111110011101
670 DATA90,10001011101111101011100011101010
680 DATA224,10100000000010101110001001111000
690 DATA187,01011100101100110110101000000100
700 DATA169,10101111000010100001101101001100
710 DATA60,00101111011011110111110010000111
720 DATA128,11101101101110001000001100100000
730 DATA36,00111100000000111110010011010001
740 DATA235,00110111110110000011101111110000
750 DATA229,11010000011000000001011011110111
760 DATA77,00001000011011010011110100101101
770 DATA167,01001000101100100011011001001011
780 DATA1,01110111000001110011000010010110
790 DATA119,11001110011000011110010010011111
800 DATA96,01001101101100100110000101011000
810 DATA158,00010111101101111011111001000011
820 DATA68,01110001101100011000010110001001
830 DATA56,00101000000000101011100010011110
840 DATA193,11101100011000111111001000100110
850 DATA87,11110101000011111100010001010111
860 DATA160,11010110110101101010001111101000
870 DATA2,11101110000011100110000100101100
880 DATA30,11111010000011110011110101100011
890 DATA7,10011110011001001001010110100011
900 DATA26,11111101011000101111100101111010
910 DATA85,00011011000000011010010101111011
920 DATA15,10010000101111110001110110010001
930 DATA201,11100010101110000111101000010100
940 DATA188,11000010110101111111111110100111
950 DATA0,00000000000000000000000000000000
960 DATA238,01000111101100101100111101111111
970 DATA181,10111011000010110100011100000011
980 DATA114,10111110000010110001000000010000</syntaxhighlight>
===Using 6502 Assembly===
<syntaxhighlight lang=Applesoft BASIC> 0 HIMEM: 37230
8A=37229:P$=" 'Q$L.L%BP#%GKSFGFB1LEZ*=!4E[-Z=!6EW-[=!TEX-W=!U-XHPR?MPN<!PJ)!7!U7!T7!607!49&^!U+!T+!6+!43 =!UIM7!U=!TI87!T=!6IC7!6=!4I 7!4/POAP;<D2(Q>5ZIYUZ0PV@"
9FORI=1TOLEN(P$):B$=MID$("D2A0--A6Q4Q5A8Q7Q8Q9R0M6--N3N4N6N8R7O2O4O6S1O7P7S4Q0--S7Q2S9U2X0J6X2X8N1A4G9T8X9U0H3H4Y0X6X7U6U7U8O5V0L5O8O9Y6Z2Z3Z5Z0Z1----J4",(ASC(MID$(P$,I))-32)*2+1,2):POKEA+I,(ASC(MID$(B$,1))-65)*10+ASC(MID$(B$,2))-48:NEXT
100 S$ = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
110 FOR I = 2 TO LEN (S$)
120 C = ASC ( MID$ (S$,I,1))
130 C$ = CHR$ (C + 32 * (C > = ASC ("A") AND C < = ASC ("Z")))
140 S$ = LEFT$ (S$,I - 1) + C$ + MID$ (S$,I + 1, LEN (S$) - I)
150 NEXT
155 PRINT MID$ (S$,1,0 * FRE (0));
160 POKE 236, PEEK (131)
170 POKE 237, PEEK (132)
180 A = PEEK (236) + PEEK (237) * 256
190 POKE 236, PEEK (A + 1)
200 POKE 237, PEEK (A + 2)
210 POKE 238, PEEK (A)
220 POKE 239,0
230 FOR I = 250 TO 253
240 POKE I,0
250 NEXT
260 CALL 37230
270 FOR I = 253 TO 250 STEP - 1
280 B = PEEK (I)
290 FOR J = 0 TO 1
300 PRINT MID$ ("0123456789ABCDEF",B / 16 + 1,1);
310 B = (B - INT (B / 16) * 16) * 16
320 NEXT J,I</syntaxhighlight>
=={{header|AutoHotkey}}==
===DllCall / WinAPI===
<
{
l := (enc = "CP1200" || enc = "UTF-16") ? 2 : 1, s := (StrPut(str, enc) - 1) * l
Line 135 ⟶ 365:
}
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")</
{{out}}
<pre>0x414fa339</pre>
===Implementation===
<
{
static table := []
Line 155 ⟶ 385:
}
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")</
{{out}}
<pre>0x414fa339</pre>
=={{header|Bait}}==
<syntaxhighlight lang="bait">
import hash.crc32
fun main() {
text := 'The quick brown fox jumps over the lazy dog'
sum := crc32.checksum(text.bytes())
println(sum.hex())
}
</syntaxhighlight>
{{out}}
<pre>
414fa339
</pre>
=={{header|C}}==
===Library===
Using [http://www.stillhq.com/gpg/source-modified-1.0.3/zlib/crc32.html zlib's crc32]:
<
#include <string.h>
#include <zlib.h>
Line 172 ⟶ 418:
return 0;
}</
===Implementation===
This code is a translation from [[{{FULLPAGENAME}}#Ruby|Ruby]], with an adjustment to use 32-bit integers. This code happens to resemble the examples from [http://tools.ietf.org/html/rfc1952#section-8 RFC 1952 section 8] and from [http://www.w3.org/TR/2003/REC-PNG-20031110/#D-CRCAppendix PNG annex D], because those examples use an identical table.
<
#include <stdio.h>
#include <string.h>
Line 224 ⟶ 470:
return 0;
}</
=={{header|C sharp|C#}}==
<
/// <summary>
/// Performs 32-bit reversed cyclic redundancy checks.
Line 395 ⟶ 544:
#endregion
}
</syntaxhighlight>
Test:
<
var arrayOfBytes = Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog");
var crc32 = new Crc32();
Console.WriteLine(crc32.Get(arrayOfBytes).ToString("X"));
</syntaxhighlight>
{{out}}
414fa339
=={{header|C++}}==
<syntaxhighlight lang=cpp>#include <array>
#include <ranges>
#include <cstdint>
#include <numeric>
#include <concepts>
#include <algorithm>
// These headers are only needed for main(), to demonstrate.
#include <iomanip>
#include <iostream>
#include <string_view>
inline constexpr auto crc_table = []() {
std::array<std::uint32_t, 256> retval{};
std::generate(retval.begin(), retval.end(),
[n = std::uint32_t{ 0 }]() mutable {
auto c = n++;
for (std::uint8_t k = 0; k < 8; ++k) {
if (c & 1) c = std::uint32_t{ 0xedb88320 } ^ (c >> 1);
else c >>= 1;
}
return c;
});
return retval;
}();
[[nodiscard]] constexpr std::uint32_t crc(const std::ranges::input_range auto& rng)
noexcept requires std::convertible_to<std::ranges::range_value_t<decltype(rng)>, std::uint8_t> {
return ~std::accumulate(std::ranges::begin(rng), std::ranges::end(rng),
~std::uint32_t{ 0 } & std::uint32_t{ 0xff'ff'ff'ffu },
[](std::uint32_t checksum, std::uint8_t value)
{ return crc_table[(checksum ^ value) & 0xff] ^ (checksum >> 8); });
}
int main() {
constexpr std::string_view str = "The quick brown fox jumps over the lazy dog";
std::cout << std::hex << std::setw(8) << std::setfill('0') << crc(str) << '\n';
}</syntaxhighlight>
{{out}}
<pre>
414fa339
</pre>
<pre>
"The quick brown fox jumps over the lazy dog"
(to hex ...)
54686520717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F67 414FA339
[other useful test vectors]
0000000000000000000000000000000000000000000000000000000000000000 190A55AD
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FF6CAB0B
000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F 91267E8A
</pre>
{{libheader|boost}}
<syntaxhighlight lang=cpp>#include <boost\crc.hpp>
#include <string>
#include <iostream>
int main()
{
std::string str( "The quick brown fox jumps over the lazy dog" );
boost::crc_32_type crc;
crc.process_bytes( str.data(), str.size() );
std::cout << "Checksum: " << std::hex << crc.checksum() << std::endl;
return 0;
}</syntaxhighlight>
{{out}}
<pre>
Checksum: 414fa339
</pre>
=={{header|Clojure}}==
{{trans|Java}}
<
str "The quick brown fox jumps over the lazy dog"]
(. crc update (. str getBytes))
(printf "CRC-32('%s') = %s\n" str (Long/toHexString (. crc getValue))))</
{{out}}
Line 421 ⟶ 645:
=={{header|COBOL}}==
{{works with|GnuCOBOL}} {{libheader|zlib}}
<
identification division.
program-id. rosetta-crc32.
Line 457 ⟶ 681:
goback.
end program rosetta-crc32.</
{{out}}
<pre>prompt$ cobc -xj crc32-zlib.cob -lz
Line 466 ⟶ 690:
=={{header|CoffeeScript}}==
Allows the specification of the initial CRC value, which defaults to 0xFFFFFFFF. Optimized for speed and terseness (then readability/indentation).
<
crc32 = do ->
table =
Line 480 ⟶ 704:
crc = crc >>> 8 ^ table[(crc ^ c.charCodeAt 0) & 255]
(crc ^ -1) >>> 0
</syntaxhighlight>
Test:
<
Output:
<syntaxhighlight lang=text>414fa339</
=={{header|Common Lisp}}==
{{libheader|Ironclad}}
<
(defun string-to-digest (str digest)
"Return the specified digest for the ASCII string as a hex string."
Line 496 ⟶ 720:
(string-to-digest "The quick brown fox jumps over the lazy dog" :crc32)
</syntaxhighlight>
{{out}}
<pre>"414fa339"</pre>
Line 503 ⟶ 727:
BlackBox Component Builder<br/>
Require ZLib Subsystem
<
MODULE BbtComputeCRC32;
IMPORT ZlibCrc32,StdLog;
Line 516 ⟶ 740:
END Do;
END BbtComputeCRC32.
</syntaxhighlight>
Execute: ^Q BbtComputeCRC32.Do<br/>
{{out}}
<pre>
0414FA339%16
</pre>
=={{header|Crystal}}==
<syntaxhighlight lang=crystal>
require "digest/crc32";
p Digest::CRC32.checksum("The quick brown fox jumps over the lazy dog").to_s(16)
</syntaxhighlight>
{{out}}
<pre>
"414fa339"
</pre>
=={{header|D}}==
<
import std.stdio, std.digest.crc;
"The quick brown fox jumps over the lazy dog"
.crc32Of.crcHexString.writeln;
}</
{{out}}
<pre>414FA339</pre>
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 548 ⟶ 783:
CRC := crc32(0, @Data[1], Length(Data));
WriteLn(Format('CRC32 = %8.8X', [CRC]));
end.</
{{out}}
<pre>CRC32 = 414FA339</pre>
=={{header|Elixir}}==
<
def crc32(str) do
IO.puts :erlang.crc32(str) |> Integer.to_string(16)
Line 559 ⟶ 794:
end
Test.crc32("The quick brown fox jumps over the lazy dog")</
{{out}}
Line 569 ⟶ 804:
Using the built-in crc32 implementation.
<
-module(crc32).
-export([test/0]).
test() ->
io:fwrite("~.16#~n",[erlang:crc32(<<"The quick brown fox jumps over the lazy dog">>)]).
</syntaxhighlight>
{{out}}
<
16#414FA339
</syntaxhighlight>
=={{header|F Sharp|F#}}==
<
module Crc32 =
Line 622 ⟶ 857:
let result = crc32OfAscii testString
printfn "CRC32: 0x%x" result
</syntaxhighlight>
{{out}}
<
ASCII Input: The quick brown fox jumps over the lazy dog
CRC32: 0x414fa339
</syntaxhighlight>
=={{header|Factor}}==
Line 641 ⟶ 876:
=={{header|FBSL}}==
<
PRINT HEX(CHECKSUM("The quick brown fox jumps over the lazy dog"))
PAUSE</
{{out}}
<pre>414FA339
Line 654 ⟶ 889:
This code can implement other types of CRC by using other polynomial constants: use $8408 for CCITT CRC-16, or $a001 for IBM CRC-16.
<
: crc/ ( n -- n ) 8 0 do dup 1 rshift swap 1 and if $edb88320 xor then loop ;
Line 666 ⟶ 901:
$ffffffff s" The quick brown fox jumps over the lazy dog" crcbuf $ffffffff xor hex. bye \ $414FA339
</syntaxhighlight>
=={{header|Fortran}}==
<
use iso_fortran_env
implicit none
Line 713 ⟶ 948:
call update_crc(s, crc)
print "(Z8)", crc
end program</
=={{header|FreeBASIC}}==
{{trans|C}}
<
' compile with: fbc -s console
Line 765 ⟶ 1,001:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>input = The quick brown fox jumps over the lazy dog
Line 773 ⟶ 1,009:
=={{header|Go}}==
===Library===
<
import (
Line 784 ⟶ 1,020:
result := crc32.ChecksumIEEE(s)
fmt.Printf("%X\n", result)
}</
{{out}}
<pre>414FA339</pre>
===Implementation===
<
import "fmt"
Line 819 ⟶ 1,055:
func main() {
fmt.Printf("%0x\n", crc32("The quick brown fox jumps over the lazy dog"))
}</
{{out}}
<pre>
Line 826 ⟶ 1,062:
=={{header|Groovy}}==
<
new java.util.zip.CRC32().with { update bytes; value }
}</
Testing:
<
=={{header|Haskell}}==
Line 837 ⟶ 1,072:
Pure Haskell:
<
import Data.Word (Word32)
import Numeric (showHex)
Line 862 ⟶ 1,097:
main :: IO ()
main = putStrLn $ crc32 "The quick brown fox jumps over the lazy dog"</
{{Out}}
<pre>414fa339</pre>
Line 869 ⟶ 1,104:
Using the zlib C library ( compile with "ghc -lz file.hs"):
<
import Numeric (showHex)
import Foreign.C
Line 881 ⟶ 1,116:
ptr <- newCString s
let r = zlib_crc32 0 ptr (genericLength s)
putStrLn $ showHex r ""</
{{Out}}
<pre>414fa339</pre>
=={{header|Haxe}}==
<
class Main {
Line 894 ⟶ 1,129:
Sys.println(crc.hex());
}
}</
{{out}}
Line 902 ⟶ 1,137:
=={{header|Icon}} and {{header|Unicon}}==
There is no library function for this so we implement one. Icon/Unicon binary operations apply to large integers so we need to mask to the desired unsigned word size. This also only applies to full bytes.
<
procedure main()
Line 934 ⟶ 1,169:
ixor(crcL[iand(255,ixor(crc,ord(!s)))+1],ishift(crc,-8)))
return hexstring(ixor(crc,mask)) # return hexstring
end</
{{libheader|Icon Programming Library}}
Line 944 ⟶ 1,179:
=={{header|J}}==
<
_3199229127</
Other possible representations of this result:
<
1095738169
require'convert'
hfd (2^32x)|((i.32) e. 32 26 23 22 16 12 11 10 8 7 5 4 2 1 0) 128!:3 'The quick brown fox jumps over the lazy dog'
414FA339</
=={{header|Java}}==
<syntaxhighlight lang=Java>
import java.util.zip.CRC32;
</syntaxhighlight>
<syntaxhighlight lang=Java>
public static void main(String[] args) throws IOException {
String string = "The quick brown fox jumps over the lazy dog";
CRC32 crc = new CRC32();
crc.update(string.getBytes());
System.out.printf("%x", crc.getValue());
}
</syntaxhighlight>
{{out}}
<pre>
414fa339
</pre>
=={{header|JavaScript}}==
<
'use strict';
// --------------------- CRC-32 ----------------------
// crc32 :: String -> Int
const crc32 = str => {
// table :: [Int]
const table = enumFromTo(0)(255).map(
n => take(9
iterate(
x => (
x & 1 ?
)(x >>> 1)
)
);
return chars(str).reduce(
(a, c) => (a >>> 8) ^ table[
(a ^ c.charCodeAt(0)) & 255
],
-1
) ^ -1;
};
//
// main :: IO ()
const main = () =>
showHex(
crc32('The quick brown fox jumps over the lazy dog')
);
// --------------------- GENERIC ---------------------
// chars :: String -> [Char]
const chars = s =>
s.split('');
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo =
}, (_, i) => m + i)
) : enumFromTo_(m)(n);
//
const
// The identity function.
x;
// iterate :: (a -> a) -> a -> Gen [a]
function*
let v =
yield(v);
v = f(v);
}
};
// showHex :: Int -> String
const showHex = n =>
// Hexadecimal string for a given integer.
'0x' + n.toString(16);
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take =
// The first n elements of a list,
// string of characters, or stream.
xs => 'GeneratorFunction' !== xs
.constructor.constructor.name ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
Line 1,056 ⟶ 1,305:
result
);
})();</
{{Out}}
<pre>
=={{header|Jsish}}==
From the shell
<
{{out}}
<pre>"414fa339"</pre>
Line 1,068 ⟶ 1,317:
=={{header|Julia}}==
===Using the zlib Library===
<
println(string(Libz.crc32(UInt8.(b"The quick brown fox jumps over the lazy dog")), base=16))
</
<pre>
414fa339
Line 1,077 ⟶ 1,326:
===Source Implementation===
{{works with|Julia|0.6}}
<
table = zeros(UInt32, 256)
Line 1,107 ⟶ 1,356:
assert(crc == 0x414fa339)
println("Message: ", str)
println("Checksum: ", hex(crc))</
{{out}}
<pre>Message: The quick brown fox jumps over the lazy dog
Checksum: 414fa339</pre>
=={{header|Kotlin}}==
<
import java.util.zip.CRC32
Line 1,126 ⟶ 1,374:
println("The CRC-32 checksum of '$text' = ${"%x".format(value)}")
}
}</
{{out}}
Line 1,137 ⟶ 1,385:
===Pure Lingo===
<
crc32 = crcObj.crc32("The quick brown fox jumps over the lazy dog")
Line 1,145 ⟶ 1,393:
put crc32.toHexString(1, crc32.length)
-- "41 4f a3 39"</
Implementation:
<
-- @desc CRC-32 Class
-- @file parent script "CRC"
Line 1,215 ⟶ 1,463:
ba.position = 1
return ba
end</
===Using an "Xtra" (=binary plugin)===
<
put cx.cx_crc32_string("The quick brown fox jumps over the lazy dog")
-- "414fa339"</
=={{header|Lua}}==
Line 1,228 ⟶ 1,475:
[https://github.com/brimworks/lua-zlib <code>zlib.crc32</code>]
<
local sum=compute("The quick brown fox jumps over the lazy dog")
print(string.format("0x%x", sum))
</syntaxhighlight>
{{out}}
0x414fa339
===Implementation===
<syntaxhighlight lang=lua>
function crc32(buf, size)
local crc = 0xFFFFFFFF
local table = {}
local rem, c
-- calculate CRC-table
for i = 0, 0xFF do
rem = i
for j = 1, 8 do
if (rem & 1 == 1) then
rem = rem >> 1
rem = rem ~ 0xEDB88320
else
rem = rem >> 1
end
end
table[i] = rem
end
for x = 1, size do
c = buf[x]
crc = (crc >> 8) ~ table[(crc & 0xFF) ~ c]
end
return crc ~ 0xFFFFFFFF
end
local str = "The quick brown fox jumps over the lazy dog"
local t = {}
for i = 1, #str do
t[i] = str:byte(i)
end
print(string.format("CRC32: %x", crc32(t,#str)))
</syntaxhighlight>
{{out}}
CRC32: 414fa339
=={{header|M2000 Interpreter}}==
===Using Code===
<
Module CheckIt {
Function PrepareTable {
Line 1,264 ⟶ 1,550:
}
CheckIt
</syntaxhighlight>
===Using Api===
<
Module CheckApi {
Declare CRC32 LIB "ntdll.RtlComputeCrc32" {Long Zero, a$, long s}
Line 1,275 ⟶ 1,561:
}
CheckApi
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
StringForm[
"The "<>type<>" hash code of \"``\" is ``.",
Line 1,284 ⟶ 1,570:
Hash[s,type,"HexString"]
]
</syntaxhighlight>
{{out}}
<pre>
Line 1,293 ⟶ 1,579:
The NekoVM is a 31 bit machine; 30 signed. Loadable primitives handle 32bit integers. The zlib library API exposes a CRC-32 function, that expects and returns Int32.
<
<doc>CRC32 in Neko</doc>
**/
Line 1,304 ⟶ 1,590:
crc = update_crc32(crc, txt, 0, $ssize(txt))
$print(crc, "\n")</
{{out}}
Line 1,315 ⟶ 1,601:
=={{header|NetRexx}}==
{{trans|Java}}
<
options replace format comments java crossref symbols binary
Line 1,326 ⟶ 1,612:
return
</syntaxhighlight>
{{out}}
Line 1,334 ⟶ 1,620:
=={{header|Nim}}==
<syntaxhighlight lang
type TCrc32* = uint32
const InitCrc32* = TCrc32(
proc createCrcTable(): array[0..255, TCrc32] =
Line 1,353 ⟶ 1,639:
result = InitCrc32
for c in s:
result = (result shr 8) xor crc32table[(result and 0xff) xor
result = not result
echo crc32("The quick brown fox jumps over the lazy dog").int64.toHex(8)</
{{out}}
<pre>414FA339</pre>
=={{header|
adapted from FreeBASIC
<syntaxhighlight lang=NOWUT>; link with PIOxxx.OBJ
sectionbss
crctable.d: resd 256
sectiondata
havetable.b: db 0
string: db "The quick brown fox jumps over the lazy dog"
stringend:
db 13,10,0 ; carriage return and null terminator
sectioncode
start!
gosub initplatform
beginfunc
localvar crc.d
callex ,printnt,"input = ".a
callex ,printnt,string
callex ,printnt,"The CRC-32 checksum = ".a
callex crc,crc32,string,stringend
callex ,printhexr,crc
endfunc
end
crc32:
beginfunc bufend.d,buf.d
localvar i.d,j.d,k.d,k2.d,crc.d
ifunequal havetable,0,tabledone
i=0
whileless i,256
k=i > j=8
countdown j
k2=k > k=_ shr 1
ifequal k2 and 1,0,noxor > k=_ xor $EDB88320
noxor:
nextcount
crctable(i shl 2)=k
i=_+1
wend
havetable=1
tabledone:
crc=-1
whileless buf,bufend
crc=_ shr 8 xor crctable(crc and $FF xor [buf].b shl 2)
buf=_+1
wend
crc=_ xor -1
endfunc crc
returnex 8 ; clean off 2 parameters from the stack
</syntaxhighlight>
{{out}}
<pre>input = The quick brown fox jumps over the lazy dog
The CRC-32 checksum = 414FA339</pre>
=={{header|Oberon-2}}==
{{Works with|oo2c Version 2}}
<
MODULE CRC32;
IMPORT
Line 1,387 ⟶ 1,730:
Out.Hex(Zlib.CRC32(0,s,0,Strings.Length(s)),0);Out.Ln
END CRC32.
</syntaxhighlight>
{{out}}
<pre>
414FA339
</pre>
=={{header|Objeck}}==
<syntaxhighlight lang=objeck>class CRC32 {
function : Main(args : String[]) ~ Nil {
"The quick brown fox jumps over the lazy dog"->ToByteArray()->CRC32()->PrintLine();
}
}
</syntaxhighlight>
{{out}}
<pre>
1095738169
</pre>
Line 1,397 ⟶ 1,753:
{{libheader|camlzip}}
<
let s = "The quick brown fox jumps over the lazy dog" in
let crc = Zlib.update_crc 0l s 0 (String.length s) in
Printf.printf "crc: %lX\n" crc</
Running this code in interpreted mode:<nowiki>[[Media:Insert non-formatted text here]][[File:[Example.jpg][http://www.example.com link title]]]</nowiki>
Line 1,407 ⟶ 1,763:
$ ocaml unix.cma -I +zip zip.cma crc.ml
crc: 414FA339
</pre>
=={{header|Ol}}==
<syntaxhighlight lang=scheme>
(define (crc32 str)
(bxor #xFFFFFFFF
(fold (lambda (crc char)
(let loop ((n 8) (crc crc) (bits char))
(if (eq? n 0)
crc
(let*((flag (band (bxor bits crc) 1))
(crc (>> crc 1))
(crc (if (eq? flag 0) crc (bxor crc #xEDB88320)))
(bits (>> bits 1)))
(loop (- n 1) crc bits)))))
#xFFFFFFFF
(string->list str))))
(print (number->string (crc32 "The quick brown fox jumps over the lazy dog") 16))
(print (number->string (crc32 (list->string (repeat #x00 32))) 16))
(print (number->string (crc32 (list->string (repeat #xFF 32))) 16))
(print (number->string (crc32 (list->string (iota 32))) 16))
</syntaxhighlight>
{{Out}}
<pre>
414fa339
190a55ad
ff6cab0b
91267e8a
</pre>
Line 1,412 ⟶ 1,797:
This Program shows how easy it is to use JAVA functionality from ooRexx.
bsf4oorexx from Sourceforge https://sourceforge.net/projects/bsf4oorexx/ makes that possible.
<
clzCRC32=bsf.importClass("java.util.zip.CRC32")
myCRC32 =clzCRC32~new
Line 1,420 ⟶ 1,805:
say 'The CRC-32 value of "'toBeEncoded'" is:' myCRC32~getValue~d2x
::requires "BSF.CLS" -- get Java bridge </
{{out}}
<pre>The CRC-32 value of "The quick brown fox jumps over the lazy dog" is: 414FA339</pre>
Line 1,429 ⟶ 1,814:
{{libheader|libz.so}}
<
install("crc32", "lLsL", "crc32", "libz.so");
s = "The quick brown fox jumps over the lazy dog";
printf("%0x\n", crc32(0, s, #s))
</syntaxhighlight>
Output:
<pre>414fa339</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang=pascal>
Program CheckCRC;
{$IFDEF fpc}{$mode Delphi}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils,crc;
function CrcString(const mystring: string) : longword;
var
crcvalue: longword;
begin
crcvalue := crc32(0,nil,0);
result := crc32(crcvalue, @mystring[1], length(mystring));
end;
var
mytext: string;
begin
myText := 'The quick brown fox jumps over the lazy dog';
writeln('crc32 = ', IntToHex(CrcString(mytext), 8));
end.</syntaxhighlight>
Output:
<pre>crc32 = 414FA339</pre>
=={{header|Perl}}==
<
use 5.010 ;
use strict ;
Line 1,448 ⟶ 1,857:
$crc->add ( "The quick brown fox jumps over the lazy dog" ) ;
say "The checksum is " . $crc->hexdigest( ) ;
</syntaxhighlight>
{{out}}
<pre>The checksum is 414fa339</pre>
=={{header|Phix}}==
Included as demo\rosetta\crc32.exw, which also includes a thread-safe version
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">table</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">have_table</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">crc32</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">have_table</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">have_table</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #000000;">table</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">256</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">255</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">rem</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rem</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rem</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rem</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">#EDB88320</span><span style="color: #0000FF;">)</span>
<span
<span style="color: #000000;">rem</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rem</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rem</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">rem</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">#100000000</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">table</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rem</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">crc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#FFFFFFFF</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">crc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">crc</span><span style="color: #0000FF;">/</span><span style="color: #000000;">#100</span><span style="color: #0000FF;">),</span><span style="color: #000000;">table</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">crc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0xff</span><span style="color: #0000FF;">),</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">crc</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">crc</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">#100000000</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">not_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">crc</span><span style="color: #0000FF;">),</span><span style="color: #000000;">#FFFFFFFF</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"The quick brown fox jumps over the lazy dog"</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The CRC of %s is %08x\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">crc32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,546 ⟶ 1,908:
PHP has a built-in function [http://us2.php.net/manual/en/function.crc32.php crc32].
<
<pre>414fa339</pre>
=={{header|PicoLisp}}==
Library and implementation.
<syntaxhighlight lang=PicoLisp>(setq *Table
(mapcar
'((N)
(do 8
(setq N
(if (bit? 1 N)
(x| (>> 1 N) `(hex "EDB88320"))
(>> 1 N) ) ) ) )
(range 0 255) ) )
(de crc32 (Lst)
(let Crc `(hex "FFFFFFFF")
(for I (chop Lst)
(setq Crc
(x|
(get
*Table
(inc (x| (& Crc 255) (char I))) )
(>> 8 Crc) ) ) )
(x| `(hex "FFFFFFFF") Crc) ) )
(let Str "The quick brown fox jumps over the lazy dog"
(println (hex (crc32 Str)))
(println
(hex (native "libz.so" "crc32" 'N 0 Str (length Str))) ) )
(bye)</syntaxhighlight>
=={{header|Pike}}==
<syntaxhighlight lang=Pike>string foo = "The quick brown fox jumps over the lazy dog";
write("0x%x\n", Gz.crc32(foo));</syntaxhighlight>
{{Out}}
<pre>
0x414fa339
</pre>
=={{header|PL/I}}==
<
crct: Proc Options(main);
/*********************************************************************
Line 1,648 ⟶ 2,049:
End;
End;
End;</
{{out}}
<pre>
Line 1,655 ⟶ 2,056:
Generate CRC32 Checksum For Byte Array Example CRC_32=D1370232
decimal 3510043186
</pre>
=={{header|PowerBASIC}}==
<
#DIM ALL
#COMPILER PBCC 6
Line 1,744 ⟶ 2,106:
crc = CRC32(STRPTR(s), LEN(s))
CON.PRINT "CRC32: " & HEX$(crc)
END FUNCTION</
{{out}}
<pre>Text: The quick brown fox jumps over the lazy dog
Line 1,751 ⟶ 2,113:
=={{header|PureBasic}}==
{{works with|PB Version 5.40}}
<
a$="The quick brown fox jumps over the lazy dog"
Line 1,761 ⟶ 2,123:
Input()
End</
{{out}}<pre>CRC32 Cecksum [hex] = 414FA339
CRC32 Cecksum [dec] = 1095738169</pre>
Line 1,769 ⟶ 2,131:
[http://docs.python.org/library/zlib.html#zlib.crc32 <code>zlib.crc32</code>] and [http://docs.python.org/library/binascii.html#binascii.crc32 <code>binascii.crc32</code>] give identical results.
<
>>> import zlib
>>> hex(zlib.crc32(s))
Line 1,776 ⟶ 2,138:
>>> import binascii
>>> hex(binascii.crc32(s))
'0x414fa339'</
If you have Python 2.x, these functions might return a negative integer; you would need to use <code>& 0xffffffff</code> to get the same answer as Python 3.x. With Python 3.x, convert first the string to bytes, for instance with <code>s.encode('UTF-8')</code>, as these functions do not accept strings.
===Implementation===
====Procedural====
<
a = []
for i in range(256):
Line 1,799 ⟶ 2,161:
crc_table = create_table()
print(hex(crc_update(b"The quick brown fox jumps over the lazy dog", 0)))</
====Composition of pure functions====
<
from functools import (reduce)
Line 1,822 ⟶ 2,184:
(a ^ ord(c)) & 0xff
],
(0xffffffff)
) ^ 0xffffffff
#
# main :: IO ()
def main():
Line 1,839 ⟶ 2,201:
#
# index (!!) :: [a] -> Int -> a
Line 1,853 ⟶ 2,215:
# iterate :: (a -> a) -> a -> Gen [a]
def iterate(f):
'''An infinite list of repeated
applications of f to x.
'''
def go(x):
v = x
Line 1,859 ⟶ 2,223:
yield v
v = f(v)
return
if __name__ == '__main__':
main()</
{{Out}}
<pre>414fa339</pre>
=={{header|QB64}}==
{{trans|C}}
<syntaxhighlight lang=QB64>
PRINT HEX$(crc32("The quick brown fox jumps over the lazy dog"))
FUNCTION crc32~& (buf AS STRING)
STATIC table(255) AS _UNSIGNED LONG
STATIC have_table AS _BYTE
DIM crc AS _UNSIGNED LONG, k AS _UNSIGNED LONG
DIM i AS LONG, j AS LONG
IF have_table = 0 THEN
FOR i = 0 TO 255
k = i
FOR j = 0 TO 7
IF (k AND 1) THEN
k = _SHR(k, 1)
k = k XOR &HEDB88320
ELSE
k = _SHR(k, 1)
END IF
table(i) = k
NEXT
NEXT
have_table = -1
END IF
crc = NOT crc ' crc = &Hffffffff
FOR i = 1 TO LEN(buf)
crc = (_SHR(crc, 8)) XOR table((crc AND &HFF) XOR ASC(buf, i))
NEXT
crc32~& = NOT crc
END FUNCTION
</syntaxhighlight>
{{Out}}
<pre>414FA339</pre>
=={{header|Quackery}}==
{{trans|Forth}}
<syntaxhighlight lang=Quackery> [ table ] is crctable ( n --> n )
256 times
[ i^ 8 times
[ dup 1 >>
swap 1 & if
[ hex EDB88320 ^ ] ]
' crctable put ]
[ hex FFFFFFFF swap
witheach
[ over ^ hex FF &
crctable
swap 8 >> ^ ]
hex FFFFFFFF ^ ] is crc-32 ( [ --> n )
$ "The quick brown fox jumps over the lazy dog" crc-32
16 base put
echo
base release</syntaxhighlight>
{{out}}
<pre>414FA339</pre>
=={{header|R}}==
<syntaxhighlight lang=R>
digest("The quick brown fox jumps over the lazy dog","crc32", serialize=F)
</syntaxhighlight>
{{out}}
<pre>[1] "414fa339"</pre>
=={{header|Racket}}==
<
(define (bytes-crc32 data)
(bitwise-xor
Line 1,889 ⟶ 2,321:
(bytes-crc32 (string->bytes/utf-8 s)))
(format "~x" (crc32 "The quick brown fox jumps over the lazy dog"))</
{{out}}
<pre>"414fa339"</pre>
=={{header|Raku}}==
(formerly Perl 6)
=== Call to native function crc32 in zlib ===
<syntaxhighlight lang=raku line>use NativeCall;
sub crc32(int32 $crc, Buf $buf, int32 $len --> int32) is native('z') { * }
my $buf = 'The quick brown fox jumps over the lazy dog'.encode;
say crc32(0, $buf, $buf.bytes).fmt('%08x');</syntaxhighlight>
The libary name "z" resolves to <tt>/usr/lib/libz.so</tt> on a typical Linux system and <tt>/usr/lib/libz.dylib</tt> on Mac OS X, but may need to be changed for other platforms. Types may be platform-dependent as well. As written, the solution has been tested on Mac OS X 10.5.8 and Arch Linux 2016.08.01 x86_64.
{{out}}
<pre>414fa339</pre>
=== Pure Raku ===
A fairly generic implementation with no regard to execution speed:
<syntaxhighlight lang=raku line>sub crc(
Blob $buf,
# polynomial including leading term, default: ISO 3309/PNG/gzip
:@poly = (1,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,1),
:$n = @poly.end, # degree of polynomial
:@init = 1 xx $n, # initial XOR bits
:@fini = 1 xx $n, # final XOR bits
:@bitorder = 0..7, # default: eat bytes LSB-first
:@crcorder = 0..$n-1, # default: MSB of checksum is coefficient of x⁰
) {
my @bit = flat ($buf.list X+& (1 X+< @bitorder))».so».Int, 0 xx $n;
@bit[0 .. $n-1] «+^=» @init;
@bit[$_ ..$_+$n] «+^=» @poly if @bit[$_] for 0..@bit.end-$n;
@bit[*-$n.. *-1] «+^=» @fini;
:2[@bit[@bit.end X- @crcorder]];
}
say crc('The quick brown fox jumps over the lazy dog'.encode('ascii')).base(16);</syntaxhighlight>
{{out}}
<pre>414FA339</pre>
=={{header|REXX}}==
<
/*─────────────────────────────────given string [as described in ISO 3309, ITU─T V.42].*/
call show 'The quick brown fox jumps over the lazy dog' /*the 1st string.*/
Line 1,924 ⟶ 2,401:
say "hex CRC─32 checksum =" c2x(checksum) left('', 15),
"dec CRC─32 checksum =" c2d(checksum) /*show the CRC─32 in hex and dec.*/
return</
{{out|output|text= when using the internal default input:}}
<pre>
Line 1,937 ⟶ 2,414:
hex CRC-32 checksum = D1370232 dec CRC-32 checksum = 3510043186
</pre>
=={{header|RPL}}==
{{trans|FreeBASIC}}
≪ → string
≪ <span style="color:red">32</span> STWS <span style="color:grey">@ set binary word size to 32</span>
'''IFERR''' ‘<span style="color:green">CRCtable</span>’ RCL '''THEN'''
{ }
<span style="color:red">0 255</span> '''FOR''' j
j R→B
<span style="color:red">0 7</span> '''START'''
SR '''IF''' LAST <span style="color:red">#1</span> AND B→R '''THEN''' <span style="color:red">#EDB88320h</span> XOR '''END'''
'''NEXT''' + '''NEXT'''
‘<span style="color:green">CRCtable</span>’ STO
'''END'''
DROP <span style="color:red">#0</span> NOT
<span style="color:red">1</span> string SIZE '''FOR''' j
SRB SWAP
<span style="color:red">#FFh</span> AND string j DUP SUB NUM R→B XOR
B→R <span style="color:red">1</span> + ‘<span style="color:green">CRCtable</span>’ SWAP GET XOR
'''NEXT'''
NOT
≫ ≫ ‘<span style="color:blue">CRC32</span>’ STO
<span style="color:red">"The quick brown fox jumps over the lazy dog"</span> <span style="color:blue">CRC32</span>
{{out}}
<pre>
1: # 414FA339h
</pre>
Line 1,942 ⟶ 2,447:
Use 'zlib' from standard library.
<
printf "0x%08x\n", Zlib.crc32('The quick brown fox jumps over the lazy dog')
# => 0x414fa339</
Reimplement CRC-32 in Ruby, with comments to show the polynomials.
<
# Divisor is a polynomial of degree 32 with coefficients modulo 2.
# We store Divisor in a 33-bit Integer; the polynomial is
Line 2,022 ⟶ 2,527:
printf "0x%08x\n", CRC.crc32("The quick brown fox jumps over the lazy dog")
# => 0x414fa339</
=={{header|Rust}}==
This does not perform any caching of the lookup table for simplicity.
<
fn crc32_compute_table() -> [u32; 256] {
let mut crc32_table = [0; 256];
Line 2,053 ⟶ 2,558:
println!("{:x}", crc32("The quick brown fox jumps over the lazy dog"));
}
</syntaxhighlight>
{{Output}}
<pre>
Line 2,061 ⟶ 2,566:
=={{header|Scala}}==
{{trans|Java}}
<
val crc=new CRC32
crc.update("The quick brown fox jumps over the lazy dog".getBytes)
println(crc.getValue.toHexString) //> 414fa339</
=={{header|Seed7}}==
<
include "crc32.s7i";
Line 2,073 ⟶ 2,578:
begin
writeln(ord(crc32("The quick brown fox jumps over the lazy dog")) radix 16 lpad0 8);
end func;</
{{out}}
Line 2,082 ⟶ 2,587:
=={{header|Shell}}==
===Bash===
<
declare -i -a CRC32_LOOKUP_TABLE
Line 2,113 ⟶ 2,618:
# crc32_string "The quick brown fox jumps over the lazy dog"
# yields 414fa339
</syntaxhighlight>
===POSIX===
The POSIX Shell has no array type and no string indexation.
It costs less to recompute polynomal shift for each character than indexing
with external tools like <code>awk</code> or <code>tr</code>.
<
# POSIX Shell CRC32 of string
# @Name: crc32.sh
Line 2,169 ⟶ 2,674:
# crc32_string "The quick brown fox jumps over the lazy dog"
# yields 414fa339</
{{out}}
<pre>bash ./crc32.sh "The quick brown fox jumps over the lazy dog"
Line 2,178 ⟶ 2,683:
{{works with|Smalltalk/X}}
the CRC32Stream utility class can do it for me:
<
{{out}}
1095738169 "which is 16r414FA339"
Line 2,184 ⟶ 2,689:
=={{header|Swift}}==
Using the zlib crc32 function available to Swift from libz.dylib.
<
let strData = "The quick brown fox jumps over the lazy dog".dataUsingEncoding(NSUTF8StringEncoding,
Line 2,190 ⟶ 2,695:
let crc = crc32(uLong(0), UnsafePointer<Bytef>(strData!.bytes), uInt(strData!.length))
println(NSString(format:"%2X", crc))</
{{out}}
<pre>414FA339
Line 2,196 ⟶ 2,701:
=={{header|Tcl}}==
<
set data "The quick brown fox jumps over the lazy dog"
puts [format "%x" [zlib crc32 $data]]</
{{out}}
<pre>414fa339</pre>
Line 2,205 ⟶ 2,710:
Alternatively, with older versions of Tcl:
{{tcllib|crc32}}
<
puts [format "%x" [crc::crc32 $data]]</
With the same input data, it produces identical output.
=={{header|TXR}}==
===Standard Library===
<syntaxhighlight lang=txrlisp>(crc32 "The quick brown fox jumps over the lazy dog")</syntaxhighlight>
{{out}}
<pre>1095738169</pre>
===FFI access to Zlib===
<syntaxhighlight lang=txrlisp>(with-dyn-lib "libz.so.1"
(deffi zlib-crc32 "crc32" ulong (ulong str uint)))</syntaxhighlight>
{{out}}
<pre>$ txr -i crc32-zlib.tl
1> (let ((s "The quick brown fox jumps over the lazy dog"))
(zlib-crc32 0 s (coded-length s)))
1095738169</pre>
Note: <code>coded-length</code> gives UTF-8 length; <code>len</code> yields a code point count. Since this is an ASCII string, the two agree.
===Lisp Code===
<syntaxhighlight lang=txrlisp>(defvarl crc-tab
#(#x00000000 #x77073096 #xee0e612c #x990951ba #x076dc419 #x706af48f
#xe963a535 #x9e6495a3 #x0edb8832 #x79dcb8a4 #xe0d5e91e #x97d2d988
#x09b64c2b #x7eb17cbd #xe7b82d07 #x90bf1d91 #x1db71064 #x6ab020f2
#xf3b97148 #x84be41de #x1adad47d #x6ddde4eb #xf4d4b551 #x83d385c7
#x136c9856 #x646ba8c0 #xfd62f97a #x8a65c9ec #x14015c4f #x63066cd9
#xfa0f3d63 #x8d080df5 #x3b6e20c8 #x4c69105e #xd56041e4 #xa2677172
#x3c03e4d1 #x4b04d447 #xd20d85fd #xa50ab56b #x35b5a8fa #x42b2986c
#xdbbbc9d6 #xacbcf940 #x32d86ce3 #x45df5c75 #xdcd60dcf #xabd13d59
#x26d930ac #x51de003a #xc8d75180 #xbfd06116 #x21b4f4b5 #x56b3c423
#xcfba9599 #xb8bda50f #x2802b89e #x5f058808 #xc60cd9b2 #xb10be924
#x2f6f7c87 #x58684c11 #xc1611dab #xb6662d3d #x76dc4190 #x01db7106
#x98d220bc #xefd5102a #x71b18589 #x06b6b51f #x9fbfe4a5 #xe8b8d433
#x7807c9a2 #x0f00f934 #x9609a88e #xe10e9818 #x7f6a0dbb #x086d3d2d
#x91646c97 #xe6635c01 #x6b6b51f4 #x1c6c6162 #x856530d8 #xf262004e
#x6c0695ed #x1b01a57b #x8208f4c1 #xf50fc457 #x65b0d9c6 #x12b7e950
#x8bbeb8ea #xfcb9887c #x62dd1ddf #x15da2d49 #x8cd37cf3 #xfbd44c65
#x4db26158 #x3ab551ce #xa3bc0074 #xd4bb30e2 #x4adfa541 #x3dd895d7
#xa4d1c46d #xd3d6f4fb #x4369e96a #x346ed9fc #xad678846 #xda60b8d0
#x44042d73 #x33031de5 #xaa0a4c5f #xdd0d7cc9 #x5005713c #x270241aa
#xbe0b1010 #xc90c2086 #x5768b525 #x206f85b3 #xb966d409 #xce61e49f
#x5edef90e #x29d9c998 #xb0d09822 #xc7d7a8b4 #x59b33d17 #x2eb40d81
#xb7bd5c3b #xc0ba6cad #xedb88320 #x9abfb3b6 #x03b6e20c #x74b1d29a
#xead54739 #x9dd277af #x04db2615 #x73dc1683 #xe3630b12 #x94643b84
#x0d6d6a3e #x7a6a5aa8 #xe40ecf0b #x9309ff9d #x0a00ae27 #x7d079eb1
#xf00f9344 #x8708a3d2 #x1e01f268 #x6906c2fe #xf762575d #x806567cb
#x196c3671 #x6e6b06e7 #xfed41b76 #x89d32be0 #x10da7a5a #x67dd4acc
#xf9b9df6f #x8ebeeff9 #x17b7be43 #x60b08ed5 #xd6d6a3e8 #xa1d1937e
#x38d8c2c4 #x4fdff252 #xd1bb67f1 #xa6bc5767 #x3fb506dd #x48b2364b
#xd80d2bda #xaf0a1b4c #x36034af6 #x41047a60 #xdf60efc3 #xa867df55
#x316e8eef #x4669be79 #xcb61b38c #xbc66831a #x256fd2a0 #x5268e236
#xcc0c7795 #xbb0b4703 #x220216b9 #x5505262f #xc5ba3bbe #xb2bd0b28
#x2bb45a92 #x5cb36a04 #xc2d7ffa7 #xb5d0cf31 #x2cd99e8b #x5bdeae1d
#x9b64c2b0 #xec63f226 #x756aa39c #x026d930a #x9c0906a9 #xeb0e363f
#x72076785 #x05005713 #x95bf4a82 #xe2b87a14 #x7bb12bae #x0cb61b38
#x92d28e9b #xe5d5be0d #x7cdcefb7 #x0bdbdf21 #x86d3d2d4 #xf1d4e242
#x68ddb3f8 #x1fda836e #x81be16cd #xf6b9265b #x6fb077e1 #x18b74777
#x88085ae6 #xff0f6a70 #x66063bca #x11010b5c #x8f659eff #xf862ae69
#x616bffd3 #x166ccf45 #xa00ae278 #xd70dd2ee #x4e048354 #x3903b3c2
#xa7672661 #xd06016f7 #x4969474d #x3e6e77db #xaed16a4a #xd9d65adc
#x40df0b66 #x37d83bf0 #xa9bcae53 #xdebb9ec5 #x47b2cf7f #x30b5ffe9
#xbdbdf21c #xcabac28a #x53b39330 #x24b4a3a6 #xbad03605 #xcdd70693
#x54de5729 #x23d967bf #xb3667a2e #xc4614ab8 #x5d681b02 #x2a6f2b94
#xb40bbe37 #xc30c8ea1 #x5a05df1b #x2d02ef8d))
(defun crc32 (buf)
(let ((crc #xffffffff)
(l (len buf)))
(each ((i 0..l))
(set crc (logxor [crc-tab (logand (logxor crc [buf i]) #xff)]
(ash crc -8))))
(logxor crc #xffffffff)))</syntaxhighlight>
{{out}}
<pre>$ ./txr -i crc.tl
warning: (crc.tl:46) defun: redefining crc32, which is a built-in defun
1> (crc32 (buf-str "The quick brown fox jumps over the lazy dog"))
1095738169</pre>
=={{header|Vala}}==
===Library===
<
void main() {
var str = (uint8[])"The quick brown fox jumps over the lazy dog".to_utf8();
stdout.printf("%lx\n", crc32(0, str));
}</
{{out}}
<pre>
Line 2,221 ⟶ 2,801:
</pre>
===Implementation===
<
{
private const uint32 s_generator = 0xedb88320u;
Line 2,257 ⟶ 2,837:
var crc32 = new Crc32();
stdout.printf("%x\n", crc32.get("The quick brown fox jumps over the lazy dog"));
}</
{{out}}
Line 2,265 ⟶ 2,845:
=={{header|VAX Assembly}}==
<
00000044 0004 2 table: .blkl 16
0044 3
Line 2,288 ⟶ 2,868:
67 6F 64 20 79 7A 61 00BC
0000002B 00C3 18 len = .-msg
00C3 19 .end crc</
=={{header|VBScript}}==
VBScript does'nt have bit rotation instructions and then bit to bit logic converts the default Double values to SIGNED 32 bit integers and back. A table driven implementation is required to speed things up. The code generates the table on the fly.
<syntaxhighlight lang=vb>
dim crctbl(255)
const crcc =&hEDB88320
sub gencrctable
for i= 0 to 255
k=i
for j=1 to 8
if k and 1 then
k=(k and &h7fffffff)\2 or (&h40000000 and ((k and &h80000000)<>0))
k=k xor crcc
else
k=(k and &h7fffffff)\2 or (&h40000000 and ((k and &h80000000)<>0))
end if
next ' j
crctbl(i)=k
next
end sub
function crc32 (buf)
dim r,r1,i
r=&hffffffff
for i=1 to len(buf)
r1=(r and &h7fffffff)\&h100 or (&h800000 and (r and &h80000000)<>0)
r=r1 xor crctbl((asc(mid(buf,i,1))xor r) and 255)
next
crc32=r xor &hffffffff
end function
'414FA339
gencrctable
wscript.stdout.writeline hex(crc32("The quick brown fox jumps over the lazy dog"))
</syntaxhighlight>
Output
<pre>
414FA339
</pre>
=={{header|Visual Basic}}==
Line 2,298 ⟶ 2,919:
{{libheader|Win32}}
Not an ideal task for Visual Basic because the language lacks bit shifting operators (which can of course be emulated, but that's slow). Then again, since the only platform supported by VB is Microsoft Windows (32 Bit Subsystem), we can let the Windows API do the work for us. RtlComputeCrc32() was available since Windows XP and is still present in Windows 10.
<
Declare Function RtlComputeCrc32 Lib "ntdll.dll" _
(ByVal dwInitial As Long, pData As Any, ByVal iLen As Long) As Long
Line 2,312 ⟶ 2,933:
Debug.Assert l = &H414FA339
End Sub</
=={{header|Visual Basic .NET}}==
Allows the resumption of calculations, useful for processing a large file with a series of buffer reads.
<
' Table for pre-calculated values.
Line 2,344 ⟶ 2,965:
End Function
End Class</
Test:
<
Function Str2BA(Str As String) As Byte()
Return System.Text.Encoding.ASCII.GetBytes(Str)
Line 2,368 ⟶ 2,989:
Crc32.cs(Str2BA(Mid(Str, 1, 20)))
Debug.Print("Crc32 = " & HexF(Crc32.cs(Str2BA(Mid(Str, 21)), False), 8))
End Sub</
Output:
<syntaxhighlight lang=text>Input = "The quick brown fox jumps over the lazy dog"
Crc32 = 414FA339
Crc32 = 414FA339</
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import hash.crc32
fn main() {
text := "The quick brown fox jumps over the lazy dog"
result := crc32.sum(text.bytes())
println(result.hex())
}
</syntaxhighlight>
{{out}}
<pre>
414fa339
</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Conv
class CRC32 {
static init() {
__table = List.filled(256, 0)
for (i in 0..255) {
var word = i
for (j in 0..7) {
if (word&1 == 1) {
word = (word >> 1) ^ 0xedb88320
} else {
word = word >> 1
}
}
__table[i] = word
}
}
static compute(s) {
var crc = ~0
var le = s.bytes.count
for (i in 0...le) {
var crb = crc & 0xff
crc = __table[crb^s[i].bytes[0]] ^ (crc >> 8)
}
return ~crc
}
}
CRC32.init()
var crc = CRC32.compute("The quick brown fox jumps over the lazy dog")
System.print(Conv.hex(crc))</syntaxhighlight>
{{out}}
<pre>
414fa339
</pre>
=={{header|XPL0}}==
<
string 0; \use zero-terminated strings
Line 2,393 ⟶ 3,070:
];
HexOut(0, CRC32("The quick brown fox jumps over the lazy dog", 43))</
{{out}}
Line 2,402 ⟶ 3,079:
=={{header|zkl}}==
Using zlib:
<
ZLib.calcCRC32(Data(Void,"The quick brown fox jumps over the lazy dog"));
//-->0x414fa339</
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
const Crc32Ieee = std.hash.Crc32;
pub fn main() !void {
var res: u32 = Crc32Ieee.hash("The quick brown fox jumps over the lazy dog");
std.debug.print("{x}\n", .{res});
}
</syntaxhighlight>
{{out}}
<pre>
414fa339
</pre>
|