CRC-32: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 21: | Line 21: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=11l>V crc_table = [0] * 256 |
||
L(i) 256 |
L(i) 256 |
||
UInt32 rem = i |
UInt32 rem = i |
||
Line 38: | Line 38: | ||
R (-)crc |
R (-)crc |
||
print(hex(crc32(‘The quick brown fox jumps over the lazy dog’)))</ |
print(hex(crc32(‘The quick brown fox jumps over the lazy dog’)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 46: | Line 46: | ||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
< |
<syntaxhighlight lang=6502 Assembly>PRHEX EQU $FDDA ; <= REPLACE THIS WITH THE PRHEX ROUTINE FOR YOUR MACHINE |
||
string EQU $EC |
string EQU $EC |
||
Line 158: | Line 158: | ||
TXA |
TXA |
||
BNE loop4 |
BNE loop4 |
||
RTS</ |
RTS</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|GNAT}} |
{{works with|GNAT}} |
||
< |
<syntaxhighlight lang=Ada>with Ada.Text_IO; use Ada.Text_IO; |
||
with GNAT.CRC32; use GNAT.CRC32; |
with GNAT.CRC32; use GNAT.CRC32; |
||
with Interfaces; use Interfaces; |
with Interfaces; use Interfaces; |
||
Line 174: | Line 174: | ||
num := Get_Value (crc); |
num := Get_Value (crc); |
||
IIO.Put (num, Base => 16); New_Line; |
IIO.Put (num, Base => 16); New_Line; |
||
end TestCRC;</ |
end TestCRC;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>16#414FA339#</pre> |
<pre>16#414FA339#</pre> |
||
Line 180: | Line 180: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang=rebol>print crc "The quick brown fox jumps over the lazy dog"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 187: | Line 187: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang=algol68> |
||
[0:255]BITS crc_table; |
[0:255]BITS crc_table; |
||
BOOL crc_table_computed := FALSE; |
BOOL crc_table_computed := FALSE; |
||
Line 242: | Line 242: | ||
STRING s = "The quick brown fox jumps over the lazy dog"; |
STRING s = "The quick brown fox jumps over the lazy dog"; |
||
print(("CRC32 OF ", s, " is: ", hex (crc (s)), newline)) |
print(("CRC32 OF ", s, " is: ", hex (crc (s)), newline)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 249: | Line 249: | ||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
===Implementation using Binary ASCII=== |
===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) |
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" |
100 C$ = "00000000000000000000000000000000" |
||
Line 324: | Line 324: | ||
960 DATA238,01000111101100101100111101111111 |
960 DATA238,01000111101100101100111101111111 |
||
970 DATA181,10111011000010110100011100000011 |
970 DATA181,10111011000010110100011100000011 |
||
980 DATA114,10111110000010110001000000010000</ |
980 DATA114,10111110000010110001000000010000</syntaxhighlight> |
||
===Using 6502 Assembly=== |
===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@" |
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 |
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 |
||
Line 353: | Line 353: | ||
300 PRINT MID$ ("0123456789ABCDEF",B / 16 + 1,1); |
300 PRINT MID$ ("0123456789ABCDEF",B / 16 + 1,1); |
||
310 B = (B - INT (B / 16) * 16) * 16 |
310 B = (B - INT (B / 16) * 16) * 16 |
||
320 NEXT J,I</ |
320 NEXT J,I</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
===DllCall / WinAPI=== |
===DllCall / WinAPI=== |
||
< |
<syntaxhighlight lang=AutoHotkey>CRC32(str, enc = "UTF-8") |
||
{ |
{ |
||
l := (enc = "CP1200" || enc = "UTF-16") ? 2 : 1, s := (StrPut(str, enc) - 1) * l |
l := (enc = "CP1200" || enc = "UTF-16") ? 2 : 1, s := (StrPut(str, enc) - 1) * l |
||
Line 365: | Line 365: | ||
} |
} |
||
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")</ |
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0x414fa339</pre> |
<pre>0x414fa339</pre> |
||
===Implementation=== |
===Implementation=== |
||
< |
<syntaxhighlight lang=AutoHotkey>CRC32(str) |
||
{ |
{ |
||
static table := [] |
static table := [] |
||
Line 385: | Line 385: | ||
} |
} |
||
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")</ |
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0x414fa339</pre> |
<pre>0x414fa339</pre> |
||
Line 392: | Line 392: | ||
===Library=== |
===Library=== |
||
Using [http://www.stillhq.com/gpg/source-modified-1.0.3/zlib/crc32.html zlib's crc32]: |
Using [http://www.stillhq.com/gpg/source-modified-1.0.3/zlib/crc32.html zlib's crc32]: |
||
< |
<syntaxhighlight lang=c>#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
#include <zlib.h> |
#include <zlib.h> |
||
Line 402: | Line 402: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===Implementation=== |
===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. |
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. |
||
< |
<syntaxhighlight lang=c>#include <inttypes.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 454: | Line 454: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang=Csharp> |
||
/// <summary> |
/// <summary> |
||
/// Performs 32-bit reversed cyclic redundancy checks. |
/// Performs 32-bit reversed cyclic redundancy checks. |
||
Line 528: | Line 528: | ||
#endregion |
#endregion |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test: |
Test: |
||
< |
<syntaxhighlight lang=Csharp> |
||
var arrayOfBytes = Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog"); |
var arrayOfBytes = Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog"); |
||
var crc32 = new Crc32(); |
var crc32 = new Crc32(); |
||
Console.WriteLine(crc32.Get(arrayOfBytes).ToString("X")); |
Console.WriteLine(crc32.Get(arrayOfBytes).ToString("X")); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 543: | Line 543: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang=cpp>#include <algorithm> |
||
#include <array> |
#include <array> |
||
#include <cstdint> |
#include <cstdint> |
||
Line 604: | Line 604: | ||
std::cout << std::hex << std::setw(8) << std::setfill('0') << crc(s.begin(), s.end()) << '\n'; |
std::cout << std::hex << std::setw(8) << std::setfill('0') << crc(s.begin(), s.end()) << '\n'; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 620: | Line 620: | ||
</pre> |
</pre> |
||
{{libheader|boost}} |
{{libheader|boost}} |
||
< |
<syntaxhighlight lang=cpp>#include <boost\crc.hpp> |
||
#include <string> |
#include <string> |
||
#include <iostream> |
#include <iostream> |
||
Line 632: | Line 632: | ||
std::cout << "Checksum: " << std::hex << crc.checksum() << std::endl; |
std::cout << "Checksum: " << std::hex << crc.checksum() << std::endl; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 640: | Line 640: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=clojure>(let [crc (new java.util.zip.CRC32) |
||
str "The quick brown fox jumps over the lazy dog"] |
str "The quick brown fox jumps over the lazy dog"] |
||
(. crc update (. str getBytes)) |
(. crc update (. str getBytes)) |
||
(printf "CRC-32('%s') = %s\n" str (Long/toHexString (. crc getValue))))</ |
(printf "CRC-32('%s') = %s\n" str (Long/toHexString (. crc getValue))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 651: | Line 651: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
{{works with|GnuCOBOL}} {{libheader|zlib}} |
{{works with|GnuCOBOL}} {{libheader|zlib}} |
||
< |
<syntaxhighlight lang=COBOL> *> tectonics: cobc -xj crc32-zlib.cob -lz |
||
identification division. |
identification division. |
||
program-id. rosetta-crc32. |
program-id. rosetta-crc32. |
||
Line 687: | Line 687: | ||
goback. |
goback. |
||
end program rosetta-crc32.</ |
end program rosetta-crc32.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>prompt$ cobc -xj crc32-zlib.cob -lz |
<pre>prompt$ cobc -xj crc32-zlib.cob -lz |
||
Line 696: | Line 696: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
Allows the specification of the initial CRC value, which defaults to 0xFFFFFFFF. Optimized for speed and terseness (then readability/indentation). |
Allows the specification of the initial CRC value, which defaults to 0xFFFFFFFF. Optimized for speed and terseness (then readability/indentation). |
||
< |
<syntaxhighlight lang=coffeescript> |
||
crc32 = do -> |
crc32 = do -> |
||
table = |
table = |
||
Line 710: | Line 710: | ||
crc = crc >>> 8 ^ table[(crc ^ c.charCodeAt 0) & 255] |
crc = crc >>> 8 ^ table[(crc ^ c.charCodeAt 0) & 255] |
||
(crc ^ -1) >>> 0 |
(crc ^ -1) >>> 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test: |
Test: |
||
< |
<syntaxhighlight lang=coffeescript>console.log (crc32 'The quick brown fox jumps over the lazy dog').toString 16</syntaxhighlight> |
||
Output: |
Output: |
||
<lang>414fa339</ |
<syntaxhighlight lang=text>414fa339</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
{{libheader|Ironclad}} |
{{libheader|Ironclad}} |
||
< |
<syntaxhighlight lang=lisp>(ql:quickload :ironclad) |
||
(defun string-to-digest (str digest) |
(defun string-to-digest (str digest) |
||
"Return the specified digest for the ASCII string as a hex string." |
"Return the specified digest for the ASCII string as a hex string." |
||
Line 726: | Line 726: | ||
(string-to-digest "The quick brown fox jumps over the lazy dog" :crc32) |
(string-to-digest "The quick brown fox jumps over the lazy dog" :crc32) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>"414fa339"</pre> |
<pre>"414fa339"</pre> |
||
Line 733: | Line 733: | ||
BlackBox Component Builder<br/> |
BlackBox Component Builder<br/> |
||
Require ZLib Subsystem |
Require ZLib Subsystem |
||
< |
<syntaxhighlight lang=oberon2> |
||
MODULE BbtComputeCRC32; |
MODULE BbtComputeCRC32; |
||
IMPORT ZlibCrc32,StdLog; |
IMPORT ZlibCrc32,StdLog; |
||
Line 746: | Line 746: | ||
END Do; |
END Do; |
||
END BbtComputeCRC32. |
END BbtComputeCRC32. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Execute: ^Q BbtComputeCRC32.Do<br/> |
Execute: ^Q BbtComputeCRC32.Do<br/> |
||
{{out}} |
{{out}} |
||
Line 754: | Line 754: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
< |
<syntaxhighlight lang=crystal> |
||
require "digest/crc32"; |
require "digest/crc32"; |
||
p Digest::CRC32.checksum("The quick brown fox jumps over the lazy dog").to_s(16) |
p Digest::CRC32.checksum("The quick brown fox jumps over the lazy dog").to_s(16) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 765: | Line 765: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang=d>void main() { |
||
import std.stdio, std.digest.crc; |
import std.stdio, std.digest.crc; |
||
"The quick brown fox jumps over the lazy dog" |
"The quick brown fox jumps over the lazy dog" |
||
.crc32Of.crcHexString.writeln; |
.crc32Of.crcHexString.writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>414FA339</pre> |
<pre>414FA339</pre> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang=delphi>program CalcCRC32; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 789: | Line 789: | ||
CRC := crc32(0, @Data[1], Length(Data)); |
CRC := crc32(0, @Data[1], Length(Data)); |
||
WriteLn(Format('CRC32 = %8.8X', [CRC])); |
WriteLn(Format('CRC32 = %8.8X', [CRC])); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>CRC32 = 414FA339</pre> |
<pre>CRC32 = 414FA339</pre> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang=elixir>defmodule Test do |
||
def crc32(str) do |
def crc32(str) do |
||
IO.puts :erlang.crc32(str) |> Integer.to_string(16) |
IO.puts :erlang.crc32(str) |> Integer.to_string(16) |
||
Line 800: | Line 800: | ||
end |
end |
||
Test.crc32("The quick brown fox jumps over the lazy dog")</ |
Test.crc32("The quick brown fox jumps over the lazy dog")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 810: | Line 810: | ||
Using the built-in crc32 implementation. |
Using the built-in crc32 implementation. |
||
< |
<syntaxhighlight lang=erlang> |
||
-module(crc32). |
-module(crc32). |
||
-export([test/0]). |
-export([test/0]). |
||
test() -> |
test() -> |
||
io:fwrite("~.16#~n",[erlang:crc32(<<"The quick brown fox jumps over the lazy dog">>)]). |
io:fwrite("~.16#~n",[erlang:crc32(<<"The quick brown fox jumps over the lazy dog">>)]). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang=erlang> |
||
16#414FA339 |
16#414FA339 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang=fsharp> |
||
module Crc32 = |
module Crc32 = |
||
Line 863: | Line 863: | ||
let result = crc32OfAscii testString |
let result = crc32OfAscii testString |
||
printfn "CRC32: 0x%x" result |
printfn "CRC32: 0x%x" result |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang=fsharp> |
||
ASCII Input: The quick brown fox jumps over the lazy dog |
ASCII Input: The quick brown fox jumps over the lazy dog |
||
CRC32: 0x414fa339 |
CRC32: 0x414fa339 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 882: | Line 882: | ||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
< |
<syntaxhighlight lang=qbasic>#APPTYPE CONSOLE |
||
PRINT HEX(CHECKSUM("The quick brown fox jumps over the lazy dog")) |
PRINT HEX(CHECKSUM("The quick brown fox jumps over the lazy dog")) |
||
PAUSE</ |
PAUSE</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>414FA339 |
<pre>414FA339 |
||
Line 895: | Line 895: | ||
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. |
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. |
||
< |
<syntaxhighlight lang=forth> |
||
: crc/ ( n -- n ) 8 0 do dup 1 rshift swap 1 and if $edb88320 xor then loop ; |
: crc/ ( n -- n ) 8 0 do dup 1 rshift swap 1 and if $edb88320 xor then loop ; |
||
Line 907: | Line 907: | ||
$ffffffff s" The quick brown fox jumps over the lazy dog" crcbuf $ffffffff xor hex. bye \ $414FA339 |
$ffffffff s" The quick brown fox jumps over the lazy dog" crcbuf $ffffffff xor hex. bye \ $414FA339 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang=fortran>module crc32_m |
||
use iso_fortran_env |
use iso_fortran_env |
||
implicit none |
implicit none |
||
Line 954: | Line 954: | ||
call update_crc(s, crc) |
call update_crc(s, crc) |
||
print "(Z8)", crc |
print "(Z8)", crc |
||
end program</ |
end program</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=freebasic>' version 18-03-2017 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 1,007: | Line 1,007: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>input = The quick brown fox jumps over the lazy dog |
<pre>input = The quick brown fox jumps over the lazy dog |
||
Line 1,015: | Line 1,015: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
===Library=== |
===Library=== |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 1,026: | Line 1,026: | ||
result := crc32.ChecksumIEEE(s) |
result := crc32.ChecksumIEEE(s) |
||
fmt.Printf("%X\n", result) |
fmt.Printf("%X\n", result) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>414FA339</pre> |
<pre>414FA339</pre> |
||
===Implementation=== |
===Implementation=== |
||
< |
<syntaxhighlight lang=go>package main |
||
import "fmt" |
import "fmt" |
||
Line 1,061: | Line 1,061: | ||
func main() { |
func main() { |
||
fmt.Printf("%0x\n", crc32("The quick brown fox jumps over the lazy dog")) |
fmt.Printf("%0x\n", crc32("The quick brown fox jumps over the lazy dog")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,068: | Line 1,068: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang=Groovy>def crc32(byte[] bytes) { |
||
new java.util.zip.CRC32().with { update bytes; value } |
new java.util.zip.CRC32().with { update bytes; value } |
||
}</ |
}</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang=Groovy>assert '414FA339' == sprintf('%04X', crc32('The quick brown fox jumps over the lazy dog'.bytes))</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 1,078: | Line 1,078: | ||
Pure Haskell: |
Pure Haskell: |
||
< |
<syntaxhighlight lang=haskell>import Data.Bits ((.&.), complement, shiftR, xor) |
||
import Data.Word (Word32) |
import Data.Word (Word32) |
||
import Numeric (showHex) |
import Numeric (showHex) |
||
Line 1,103: | Line 1,103: | ||
main :: IO () |
main :: IO () |
||
main = putStrLn $ crc32 "The quick brown fox jumps over the lazy dog"</ |
main = putStrLn $ crc32 "The quick brown fox jumps over the lazy dog"</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>414fa339</pre> |
<pre>414fa339</pre> |
||
Line 1,110: | Line 1,110: | ||
Using the zlib C library ( compile with "ghc -lz file.hs"): |
Using the zlib C library ( compile with "ghc -lz file.hs"): |
||
< |
<syntaxhighlight lang=haskell>import Data.List (genericLength) |
||
import Numeric (showHex) |
import Numeric (showHex) |
||
import Foreign.C |
import Foreign.C |
||
Line 1,122: | Line 1,122: | ||
ptr <- newCString s |
ptr <- newCString s |
||
let r = zlib_crc32 0 ptr (genericLength s) |
let r = zlib_crc32 0 ptr (genericLength s) |
||
putStrLn $ showHex r ""</ |
putStrLn $ showHex r ""</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>414fa339</pre> |
<pre>414fa339</pre> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
< |
<syntaxhighlight lang=haxe>using StringTools; |
||
class Main { |
class Main { |
||
Line 1,135: | Line 1,135: | ||
Sys.println(crc.hex()); |
Sys.println(crc.hex()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,143: | Line 1,143: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang=Icon>link hexcvt,printf |
||
procedure main() |
procedure main() |
||
Line 1,175: | Line 1,175: | ||
ixor(crcL[iand(255,ixor(crc,ord(!s)))+1],ishift(crc,-8))) |
ixor(crcL[iand(255,ixor(crc,ord(!s)))+1],ishift(crc,-8))) |
||
return hexstring(ixor(crc,mask)) # return hexstring |
return hexstring(ixor(crc,mask)) # return hexstring |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 1,185: | Line 1,185: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang=j> ((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' |
||
_3199229127</ |
_3199229127</syntaxhighlight> |
||
Other possible representations of this result: |
Other possible representations of this result: |
||
< |
<syntaxhighlight lang=j> (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' |
||
1095738169 |
1095738169 |
||
require'convert' |
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' |
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</ |
414FA339</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang=Java>import java.util.zip.* ; |
||
public class CRCMaker { |
public class CRCMaker { |
||
Line 1,206: | Line 1,206: | ||
System.out.println( "The CRC-32 value is : " + Long.toHexString( myCRC.getValue( ) ) + " !" ) ; |
System.out.println( "The CRC-32 value is : " + Long.toHexString( myCRC.getValue( ) ) + " !" ) ; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The CRC-32 value is : 414fa339 !</pre> |
<pre>The CRC-32 value is : 414fa339 !</pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang=JavaScript>(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,308: | Line 1,308: | ||
result |
result |
||
); |
); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>0x414fa339</pre> |
<pre>0x414fa339</pre> |
||
Line 1,314: | Line 1,314: | ||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
From the shell |
From the shell |
||
< |
<syntaxhighlight lang=javascript># Util.crc32('The quick brown fox jumps over the lazy dog').toString(16);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"414fa339"</pre> |
<pre>"414fa339"</pre> |
||
Line 1,320: | Line 1,320: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
===Using the zlib Library=== |
===Using the zlib Library=== |
||
< |
<syntaxhighlight lang=julia>using Libz |
||
println(string(Libz.crc32(UInt8.(b"The quick brown fox jumps over the lazy dog")), base=16)) |
println(string(Libz.crc32(UInt8.(b"The quick brown fox jumps over the lazy dog")), base=16)) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
414fa339 |
414fa339 |
||
Line 1,329: | Line 1,329: | ||
===Source Implementation=== |
===Source Implementation=== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang=Julia>function crc32(crc::Int, str::String) |
||
table = zeros(UInt32, 256) |
table = zeros(UInt32, 256) |
||
Line 1,359: | Line 1,359: | ||
assert(crc == 0x414fa339) |
assert(crc == 0x414fa339) |
||
println("Message: ", str) |
println("Message: ", str) |
||
println("Checksum: ", hex(crc))</ |
println("Checksum: ", hex(crc))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,366: | Line 1,366: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang=scala>// version 1.0.6 |
||
import java.util.zip.CRC32 |
import java.util.zip.CRC32 |
||
Line 1,377: | Line 1,377: | ||
println("The CRC-32 checksum of '$text' = ${"%x".format(value)}") |
println("The CRC-32 checksum of '$text' = ${"%x".format(value)}") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,388: | Line 1,388: | ||
===Pure Lingo=== |
===Pure Lingo=== |
||
< |
<syntaxhighlight lang=lingo>crcObj = script("CRC").new() |
||
crc32 = crcObj.crc32("The quick brown fox jumps over the lazy dog") |
crc32 = crcObj.crc32("The quick brown fox jumps over the lazy dog") |
||
Line 1,396: | Line 1,396: | ||
put crc32.toHexString(1, crc32.length) |
put crc32.toHexString(1, crc32.length) |
||
-- "41 4f a3 39"</ |
-- "41 4f a3 39"</syntaxhighlight> |
||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang=lingo>--**************************************************************************** |
||
-- @desc CRC-32 Class |
-- @desc CRC-32 Class |
||
-- @file parent script "CRC" |
-- @file parent script "CRC" |
||
Line 1,466: | Line 1,466: | ||
ba.position = 1 |
ba.position = 1 |
||
return ba |
return ba |
||
end</ |
end</syntaxhighlight> |
||
===Using an "Xtra" (=binary plugin)=== |
===Using an "Xtra" (=binary plugin)=== |
||
< |
<syntaxhighlight lang=lingo>cx = Xtra("Crypto").new() |
||
put cx.cx_crc32_string("The quick brown fox jumps over the lazy dog") |
put cx.cx_crc32_string("The quick brown fox jumps over the lazy dog") |
||
-- "414fa339"</ |
-- "414fa339"</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 1,478: | Line 1,478: | ||
[https://github.com/brimworks/lua-zlib <code>zlib.crc32</code>] |
[https://github.com/brimworks/lua-zlib <code>zlib.crc32</code>] |
||
< |
<syntaxhighlight lang=lua>local compute=require"zlib".crc32() |
||
local sum=compute("The quick brown fox jumps over the lazy dog") |
local sum=compute("The quick brown fox jumps over the lazy dog") |
||
print(string.format("0x%x", sum)) |
print(string.format("0x%x", sum)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
0x414fa339 |
0x414fa339 |
||
===Implementation=== |
===Implementation=== |
||
< |
<syntaxhighlight lang=lua> |
||
function crc32(buf, size) |
function crc32(buf, size) |
||
local crc = 0xFFFFFFFF |
local crc = 0xFFFFFFFF |
||
Line 1,521: | Line 1,521: | ||
print(string.format("CRC32: %x", crc32(t,#str))) |
print(string.format("CRC32: %x", crc32(t,#str))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
CRC32: 414fa339 |
CRC32: 414fa339 |
||
Line 1,527: | Line 1,527: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
===Using Code=== |
===Using Code=== |
||
< |
<syntaxhighlight lang=M2000 Interpreter> |
||
Module CheckIt { |
Module CheckIt { |
||
Function PrepareTable { |
Function PrepareTable { |
||
Line 1,553: | Line 1,553: | ||
} |
} |
||
CheckIt |
CheckIt |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Using Api=== |
===Using Api=== |
||
< |
<syntaxhighlight lang=M2000 Interpreter> |
||
Module CheckApi { |
Module CheckApi { |
||
Declare CRC32 LIB "ntdll.RtlComputeCrc32" {Long Zero, a$, long s} |
Declare CRC32 LIB "ntdll.RtlComputeCrc32" {Long Zero, a$, long s} |
||
Line 1,564: | Line 1,564: | ||
} |
} |
||
CheckApi |
CheckApi |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=Mathematica>type="CRC32"; (*pick one out of 13 predefined hash types*) |
||
StringForm[ |
StringForm[ |
||
"The "<>type<>" hash code of \"``\" is ``.", |
"The "<>type<>" hash code of \"``\" is ``.", |
||
Line 1,573: | Line 1,573: | ||
Hash[s,type,"HexString"] |
Hash[s,type,"HexString"] |
||
] |
] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,582: | Line 1,582: | ||
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. |
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. |
||
< |
<syntaxhighlight lang=ActionScript>/** |
||
<doc>CRC32 in Neko</doc> |
<doc>CRC32 in Neko</doc> |
||
**/ |
**/ |
||
Line 1,593: | Line 1,593: | ||
crc = update_crc32(crc, txt, 0, $ssize(txt)) |
crc = update_crc32(crc, txt, 0, $ssize(txt)) |
||
$print(crc, "\n")</ |
$print(crc, "\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,604: | Line 1,604: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=NetRexx>/* NetRexx */ |
||
options replace format comments java crossref symbols binary |
options replace format comments java crossref symbols binary |
||
Line 1,615: | Line 1,615: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,623: | Line 1,623: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang=nim>import strutils |
||
type TCrc32* = uint32 |
type TCrc32* = uint32 |
||
Line 1,645: | Line 1,645: | ||
result = not result |
result = not result |
||
echo crc32("The quick brown fox jumps over the lazy dog").int64.toHex(8)</ |
echo crc32("The quick brown fox jumps over the lazy dog").int64.toHex(8)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>414FA339</pre> |
<pre>414FA339</pre> |
||
Line 1,651: | Line 1,651: | ||
=={{header|NOWUT}}== |
=={{header|NOWUT}}== |
||
adapted from FreeBASIC |
adapted from FreeBASIC |
||
< |
<syntaxhighlight lang=NOWUT>; link with PIOxxx.OBJ |
||
sectionbss |
sectionbss |
||
Line 1,714: | Line 1,714: | ||
endfunc crc |
endfunc crc |
||
returnex 8 ; clean off 2 parameters from the stack |
returnex 8 ; clean off 2 parameters from the stack |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>input = The quick brown fox jumps over the lazy dog |
<pre>input = The quick brown fox jumps over the lazy dog |
||
Line 1,721: | Line 1,721: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
{{Works with|oo2c Version 2}} |
{{Works with|oo2c Version 2}} |
||
< |
<syntaxhighlight lang=oberon2> |
||
MODULE CRC32; |
MODULE CRC32; |
||
IMPORT |
IMPORT |
||
Line 1,733: | Line 1,733: | ||
Out.Hex(Zlib.CRC32(0,s,0,Strings.Length(s)),0);Out.Ln |
Out.Hex(Zlib.CRC32(0,s,0,Strings.Length(s)),0);Out.Ln |
||
END CRC32. |
END CRC32. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,740: | Line 1,740: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang=objeck>class CRC32 { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
"The quick brown fox jumps over the lazy dog"->ToByteArray()->CRC32()->PrintLine(); |
"The quick brown fox jumps over the lazy dog"->ToByteArray()->CRC32()->PrintLine(); |
||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,756: | Line 1,756: | ||
{{libheader|camlzip}} |
{{libheader|camlzip}} |
||
< |
<syntaxhighlight lang=ocaml>let () = |
||
let s = "The quick brown fox jumps over the lazy dog" in |
let s = "The quick brown fox jumps over the lazy dog" in |
||
let crc = Zlib.update_crc 0l s 0 (String.length s) in |
let crc = Zlib.update_crc 0l s 0 (String.length s) in |
||
Printf.printf "crc: %lX\n" crc</ |
Printf.printf "crc: %lX\n" crc</syntaxhighlight> |
||
Running this code in interpreted mode:<nowiki>[[Media:Insert non-formatted text here]][[File:[Example.jpg][http://www.example.com link title]]]</nowiki> |
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,769: | Line 1,769: | ||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
< |
<syntaxhighlight lang=scheme> |
||
(define (crc32 str) |
(define (crc32 str) |
||
(bxor #xFFFFFFFF |
(bxor #xFFFFFFFF |
||
Line 1,788: | Line 1,788: | ||
(print (number->string (crc32 (list->string (repeat #xFF 32))) 16)) |
(print (number->string (crc32 (list->string (repeat #xFF 32))) 16)) |
||
(print (number->string (crc32 (list->string (iota 32))) 16)) |
(print (number->string (crc32 (list->string (iota 32))) 16)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,800: | Line 1,800: | ||
This Program shows how easy it is to use JAVA functionality from ooRexx. |
This Program shows how easy it is to use JAVA functionality from ooRexx. |
||
bsf4oorexx from Sourceforge https://sourceforge.net/projects/bsf4oorexx/ makes that possible. |
bsf4oorexx from Sourceforge https://sourceforge.net/projects/bsf4oorexx/ makes that possible. |
||
< |
<syntaxhighlight lang=oorexx>/* ooRexx */ |
||
clzCRC32=bsf.importClass("java.util.zip.CRC32") |
clzCRC32=bsf.importClass("java.util.zip.CRC32") |
||
myCRC32 =clzCRC32~new |
myCRC32 =clzCRC32~new |
||
Line 1,808: | Line 1,808: | ||
say 'The CRC-32 value of "'toBeEncoded'" is:' myCRC32~getValue~d2x |
say 'The CRC-32 value of "'toBeEncoded'" is:' myCRC32~getValue~d2x |
||
::requires "BSF.CLS" -- get Java bridge </ |
::requires "BSF.CLS" -- get Java bridge </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The CRC-32 value of "The quick brown fox jumps over the lazy dog" is: 414FA339</pre> |
<pre>The CRC-32 value of "The quick brown fox jumps over the lazy dog" is: 414FA339</pre> |
||
Line 1,817: | Line 1,817: | ||
{{libheader|libz.so}} |
{{libheader|libz.so}} |
||
< |
<syntaxhighlight lang=parigp> |
||
install("crc32", "lLsL", "crc32", "libz.so"); |
install("crc32", "lLsL", "crc32", "libz.so"); |
||
s = "The quick brown fox jumps over the lazy dog"; |
s = "The quick brown fox jumps over the lazy dog"; |
||
printf("%0x\n", crc32(0, s, #s)) |
printf("%0x\n", crc32(0, s, #s)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 1,827: | Line 1,827: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang=Perl>#!/usr/bin/perl |
||
use 5.010 ; |
use 5.010 ; |
||
use strict ; |
use strict ; |
||
Line 1,836: | Line 1,836: | ||
$crc->add ( "The quick brown fox jumps over the lazy dog" ) ; |
$crc->add ( "The quick brown fox jumps over the lazy dog" ) ; |
||
say "The checksum is " . $crc->hexdigest( ) ; |
say "The checksum is " . $crc->hexdigest( ) ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>The checksum is 414fa339</pre> |
<pre>The checksum is 414fa339</pre> |
||
Line 1,842: | Line 1,842: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Included as demo\rosetta\crc32.exw, which also includes a thread-safe version |
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: #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;">sequence</span> <span style="color: #000000;">table</span> |
||
Line 1,878: | Line 1,878: | ||
<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: #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> |
<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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,887: | Line 1,887: | ||
PHP has a built-in function [http://us2.php.net/manual/en/function.crc32.php crc32]. |
PHP has a built-in function [http://us2.php.net/manual/en/function.crc32.php crc32]. |
||
< |
<syntaxhighlight lang=php>printf("%x\n", crc32("The quick brown fox jumps over the lazy dog"));</syntaxhighlight> |
||
<pre>414fa339</pre> |
<pre>414fa339</pre> |
||
Line 1,894: | Line 1,894: | ||
Library and implementation. |
Library and implementation. |
||
< |
<syntaxhighlight lang=PicoLisp>(setq *Table |
||
(mapcar |
(mapcar |
||
'((N) |
'((N) |
||
Line 1,920: | Line 1,920: | ||
(hex (native "libz.so" "crc32" 'N 0 Str (length Str))) ) ) |
(hex (native "libz.so" "crc32" 'N 0 Str (length Str))) ) ) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang=Pike>string foo = "The quick brown fox jumps over the lazy dog"; |
||
write("0x%x\n", Gz.crc32(foo));</ |
write("0x%x\n", Gz.crc32(foo));</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,931: | Line 1,931: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang=pli>*process source attributes xref or(!) nest; |
||
crct: Proc Options(main); |
crct: Proc Options(main); |
||
/********************************************************************* |
/********************************************************************* |
||
Line 2,028: | Line 2,028: | ||
End; |
End; |
||
End; |
End; |
||
End;</ |
End;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,038: | Line 2,038: | ||
=={{header|PowerBASIC}}== |
=={{header|PowerBASIC}}== |
||
< |
<syntaxhighlight lang=powerbasic>#COMPILE EXE |
||
#DIM ALL |
#DIM ALL |
||
#COMPILER PBCC 6 |
#COMPILER PBCC 6 |
||
Line 2,085: | Line 2,085: | ||
crc = CRC32(STRPTR(s), LEN(s)) |
crc = CRC32(STRPTR(s), LEN(s)) |
||
CON.PRINT "CRC32: " & HEX$(crc) |
CON.PRINT "CRC32: " & HEX$(crc) |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Text: The quick brown fox jumps over the lazy dog |
<pre>Text: The quick brown fox jumps over the lazy dog |
||
Line 2,092: | Line 2,092: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
{{works with|PB Version 5.40}} |
{{works with|PB Version 5.40}} |
||
< |
<syntaxhighlight lang=PureBasic> |
||
a$="The quick brown fox jumps over the lazy dog" |
a$="The quick brown fox jumps over the lazy dog" |
||
Line 2,102: | Line 2,102: | ||
Input() |
Input() |
||
End</ |
End</syntaxhighlight> |
||
{{out}}<pre>CRC32 Cecksum [hex] = 414FA339 |
{{out}}<pre>CRC32 Cecksum [hex] = 414FA339 |
||
CRC32 Cecksum [dec] = 1095738169</pre> |
CRC32 Cecksum [dec] = 1095738169</pre> |
||
Line 2,110: | Line 2,110: | ||
[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. |
[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. |
||
< |
<syntaxhighlight lang=python>>>> s = 'The quick brown fox jumps over the lazy dog' |
||
>>> import zlib |
>>> import zlib |
||
>>> hex(zlib.crc32(s)) |
>>> hex(zlib.crc32(s)) |
||
Line 2,117: | Line 2,117: | ||
>>> import binascii |
>>> import binascii |
||
>>> hex(binascii.crc32(s)) |
>>> hex(binascii.crc32(s)) |
||
'0x414fa339'</ |
'0x414fa339'</syntaxhighlight> |
||
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. |
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=== |
===Implementation=== |
||
====Procedural==== |
====Procedural==== |
||
< |
<syntaxhighlight lang=python>def create_table(): |
||
a = [] |
a = [] |
||
for i in range(256): |
for i in range(256): |
||
Line 2,140: | Line 2,140: | ||
crc_table = create_table() |
crc_table = create_table() |
||
print(hex(crc_update(b"The quick brown fox jumps over the lazy dog", 0)))</ |
print(hex(crc_update(b"The quick brown fox jumps over the lazy dog", 0)))</syntaxhighlight> |
||
====Composition of pure functions==== |
====Composition of pure functions==== |
||
< |
<syntaxhighlight lang=Python>'''CRC-32 checksums for ascii strings''' |
||
from functools import (reduce) |
from functools import (reduce) |
||
Line 2,206: | Line 2,206: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>414fa339</pre> |
<pre>414fa339</pre> |
||
Line 2,212: | Line 2,212: | ||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=QB64> |
||
PRINT HEX$(crc32("The quick brown fox jumps over the lazy dog")) |
PRINT HEX$(crc32("The quick brown fox jumps over the lazy dog")) |
||
Line 2,245: | Line 2,245: | ||
crc32~& = NOT crc |
crc32~& = NOT crc |
||
END FUNCTION |
END FUNCTION |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>414FA339</pre> |
<pre>414FA339</pre> |
||
Line 2,253: | Line 2,253: | ||
{{trans|Forth}} |
{{trans|Forth}} |
||
< |
<syntaxhighlight lang=Quackery> [ table ] is crctable ( n --> n ) |
||
256 times |
256 times |
||
Line 2,272: | Line 2,272: | ||
16 base put |
16 base put |
||
echo |
echo |
||
base release</ |
base release</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,279: | Line 2,279: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<syntaxhighlight lang=R> |
|||
<lang R> |
|||
digest("The quick brown fox jumps over the lazy dog","crc32", serialize=F) |
digest("The quick brown fox jumps over the lazy dog","crc32", serialize=F) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[1] "414fa339"</pre> |
<pre>[1] "414fa339"</pre> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang=scheme>#lang racket |
||
(define (bytes-crc32 data) |
(define (bytes-crc32 data) |
||
(bitwise-xor |
(bitwise-xor |
||
Line 2,300: | Line 2,300: | ||
(bytes-crc32 (string->bytes/utf-8 s))) |
(bytes-crc32 (string->bytes/utf-8 s))) |
||
(format "~x" (crc32 "The quick brown fox jumps over the lazy dog"))</ |
(format "~x" (crc32 "The quick brown fox jumps over the lazy dog"))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"414fa339"</pre> |
<pre>"414fa339"</pre> |
||
Line 2,309: | Line 2,309: | ||
=== Call to native function crc32 in zlib === |
=== Call to native function crc32 in zlib === |
||
<lang |
<syntaxhighlight lang=raku line>use NativeCall; |
||
sub crc32(int32 $crc, Buf $buf, int32 $len --> int32) is native('z') { * } |
sub crc32(int32 $crc, Buf $buf, int32 $len --> int32) is native('z') { * } |
||
my $buf = 'The quick brown fox jumps over the lazy dog'.encode; |
my $buf = 'The quick brown fox jumps over the lazy dog'.encode; |
||
say crc32(0, $buf, $buf.bytes).fmt('%08x');</ |
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. |
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. |
||
Line 2,325: | Line 2,325: | ||
A fairly generic implementation with no regard to execution speed: |
A fairly generic implementation with no regard to execution speed: |
||
<lang |
<syntaxhighlight lang=raku line>sub crc( |
||
Blob $buf, |
Blob $buf, |
||
# polynomial including leading term, default: ISO 3309/PNG/gzip |
# polynomial including leading term, default: ISO 3309/PNG/gzip |
||
Line 2,344: | Line 2,344: | ||
} |
} |
||
say crc('The quick brown fox jumps over the lazy dog'.encode('ascii')).base(16);</ |
say crc('The quick brown fox jumps over the lazy dog'.encode('ascii')).base(16);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,350: | Line 2,350: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang=rexx>/*REXX program computes the CRC─32 (32 bit Cyclic Redundancy Check) checksum for a */ |
||
/*─────────────────────────────────given string [as described in ISO 3309, ITU─T V.42].*/ |
/*─────────────────────────────────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.*/ |
call show 'The quick brown fox jumps over the lazy dog' /*the 1st string.*/ |
||
Line 2,380: | Line 2,380: | ||
say "hex CRC─32 checksum =" c2x(checksum) left('', 15), |
say "hex CRC─32 checksum =" c2x(checksum) left('', 15), |
||
"dec CRC─32 checksum =" c2d(checksum) /*show the CRC─32 in hex and dec.*/ |
"dec CRC─32 checksum =" c2d(checksum) /*show the CRC─32 in hex and dec.*/ |
||
return</ |
return</syntaxhighlight> |
||
{{out|output|text= when using the internal default input:}} |
{{out|output|text= when using the internal default input:}} |
||
<pre> |
<pre> |
||
Line 2,398: | Line 2,398: | ||
Use 'zlib' from standard library. |
Use 'zlib' from standard library. |
||
< |
<syntaxhighlight lang=ruby>require 'zlib' |
||
printf "0x%08x\n", Zlib.crc32('The quick brown fox jumps over the lazy dog') |
printf "0x%08x\n", Zlib.crc32('The quick brown fox jumps over the lazy dog') |
||
# => 0x414fa339</ |
# => 0x414fa339</syntaxhighlight> |
||
Reimplement CRC-32 in Ruby, with comments to show the polynomials. |
Reimplement CRC-32 in Ruby, with comments to show the polynomials. |
||
< |
<syntaxhighlight lang=ruby>module CRC |
||
# Divisor is a polynomial of degree 32 with coefficients modulo 2. |
# Divisor is a polynomial of degree 32 with coefficients modulo 2. |
||
# We store Divisor in a 33-bit Integer; the polynomial is |
# We store Divisor in a 33-bit Integer; the polynomial is |
||
Line 2,478: | Line 2,478: | ||
printf "0x%08x\n", CRC.crc32("The quick brown fox jumps over the lazy dog") |
printf "0x%08x\n", CRC.crc32("The quick brown fox jumps over the lazy dog") |
||
# => 0x414fa339</ |
# => 0x414fa339</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
This does not perform any caching of the lookup table for simplicity. |
This does not perform any caching of the lookup table for simplicity. |
||
< |
<syntaxhighlight lang=rust> |
||
fn crc32_compute_table() -> [u32; 256] { |
fn crc32_compute_table() -> [u32; 256] { |
||
let mut crc32_table = [0; 256]; |
let mut crc32_table = [0; 256]; |
||
Line 2,509: | Line 2,509: | ||
println!("{:x}", crc32("The quick brown fox jumps over the lazy dog")); |
println!("{:x}", crc32("The quick brown fox jumps over the lazy dog")); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,517: | Line 2,517: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=scala>import java.util.zip.CRC32 |
||
val crc=new CRC32 |
val crc=new CRC32 |
||
crc.update("The quick brown fox jumps over the lazy dog".getBytes) |
crc.update("The quick brown fox jumps over the lazy dog".getBytes) |
||
println(crc.getValue.toHexString) //> 414fa339</ |
println(crc.getValue.toHexString) //> 414fa339</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
||
include "crc32.s7i"; |
include "crc32.s7i"; |
||
Line 2,529: | Line 2,529: | ||
begin |
begin |
||
writeln(ord(crc32("The quick brown fox jumps over the lazy dog")) radix 16 lpad0 8); |
writeln(ord(crc32("The quick brown fox jumps over the lazy dog")) radix 16 lpad0 8); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,538: | Line 2,538: | ||
=={{header|Shell}}== |
=={{header|Shell}}== |
||
===Bash=== |
===Bash=== |
||
< |
<syntaxhighlight lang=Bash>#!/usr/bin/env bash |
||
declare -i -a CRC32_LOOKUP_TABLE |
declare -i -a CRC32_LOOKUP_TABLE |
||
Line 2,569: | Line 2,569: | ||
# crc32_string "The quick brown fox jumps over the lazy dog" |
# crc32_string "The quick brown fox jumps over the lazy dog" |
||
# yields 414fa339 |
# yields 414fa339 |
||
</syntaxhighlight> |
|||
</lang> |
|||
===POSIX=== |
===POSIX=== |
||
The POSIX Shell has no array type and no string indexation. |
The POSIX Shell has no array type and no string indexation. |
||
It costs less to recompute polynomal shift for each character than indexing |
It costs less to recompute polynomal shift for each character than indexing |
||
with external tools like <code>awk</code> or <code>tr</code>. |
with external tools like <code>awk</code> or <code>tr</code>. |
||
< |
<syntaxhighlight lang=bash>#!/usr/bin/env sh |
||
# POSIX Shell CRC32 of string |
# POSIX Shell CRC32 of string |
||
# @Name: crc32.sh |
# @Name: crc32.sh |
||
Line 2,625: | Line 2,625: | ||
# crc32_string "The quick brown fox jumps over the lazy dog" |
# crc32_string "The quick brown fox jumps over the lazy dog" |
||
# yields 414fa339</ |
# yields 414fa339</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>bash ./crc32.sh "The quick brown fox jumps over the lazy dog" |
<pre>bash ./crc32.sh "The quick brown fox jumps over the lazy dog" |
||
Line 2,634: | Line 2,634: | ||
{{works with|Smalltalk/X}} |
{{works with|Smalltalk/X}} |
||
the CRC32Stream utility class can do it for me: |
the CRC32Stream utility class can do it for me: |
||
< |
<syntaxhighlight lang=smalltalk>CRC32Stream hashValueOf:'The quick brown fox jumps over the lazy dog'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
1095738169 "which is 16r414FA339" |
1095738169 "which is 16r414FA339" |
||
Line 2,640: | Line 2,640: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
Using the zlib crc32 function available to Swift from libz.dylib. |
Using the zlib crc32 function available to Swift from libz.dylib. |
||
< |
<syntaxhighlight lang=Swift>import Foundation |
||
let strData = "The quick brown fox jumps over the lazy dog".dataUsingEncoding(NSUTF8StringEncoding, |
let strData = "The quick brown fox jumps over the lazy dog".dataUsingEncoding(NSUTF8StringEncoding, |
||
Line 2,646: | Line 2,646: | ||
let crc = crc32(uLong(0), UnsafePointer<Bytef>(strData!.bytes), uInt(strData!.length)) |
let crc = crc32(uLong(0), UnsafePointer<Bytef>(strData!.bytes), uInt(strData!.length)) |
||
println(NSString(format:"%2X", crc))</ |
println(NSString(format:"%2X", crc))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>414FA339 |
<pre>414FA339 |
||
Line 2,652: | Line 2,652: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang=tcl>package require Tcl 8.6 |
||
set data "The quick brown fox jumps over the lazy dog" |
set data "The quick brown fox jumps over the lazy dog" |
||
puts [format "%x" [zlib crc32 $data]]</ |
puts [format "%x" [zlib crc32 $data]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>414fa339</pre> |
<pre>414fa339</pre> |
||
Line 2,661: | Line 2,661: | ||
Alternatively, with older versions of Tcl: |
Alternatively, with older versions of Tcl: |
||
{{tcllib|crc32}} |
{{tcllib|crc32}} |
||
< |
<syntaxhighlight lang=tcl>package require crc32 |
||
puts [format "%x" [crc::crc32 $data]]</ |
puts [format "%x" [crc::crc32 $data]]</syntaxhighlight> |
||
With the same input data, it produces identical output. |
With the same input data, it produces identical output. |
||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
===Standard Library=== |
===Standard Library=== |
||
< |
<syntaxhighlight lang=txrlisp>(crc32 "The quick brown fox jumps over the lazy dog")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1095738169</pre> |
<pre>1095738169</pre> |
||
===FFI access to Zlib=== |
===FFI access to Zlib=== |
||
< |
<syntaxhighlight lang=txrlisp>(with-dyn-lib "libz.so.1" |
||
(deffi zlib-crc32 "crc32" ulong (ulong str uint)))</ |
(deffi zlib-crc32 "crc32" ulong (ulong str uint)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ txr -i crc32-zlib.tl |
<pre>$ txr -i crc32-zlib.tl |
||
Line 2,680: | Line 2,680: | ||
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. |
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=== |
===Lisp Code=== |
||
< |
<syntaxhighlight lang=txrlisp>(defvarl crc-tab |
||
#(#x00000000 #x77073096 #xee0e612c #x990951ba #x076dc419 #x706af48f |
#(#x00000000 #x77073096 #xee0e612c #x990951ba #x076dc419 #x706af48f |
||
#xe963a535 #x9e6495a3 #x0edb8832 #x79dcb8a4 #xe0d5e91e #x97d2d988 |
#xe963a535 #x9e6495a3 #x0edb8832 #x79dcb8a4 #xe0d5e91e #x97d2d988 |
||
Line 2,731: | Line 2,731: | ||
(set crc (logxor [crc-tab (logand (logxor crc [buf i]) #xff)] |
(set crc (logxor [crc-tab (logand (logxor crc [buf i]) #xff)] |
||
(ash crc -8)))) |
(ash crc -8)))) |
||
(logxor crc #xffffffff)))</ |
(logxor crc #xffffffff)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,741: | Line 2,741: | ||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
===Library=== |
===Library=== |
||
< |
<syntaxhighlight lang=vala>using ZLib.Utility; |
||
void main() { |
void main() { |
||
var str = (uint8[])"The quick brown fox jumps over the lazy dog".to_utf8(); |
var str = (uint8[])"The quick brown fox jumps over the lazy dog".to_utf8(); |
||
stdout.printf("%lx\n", crc32(0, str)); |
stdout.printf("%lx\n", crc32(0, str)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,752: | Line 2,752: | ||
</pre> |
</pre> |
||
===Implementation=== |
===Implementation=== |
||
< |
<syntaxhighlight lang=vala>public class Crc32 |
||
{ |
{ |
||
private const uint32 s_generator = 0xedb88320u; |
private const uint32 s_generator = 0xedb88320u; |
||
Line 2,788: | Line 2,788: | ||
var crc32 = new Crc32(); |
var crc32 = new Crc32(); |
||
stdout.printf("%x\n", crc32.get("The quick brown fox jumps over the lazy dog")); |
stdout.printf("%x\n", crc32.get("The quick brown fox jumps over the lazy dog")); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,796: | Line 2,796: | ||
=={{header|VAX Assembly}}== |
=={{header|VAX Assembly}}== |
||
< |
<syntaxhighlight lang=VAX Assembly> EDB88320 0000 1 poly: .long ^xedb88320 ;crc32 |
||
00000044 0004 2 table: .blkl 16 |
00000044 0004 2 table: .blkl 16 |
||
0044 3 |
0044 3 |
||
Line 2,819: | Line 2,819: | ||
67 6F 64 20 79 7A 61 00BC |
67 6F 64 20 79 7A 61 00BC |
||
0000002B 00C3 18 len = .-msg |
0000002B 00C3 18 len = .-msg |
||
00C3 19 .end crc</ |
00C3 19 .end crc</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{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. |
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> |
|||
<lang vb> |
|||
dim crctbl(255) |
dim crctbl(255) |
||
const crcc =&hEDB88320 |
const crcc =&hEDB88320 |
||
Line 2,856: | Line 2,856: | ||
gencrctable |
gencrctable |
||
wscript.stdout.writeline hex(crc32("The quick brown fox jumps over the lazy dog")) |
wscript.stdout.writeline hex(crc32("The quick brown fox jumps over the lazy dog")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output |
Output |
||
<pre> |
<pre> |
||
Line 2,870: | Line 2,870: | ||
{{libheader|Win32}} |
{{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. |
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. |
||
< |
<syntaxhighlight lang=vb>Option Explicit |
||
Declare Function RtlComputeCrc32 Lib "ntdll.dll" _ |
Declare Function RtlComputeCrc32 Lib "ntdll.dll" _ |
||
(ByVal dwInitial As Long, pData As Any, ByVal iLen As Long) As Long |
(ByVal dwInitial As Long, pData As Any, ByVal iLen As Long) As Long |
||
Line 2,884: | Line 2,884: | ||
Debug.Assert l = &H414FA339 |
Debug.Assert l = &H414FA339 |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
Allows the resumption of calculations, useful for processing a large file with a series of buffer reads. |
Allows the resumption of calculations, useful for processing a large file with a series of buffer reads. |
||
< |
<syntaxhighlight lang=vbnet>Public Class Crc32 |
||
' Table for pre-calculated values. |
' Table for pre-calculated values. |
||
Line 2,916: | Line 2,916: | ||
End Function |
End Function |
||
End Class</ |
End Class</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang=vbnet> ' Returns a Byte Array from a string of ASCII characters. |
||
Function Str2BA(Str As String) As Byte() |
Function Str2BA(Str As String) As Byte() |
||
Return System.Text.Encoding.ASCII.GetBytes(Str) |
Return System.Text.Encoding.ASCII.GetBytes(Str) |
||
Line 2,940: | Line 2,940: | ||
Crc32.cs(Str2BA(Mid(Str, 1, 20))) |
Crc32.cs(Str2BA(Mid(Str, 1, 20))) |
||
Debug.Print("Crc32 = " & HexF(Crc32.cs(Str2BA(Mid(Str, 21)), False), 8)) |
Debug.Print("Crc32 = " & HexF(Crc32.cs(Str2BA(Mid(Str, 21)), False), 8)) |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
Output: |
Output: |
||
<lang>Input = "The quick brown fox jumps over the lazy dog" |
<syntaxhighlight lang=text>Input = "The quick brown fox jumps over the lazy dog" |
||
Crc32 = 414FA339 |
Crc32 = 414FA339 |
||
Crc32 = 414FA339</ |
Crc32 = 414FA339</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang=ecmascript>import "/fmt" for Conv |
||
class CRC32 { |
class CRC32 { |
||
Line 2,980: | Line 2,980: | ||
CRC32.init() |
CRC32.init() |
||
var crc = CRC32.compute("The quick brown fox jumps over the lazy dog") |
var crc = CRC32.compute("The quick brown fox jumps over the lazy dog") |
||
System.print(Conv.hex(crc))</ |
System.print(Conv.hex(crc))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,988: | Line 2,988: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang=XPL0>code HexOut=27; \intrinsic routine |
||
string 0; \use zero-terminated strings |
string 0; \use zero-terminated strings |
||
Line 3,006: | Line 3,006: | ||
]; |
]; |
||
HexOut(0, CRC32("The quick brown fox jumps over the lazy dog", 43))</ |
HexOut(0, CRC32("The quick brown fox jumps over the lazy dog", 43))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,015: | Line 3,015: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Using zlib: |
Using zlib: |
||
< |
<syntaxhighlight lang=zkl>var [const] ZLib=Import("zeelib"); |
||
ZLib.calcCRC32(Data(Void,"The quick brown fox jumps over the lazy dog")); |
ZLib.calcCRC32(Data(Void,"The quick brown fox jumps over the lazy dog")); |
||
//-->0x414fa339</ |
//-->0x414fa339</syntaxhighlight> |