CRC-32: Difference between revisions

5,884 bytes added ,  2 months ago
m
→‎{{header|11l}}: `(-)` -> `~`
(Applesoft BASIC)
m (→‎{{header|11l}}: `(-)` -> `~`)
 
(16 intermediate revisions by 12 users not shown)
Line 21:
=={{header|11l}}==
{{trans|C}}
<langsyntaxhighlight lang=11l>V crc_table = [0] * 256
L(i) 256
UInt32 rem = i
Line 33:
 
F crc32(buf, =crc = UInt32(0))
crc = (-)~crc
L(k) buf
crc = (crc >> 8) (+) :crc_table[(crc [&] F'F) (+) k.code]
R (-)~crc
 
print(hex(crc32(‘The quick brown fox jumps over the lazy dog’)))</langsyntaxhighlight>
 
{{out}}
Line 46:
 
=={{header|6502 Assembly}}==
<langsyntaxhighlight lang=6502 Assembly>PRHEX EQU $FDDA ; <= REPLACE THIS WITH THE PRHEX ROUTINE FOR YOUR MACHINE
 
string EQU $EC
Line 158:
TXA
BNE loop4
RTS</langsyntaxhighlight>
=={{header|Ada}}==
{{works with|GNAT}}
<langsyntaxhighlight lang=Ada>with Ada.Text_IO; use Ada.Text_IO;
with GNAT.CRC32; use GNAT.CRC32;
with Interfaces; use Interfaces;
Line 174:
num := Get_Value (crc);
IIO.Put (num, Base => 16); New_Line;
end TestCRC;</langsyntaxhighlight>
{{out}}
<pre>16#414FA339#</pre>
Line 180:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>print crc "The quick brown fox jumps over the lazy dog"</langsyntaxhighlight>
 
{{out}}
Line 187:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang=algol68>
[0:255]BITS crc_table;
BOOL crc_table_computed := FALSE;
Line 242:
STRING s = "The quick brown fox jumps over the lazy dog";
print(("CRC32 OF ", s, " is: ", hex (crc (s)), newline))
</syntaxhighlight>
</lang>
 
{{out}}
Line 249:
=={{header|Applesoft BASIC}}==
===Implementation using Binary ASCII===
<langsyntaxhighlight 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"
Line 324:
960 DATA238,01000111101100101100111101111111
970 DATA181,10111011000010110100011100000011
980 DATA114,10111110000010110001000000010000</langsyntaxhighlight>
 
===Using 60526502 Assembly===
<langsyntaxhighlight 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
Line 353:
300 PRINT MID$ ("0123456789ABCDEF",B / 16 + 1,1);
310 B = (B - INT (B / 16) * 16) * 16
320 NEXT J,I</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
===DllCall / WinAPI===
<langsyntaxhighlight lang=AutoHotkey>CRC32(str, enc = "UTF-8")
{
l := (enc = "CP1200" || enc = "UTF-16") ? 2 : 1, s := (StrPut(str, enc) - 1) * l
Line 365:
}
 
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")</langsyntaxhighlight>
{{out}}
<pre>0x414fa339</pre>
 
===Implementation===
<langsyntaxhighlight lang=AutoHotkey>CRC32(str)
{
static table := []
Line 385:
}
 
MsgBox % CRC32("The quick brown fox jumps over the lazy dog")</langsyntaxhighlight>
{{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]:
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <string.h>
#include <zlib.h>
Line 402 ⟶ 418:
 
return 0;
}</langsyntaxhighlight>
 
===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.
 
<langsyntaxhighlight lang=c>#include <inttypes.h>
#include <stdio.h>
#include <string.h>
Line 454 ⟶ 470:
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=Csharp>
/// <summary>
/// Performs 32-bit reversed cyclic redundancy checks.
Line 528 ⟶ 544:
#endregion
}
</syntaxhighlight>
</lang>
 
Test:
<langsyntaxhighlight lang=Csharp>
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>
</lang>
 
{{out}}
Line 543 ⟶ 559:
=={{header|C++}}==
 
<langsyntaxhighlight lang=cpp>#include <algorithmarray>
#include <arrayranges>
#include <cstdint>
#include <numeric>
#include <concepts>
#include <algorithm>
 
// These headers are only needed for main(), to demonstrate.
#include <iomanip>
#include <iostream>
#include <stringstring_view>
 
inline constexpr auto crc_table = []() {
// Generates a lookup table for the checksums of all 8-bit values.
std::array<std::uint_fast32_tuint32_t, 256> generate_crc_lookup_table() noexceptretval{};
std::generate(retval.begin(), retval.end(),
{
[n = std::uint32_t{ 0 }]() mutable {
auto const reversed_polynomial = std::uint_fast32_t{0xEDB88320uL};
auto c = n++;
for (std::uint8_t k = 0; k < 8; ++k) {
// This is a function object that calculates the checksum for a value,
if (c & 1) c = std::uint32_t{ 0xedb88320 } ^ (c >> 1);
// then increments the value, starting from zero.
else c >>= 1;
struct byte_checksum
}
{
return c;
std::uint_fast32_t operator()() noexcept
{ });
return retval;
auto checksum = static_cast<std::uint_fast32_t>(n++);
}();
for (auto i = 0; i < 8; ++i)
checksum = (checksum >> 1) ^ ((checksum & 0x1u) ? reversed_polynomial : 0);
return checksum;
}
unsigned n = 0;
};
auto table = std::array<std::uint_fast32_t, 256>{};
std::generate(table.begin(), table.end(), byte_checksum{});
return table;
}
 
 
// Calculates the CRC for any sequence of values. (You could use type traits and a
[[nodiscard]] constexpr std::uint32_t crc(const std::ranges::input_range auto& rng)
// static assert to ensure the values can be converted to 8 bits.)
noexcept requires std::convertible_to<std::ranges::range_value_t<decltype(rng)>, std::uint8_t> {
template <typename InputIterator>
return ~std::accumulate(std::ranges::begin(rng), std::ranges::end(rng),
std::uint_fast32_t crc(InputIterator first, InputIterator last)
~std::uint32_t{ 0 } & std::uint32_t{ 0xff'ff'ff'ffu },
{
[](std::uint32_t checksum, std::uint8_t value)
// Generate lookup table only on first use then cache it - this is thread-safe.
{ return crc_table[(checksum ^ value) & 0xff] ^ (checksum >> 8); });
static auto const table = generate_crc_lookup_table();
// Calculate the checksum - make sure to clip to 32 bits, for systems that don't
// have a true (fast) 32-bit type.
return std::uint_fast32_t{0xFFFFFFFFuL} &
~std::accumulate(first, last,
~std::uint_fast32_t{0} & std::uint_fast32_t{0xFFFFFFFFuL},
[](std::uint_fast32_t checksum, std::uint_fast8_t value)
{ return table[(checksum ^ value) & 0xFFu] ^ (checksum >> 8); });
}
 
int main() {
constexpr std::string_view str = "The quick brown fox jumps over the lazy dog";
{
auto const s = std::string{"The quick brown fox jumps over the lazy dog"};
std::cout << std::hex << std::setw(8) << std::setfill('0') << crc(s.begin(), s.end()str) << '\n';
}</syntaxhighlight>
}
</lang>
{{out}}
<pre>
Line 620 ⟶ 614:
</pre>
{{libheader|boost}}
<langsyntaxhighlight lang=cpp>#include <boost\crc.hpp>
#include <string>
#include <iostream>
Line 632 ⟶ 626:
std::cout << "Checksum: " << std::hex << crc.checksum() << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 640 ⟶ 634:
=={{header|Clojure}}==
{{trans|Java}}
<langsyntaxhighlight lang=clojure>(let [crc (new java.util.zip.CRC32)
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))))</langsyntaxhighlight>
 
{{out}}
Line 651 ⟶ 645:
=={{header|COBOL}}==
{{works with|GnuCOBOL}} {{libheader|zlib}}
<langsyntaxhighlight lang=COBOL> *> tectonics: cobc -xj crc32-zlib.cob -lz
identification division.
program-id. rosetta-crc32.
Line 687 ⟶ 681:
 
goback.
end program rosetta-crc32.</langsyntaxhighlight>
{{out}}
<pre>prompt$ cobc -xj crc32-zlib.cob -lz
Line 696 ⟶ 690:
=={{header|CoffeeScript}}==
Allows the specification of the initial CRC value, which defaults to 0xFFFFFFFF. Optimized for speed and terseness (then readability/indentation).
<langsyntaxhighlight lang=coffeescript>
crc32 = do ->
table =
Line 710 ⟶ 704:
crc = crc >>> 8 ^ table[(crc ^ c.charCodeAt 0) & 255]
(crc ^ -1) >>> 0
</syntaxhighlight>
</lang>
Test:
<langsyntaxhighlight lang=coffeescript>console.log (crc32 'The quick brown fox jumps over the lazy dog').toString 16</langsyntaxhighlight>
Output:
<syntaxhighlight lang=text>414fa339</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
{{libheader|Ironclad}}
<langsyntaxhighlight lang=lisp>(ql:quickload :ironclad)
(defun string-to-digest (str digest)
"Return the specified digest for the ASCII string as a hex string."
Line 726 ⟶ 720:
 
(string-to-digest "The quick brown fox jumps over the lazy dog" :crc32)
</syntaxhighlight>
</lang>
{{out}}
<pre>"414fa339"</pre>
Line 733 ⟶ 727:
BlackBox Component Builder<br/>
Require ZLib Subsystem
<langsyntaxhighlight lang=oberon2>
MODULE BbtComputeCRC32;
IMPORT ZlibCrc32,StdLog;
Line 746 ⟶ 740:
END Do;
END BbtComputeCRC32.
</syntaxhighlight>
</lang>
Execute: ^Q BbtComputeCRC32.Do<br/>
{{out}}
Line 754 ⟶ 748:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang=crystal>
require "digest/crc32";
 
p Digest::CRC32.checksum("The quick brown fox jumps over the lazy dog").to_s(16)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 765 ⟶ 759:
 
=={{header|D}}==
<langsyntaxhighlight lang=d>void main() {
import std.stdio, std.digest.crc;
 
"The quick brown fox jumps over the lazy dog"
.crc32Of.crcHexString.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>414FA339</pre>
 
=={{header|Delphi}}==
<langsyntaxhighlight lang=delphi>program CalcCRC32;
 
{$APPTYPE CONSOLE}
Line 789 ⟶ 783:
CRC := crc32(0, @Data[1], Length(Data));
WriteLn(Format('CRC32 = %8.8X', [CRC]));
end.</langsyntaxhighlight>
{{out}}
<pre>CRC32 = 414FA339</pre>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang=elixir>defmodule Test do
def crc32(str) do
IO.puts :erlang.crc32(str) |> Integer.to_string(16)
Line 800 ⟶ 794:
end
 
Test.crc32("The quick brown fox jumps over the lazy dog")</langsyntaxhighlight>
 
{{out}}
Line 810 ⟶ 804:
Using the built-in crc32 implementation.
 
<langsyntaxhighlight lang=erlang>
-module(crc32).
-export([test/0]).
test() ->
io:fwrite("~.16#~n",[erlang:crc32(<<"The quick brown fox jumps over the lazy dog">>)]).
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight lang=erlang>
16#414FA339
</syntaxhighlight>
</lang>
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
module Crc32 =
Line 863 ⟶ 857:
let result = crc32OfAscii testString
printfn "CRC32: 0x%x" result
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight lang=fsharp>
ASCII Input: The quick brown fox jumps over the lazy dog
CRC32: 0x414fa339
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
Line 882 ⟶ 876:
 
=={{header|FBSL}}==
<langsyntaxhighlight lang=qbasic>#APPTYPE CONSOLE
 
PRINT HEX(CHECKSUM("The quick brown fox jumps over the lazy dog"))
 
PAUSE</langsyntaxhighlight>
{{out}}
<pre>414FA339
Line 895 ⟶ 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.
 
<langsyntaxhighlight lang=forth>
: crc/ ( n -- n ) 8 0 do dup 1 rshift swap 1 and if $edb88320 xor then loop ;
 
Line 907 ⟶ 901:
 
$ffffffff s" The quick brown fox jumps over the lazy dog" crcbuf $ffffffff xor hex. bye \ $414FA339
</syntaxhighlight>
</lang>
 
=={{header|Fortran}}==
<langsyntaxhighlight lang=fortran>module crc32_m
use iso_fortran_env
implicit none
Line 954 ⟶ 948:
call update_crc(s, crc)
print "(Z8)", crc
end program</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|C}}
<langsyntaxhighlight lang=freebasic>' version 18-03-2017
' compile with: fbc -s console
 
Line 1,007 ⟶ 1,001:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>input = The quick brown fox jumps over the lazy dog
Line 1,015 ⟶ 1,009:
=={{header|Go}}==
===Library===
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,026 ⟶ 1,020:
result := crc32.ChecksumIEEE(s)
fmt.Printf("%X\n", result)
}</langsyntaxhighlight>
{{out}}
<pre>414FA339</pre>
 
===Implementation===
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 1,061 ⟶ 1,055:
func main() {
fmt.Printf("%0x\n", crc32("The quick brown fox jumps over the lazy dog"))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,068 ⟶ 1,062:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang=Groovy>def crc32(byte[] bytes) {
new java.util.zip.CRC32().with { update bytes; value }
}</langsyntaxhighlight>
Testing:
<langsyntaxhighlight lang=Groovy>assert '414FA339' == sprintf('%04X', crc32('The quick brown fox jumps over the lazy dog'.bytes))</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 1,078 ⟶ 1,072:
Pure Haskell:
 
<langsyntaxhighlight lang=haskell>import Data.Bits ((.&.), complement, shiftR, xor)
import Data.Word (Word32)
import Numeric (showHex)
Line 1,103 ⟶ 1,097:
 
main :: IO ()
main = putStrLn $ crc32 "The quick brown fox jumps over the lazy dog"</langsyntaxhighlight>
{{Out}}
<pre>414fa339</pre>
Line 1,110 ⟶ 1,104:
Using the zlib C library ( compile with "ghc -lz file.hs"):
 
<langsyntaxhighlight lang=haskell>import Data.List (genericLength)
import Numeric (showHex)
import Foreign.C
Line 1,122 ⟶ 1,116:
ptr <- newCString s
let r = zlib_crc32 0 ptr (genericLength s)
putStrLn $ showHex r ""</langsyntaxhighlight>
{{Out}}
<pre>414fa339</pre>
 
=={{header|Haxe}}==
<langsyntaxhighlight lang=haxe>using StringTools;
 
class Main {
Line 1,135 ⟶ 1,129:
Sys.println(crc.hex());
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,143 ⟶ 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.
<langsyntaxhighlight lang=Icon>link hexcvt,printf
 
procedure main()
Line 1,175 ⟶ 1,169:
ixor(crcL[iand(255,ixor(crc,ord(!s)))+1],ishift(crc,-8)))
return hexstring(ixor(crc,mask)) # return hexstring
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 1,185 ⟶ 1,179:
 
=={{header|J}}==
<langsyntaxhighlight 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</langsyntaxhighlight>
 
Other possible representations of this result:
 
<langsyntaxhighlight 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
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</langsyntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang=Java>
<lang Java>import java.util.zip.* ;
import java.util.zip.CRC32;
 
</syntaxhighlight>
public class CRCMaker {
<syntaxhighlight lang=Java>
public static void main( String[ ] args ) {
public static void main(String[] args) throws IOException {
String toBeEncoded = new String( "The quick brown fox jumps over the lazy dog" ) ;
String string = "The quick brown fox jumps over the lazy dog";
CRC32 myCRC = new CRC32( ) ;
CRC32 crc = new CRC32();
myCRC.update( toBeEncoded.getBytes( ) ) ;
crc.update(string.getBytes());
System.out.println( "The CRC-32 value is : " + Long.toHexString( myCRC.getValue( ) ) + " !" ) ;
System.out.printf("%x", crc.getValue());
}
}
}</lang>
</syntaxhighlight>
{{out}}
<pre>
<pre>The CRC-32 value is : 414fa339 !</pre>
414fa339
</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang=JavaScript>(() => {
'use strict';
 
Line 1,308 ⟶ 1,305:
result
);
})();</langsyntaxhighlight>
{{Out}}
<pre>0x414fa339</pre>
Line 1,314 ⟶ 1,311:
=={{header|Jsish}}==
From the shell
<langsyntaxhighlight lang=javascript># Util.crc32('The quick brown fox jumps over the lazy dog').toString(16);</langsyntaxhighlight>
{{out}}
<pre>"414fa339"</pre>
Line 1,320 ⟶ 1,317:
=={{header|Julia}}==
===Using the zlib Library===
<langsyntaxhighlight lang=julia>using Libz
println(string(Libz.crc32(UInt8.(b"The quick brown fox jumps over the lazy dog")), base=16))
</langsyntaxhighlight>{{out}}
<pre>
414fa339
Line 1,329 ⟶ 1,326:
===Source Implementation===
{{works with|Julia|0.6}}
<langsyntaxhighlight lang=Julia>function crc32(crc::Int, str::String)
table = zeros(UInt32, 256)
 
Line 1,359 ⟶ 1,356:
assert(crc == 0x414fa339)
println("Message: ", str)
println("Checksum: ", hex(crc))</langsyntaxhighlight>
 
{{out}}
Line 1,366 ⟶ 1,363:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang=scala>// version 1.0.6
 
import java.util.zip.CRC32
Line 1,377 ⟶ 1,374:
println("The CRC-32 checksum of '$text' = ${"%x".format(value)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,388 ⟶ 1,385:
===Pure Lingo===
 
<langsyntaxhighlight lang=lingo>crcObj = script("CRC").new()
 
crc32 = crcObj.crc32("The quick brown fox jumps over the lazy dog")
Line 1,396 ⟶ 1,393:
 
put crc32.toHexString(1, crc32.length)
-- "41 4f a3 39"</langsyntaxhighlight>
 
Implementation:
 
<langsyntaxhighlight lang=lingo>--****************************************************************************
-- @desc CRC-32 Class
-- @file parent script "CRC"
Line 1,466 ⟶ 1,463:
ba.position = 1
return ba
end</langsyntaxhighlight>
 
===Using an "Xtra" (=binary plugin)===
 
<langsyntaxhighlight lang=lingo>cx = Xtra("Crypto").new()
put cx.cx_crc32_string("The quick brown fox jumps over the lazy dog")
-- "414fa339"</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 1,478 ⟶ 1,475:
[https://github.com/brimworks/lua-zlib <code>zlib.crc32</code>]
 
<langsyntaxhighlight lang=lua>local compute=require"zlib".crc32()
local sum=compute("The quick brown fox jumps over the lazy dog")
print(string.format("0x%x", sum))
</syntaxhighlight>
</lang>
 
{{out}}
0x414fa339
===Implementation===
<langsyntaxhighlight lang=lua>
function crc32(buf, size)
local crc = 0xFFFFFFFF
Line 1,521 ⟶ 1,518:
 
print(string.format("CRC32: %x", crc32(t,#str)))
</syntaxhighlight>
</lang>
{{out}}
CRC32: 414fa339
Line 1,527 ⟶ 1,524:
=={{header|M2000 Interpreter}}==
===Using Code===
<langsyntaxhighlight lang=M2000 Interpreter>
Module CheckIt {
Function PrepareTable {
Line 1,553 ⟶ 1,550:
}
CheckIt
</syntaxhighlight>
</lang>
 
===Using Api===
<langsyntaxhighlight lang=M2000 Interpreter>
Module CheckApi {
Declare CRC32 LIB "ntdll.RtlComputeCrc32" {Long Zero, a$, long s}
Line 1,564 ⟶ 1,561:
}
CheckApi
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>type="CRC32"; (*pick one out of 13 predefined hash types*)
StringForm[
"The "<>type<>" hash code of \"``\" is ``.",
Line 1,573 ⟶ 1,570:
Hash[s,type,"HexString"]
]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,582 ⟶ 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.
 
<langsyntaxhighlight lang=ActionScript>/**
<doc>CRC32 in Neko</doc>
**/
Line 1,593 ⟶ 1,590:
 
crc = update_crc32(crc, txt, 0, $ssize(txt))
$print(crc, "\n")</langsyntaxhighlight>
 
{{out}}
Line 1,604 ⟶ 1,601:
=={{header|NetRexx}}==
{{trans|Java}}
<langsyntaxhighlight lang=NetRexx>/* NetRexx */
options replace format comments java crossref symbols binary
 
Line 1,615 ⟶ 1,612:
 
return
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,623 ⟶ 1,620:
 
=={{header|Nim}}==
<langsyntaxhighlight lang=nim>import strutils
 
type TCrc32* = uint32
Line 1,645 ⟶ 1,642:
result = not result
 
echo crc32("The quick brown fox jumps over the lazy dog").int64.toHex(8)</langsyntaxhighlight>
{{out}}
<pre>414FA339</pre>
Line 1,651 ⟶ 1,648:
=={{header|NOWUT}}==
adapted from FreeBASIC
<langsyntaxhighlight lang=NOWUT>; link with PIOxxx.OBJ
 
sectionbss
Line 1,714 ⟶ 1,711:
endfunc crc
returnex 8 ; clean off 2 parameters from the stack
</syntaxhighlight>
</lang>
{{out}}
<pre>input = The quick brown fox jumps over the lazy dog
Line 1,721 ⟶ 1,718:
=={{header|Oberon-2}}==
{{Works with|oo2c Version 2}}
<langsyntaxhighlight lang=oberon2>
MODULE CRC32;
IMPORT
Line 1,733 ⟶ 1,730:
Out.Hex(Zlib.CRC32(0,s,0,Strings.Length(s)),0);Out.Ln
END CRC32.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,740 ⟶ 1,737:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang=objeck>class CRC32 {
function : Main(args : String[]) ~ Nil {
"The quick brown fox jumps over the lazy dog"->ToByteArray()->CRC32()->PrintLine();
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,756 ⟶ 1,753:
{{libheader|camlzip}}
 
<langsyntaxhighlight lang=ocaml>let () =
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</langsyntaxhighlight>
 
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 ⟶ 1,766:
 
=={{header|Ol}}==
<langsyntaxhighlight lang=scheme>
(define (crc32 str)
(bxor #xFFFFFFFF
Line 1,788 ⟶ 1,785:
(print (number->string (crc32 (list->string (repeat #xFF 32))) 16))
(print (number->string (crc32 (list->string (iota 32))) 16))
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,800 ⟶ 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.
<langsyntaxhighlight lang=oorexx>/* ooRexx */
clzCRC32=bsf.importClass("java.util.zip.CRC32")
myCRC32 =clzCRC32~new
Line 1,808 ⟶ 1,805:
say 'The CRC-32 value of "'toBeEncoded'" is:' myCRC32~getValue~d2x
 
::requires "BSF.CLS" -- get Java bridge </langsyntaxhighlight>
{{out}}
<pre>The CRC-32 value of "The quick brown fox jumps over the lazy dog" is: 414FA339</pre>
Line 1,817 ⟶ 1,814:
{{libheader|libz.so}}
 
<langsyntaxhighlight lang=parigp>
install("crc32", "lLsL", "crc32", "libz.so");
s = "The quick brown fox jumps over the lazy dog";
printf("%0x\n", crc32(0, s, #s))
</syntaxhighlight>
</lang>
 
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}}==
<langsyntaxhighlight lang=Perl>#!/usr/bin/perl
use 5.010 ;
use strict ;
Line 1,836 ⟶ 1,857:
$crc->add ( "The quick brown fox jumps over the lazy dog" ) ;
say "The checksum is " . $crc->hexdigest( ) ;
</syntaxhighlight>
</lang>
{{out}}
<pre>The checksum is 414fa339</pre>
Line 1,842 ⟶ 1,863:
=={{header|Phix}}==
Included as demo\rosetta\crc32.exw, which also includes a thread-safe version
<!--<langsyntaxhighlight Phixlang="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>
Line 1,878 ⟶ 1,899:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,887 ⟶ 1,908:
PHP has a built-in function [http://us2.php.net/manual/en/function.crc32.php crc32].
 
<langsyntaxhighlight lang=php>printf("%x\n", crc32("The quick brown fox jumps over the lazy dog"));</langsyntaxhighlight>
 
<pre>414fa339</pre>
Line 1,894 ⟶ 1,915:
Library and implementation.
 
<langsyntaxhighlight lang=PicoLisp>(setq *Table
(mapcar
'((N)
Line 1,920 ⟶ 1,941:
(hex (native "libz.so" "crc32" 'N 0 Str (length Str))) ) )
(bye)</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight lang=Pike>string foo = "The quick brown fox jumps over the lazy dog";
write("0x%x\n", Gz.crc32(foo));</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,931 ⟶ 1,952:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang=pli>*process source attributes xref or(!) nest;
crct: Proc Options(main);
/*********************************************************************
Line 2,028 ⟶ 2,049:
End;
End;
End;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,038 ⟶ 2,059:
 
=={{header|PowerBASIC}}==
<langsyntaxhighlight lang=powerbasic>#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
Line 2,085 ⟶ 2,106:
crc = CRC32(STRPTR(s), LEN(s))
CON.PRINT "CRC32: " & HEX$(crc)
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre>Text: The quick brown fox jumps over the lazy dog
Line 2,092 ⟶ 2,113:
=={{header|PureBasic}}==
{{works with|PB Version 5.40}}
<langsyntaxhighlight lang=PureBasic>
a$="The quick brown fox jumps over the lazy dog"
 
Line 2,102 ⟶ 2,123:
Input()
 
End</langsyntaxhighlight>
{{out}}<pre>CRC32 Cecksum [hex] = 414FA339
CRC32 Cecksum [dec] = 1095738169</pre>
Line 2,110 ⟶ 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.
 
<langsyntaxhighlight lang=python>>>> s = 'The quick brown fox jumps over the lazy dog'
>>> import zlib
>>> hex(zlib.crc32(s))
Line 2,117 ⟶ 2,138:
>>> import binascii
>>> hex(binascii.crc32(s))
'0x414fa339'</langsyntaxhighlight>
 
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====
<langsyntaxhighlight lang=python>def create_table():
a = []
for i in range(256):
Line 2,140 ⟶ 2,161:
crc_table = create_table()
print(hex(crc_update(b"The quick brown fox jumps over the lazy dog", 0)))</langsyntaxhighlight>
 
====Composition of pure functions====
<langsyntaxhighlight lang=Python>'''CRC-32 checksums for ascii strings'''
 
from functools import (reduce)
Line 2,206 ⟶ 2,227:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>414fa339</pre>
Line 2,212 ⟶ 2,233:
=={{header|QB64}}==
{{trans|C}}
<langsyntaxhighlight lang=QB64>
PRINT HEX$(crc32("The quick brown fox jumps over the lazy dog"))
 
Line 2,245 ⟶ 2,266:
crc32~& = NOT crc
END FUNCTION
</syntaxhighlight>
</lang>
{{Out}}
<pre>414FA339</pre>
Line 2,253 ⟶ 2,274:
{{trans|Forth}}
 
<langsyntaxhighlight lang=Quackery> [ table ] is crctable ( n --> n )
 
256 times
Line 2,272 ⟶ 2,293:
16 base put
echo
base release</langsyntaxhighlight>
 
{{out}}
Line 2,279 ⟶ 2,300:
 
=={{header|R}}==
<syntaxhighlight lang=R>
<lang R>
digest("The quick brown fox jumps over the lazy dog","crc32", serialize=F)
</syntaxhighlight>
</lang>
{{out}}
<pre>[1] "414fa339"</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang=scheme>#lang racket
(define (bytes-crc32 data)
(bitwise-xor
Line 2,300 ⟶ 2,321:
(bytes-crc32 (string->bytes/utf-8 s)))
 
(format "~x" (crc32 "The quick brown fox jumps over the lazy dog"))</langsyntaxhighlight>
{{out}}
<pre>"414fa339"</pre>
Line 2,309 ⟶ 2,330:
=== Call to native function crc32 in zlib ===
 
<syntaxhighlight lang=raku perl6line>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');</langsyntaxhighlight>
 
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 ⟶ 2,346:
A fairly generic implementation with no regard to execution speed:
 
<syntaxhighlight lang=raku perl6line>sub crc(
Blob $buf,
# polynomial including leading term, default: ISO 3309/PNG/gzip
Line 2,344 ⟶ 2,365:
}
 
say crc('The quick brown fox jumps over the lazy dog'.encode('ascii')).base(16);</langsyntaxhighlight>
 
{{out}}
Line 2,350 ⟶ 2,371:
 
=={{header|REXX}}==
<langsyntaxhighlight 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].*/
call show 'The quick brown fox jumps over the lazy dog' /*the 1st string.*/
Line 2,380 ⟶ 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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 2,393 ⟶ 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 2,398 ⟶ 2,447:
Use 'zlib' from standard library.
 
<langsyntaxhighlight lang=ruby>require 'zlib'
printf "0x%08x\n", Zlib.crc32('The quick brown fox jumps over the lazy dog')
# => 0x414fa339</langsyntaxhighlight>
 
Reimplement CRC-32 in Ruby, with comments to show the polynomials.
 
<langsyntaxhighlight lang=ruby>module CRC
# Divisor is a polynomial of degree 32 with coefficients modulo 2.
# We store Divisor in a 33-bit Integer; the polynomial is
Line 2,478 ⟶ 2,527:
 
printf "0x%08x\n", CRC.crc32("The quick brown fox jumps over the lazy dog")
# => 0x414fa339</langsyntaxhighlight>
 
=={{header|Rust}}==
This does not perform any caching of the lookup table for simplicity.
<langsyntaxhighlight lang=rust>
fn crc32_compute_table() -> [u32; 256] {
let mut crc32_table = [0; 256];
Line 2,509 ⟶ 2,558:
println!("{:x}", crc32("The quick brown fox jumps over the lazy dog"));
}
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 2,517 ⟶ 2,566:
=={{header|Scala}}==
{{trans|Java}}
<langsyntaxhighlight lang=scala>import java.util.zip.CRC32
val crc=new CRC32
crc.update("The quick brown fox jumps over the lazy dog".getBytes)
println(crc.getValue.toHexString) //> 414fa339</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "crc32.s7i";
 
Line 2,529 ⟶ 2,578:
begin
writeln(ord(crc32("The quick brown fox jumps over the lazy dog")) radix 16 lpad0 8);
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,538 ⟶ 2,587:
=={{header|Shell}}==
===Bash===
<langsyntaxhighlight lang=Bash>#!/usr/bin/env bash
declare -i -a CRC32_LOOKUP_TABLE
 
Line 2,569 ⟶ 2,618:
# crc32_string "The quick brown fox jumps over the lazy dog"
# yields 414fa339
</syntaxhighlight>
</lang>
===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>.
<langsyntaxhighlight lang=bash>#!/usr/bin/env sh
# POSIX Shell CRC32 of string
# @Name: crc32.sh
Line 2,625 ⟶ 2,674:
 
# crc32_string "The quick brown fox jumps over the lazy dog"
# yields 414fa339</langsyntaxhighlight>
{{out}}
<pre>bash ./crc32.sh "The quick brown fox jumps over the lazy dog"
Line 2,634 ⟶ 2,683:
{{works with|Smalltalk/X}}
the CRC32Stream utility class can do it for me:
<langsyntaxhighlight lang=smalltalk>CRC32Stream hashValueOf:'The quick brown fox jumps over the lazy dog'</langsyntaxhighlight>
{{out}}
1095738169 "which is 16r414FA339"
Line 2,640 ⟶ 2,689:
=={{header|Swift}}==
Using the zlib crc32 function available to Swift from libz.dylib.
<langsyntaxhighlight lang=Swift>import Foundation
 
let strData = "The quick brown fox jumps over the lazy dog".dataUsingEncoding(NSUTF8StringEncoding,
Line 2,646 ⟶ 2,695:
let crc = crc32(uLong(0), UnsafePointer<Bytef>(strData!.bytes), uInt(strData!.length))
 
println(NSString(format:"%2X", crc))</langsyntaxhighlight>
{{out}}
<pre>414FA339
Line 2,652 ⟶ 2,701:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang=tcl>package require Tcl 8.6
 
set data "The quick brown fox jumps over the lazy dog"
puts [format "%x" [zlib crc32 $data]]</langsyntaxhighlight>
{{out}}
<pre>414fa339</pre>
Line 2,661 ⟶ 2,710:
Alternatively, with older versions of Tcl:
{{tcllib|crc32}}
<langsyntaxhighlight lang=tcl>package require crc32
puts [format "%x" [crc::crc32 $data]]</langsyntaxhighlight>
With the same input data, it produces identical output.
 
=={{header|TXR}}==
===Standard Library===
<langsyntaxhighlight lang=txrlisp>(crc32 "The quick brown fox jumps over the lazy dog")</langsyntaxhighlight>
{{out}}
<pre>1095738169</pre>
===FFI access to Zlib===
<langsyntaxhighlight lang=txrlisp>(with-dyn-lib "libz.so.1"
(deffi zlib-crc32 "crc32" ulong (ulong str uint)))</langsyntaxhighlight>
{{out}}
<pre>$ txr -i crc32-zlib.tl
Line 2,680 ⟶ 2,729:
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===
<langsyntaxhighlight lang=txrlisp>(defvarl crc-tab
#(#x00000000 #x77073096 #xee0e612c #x990951ba #x076dc419 #x706af48f
#xe963a535 #x9e6495a3 #x0edb8832 #x79dcb8a4 #xe0d5e91e #x97d2d988
Line 2,731 ⟶ 2,780:
(set crc (logxor [crc-tab (logand (logxor crc [buf i]) #xff)]
(ash crc -8))))
(logxor crc #xffffffff)))</langsyntaxhighlight>
 
{{out}}
Line 2,741 ⟶ 2,790:
=={{header|Vala}}==
===Library===
<langsyntaxhighlight lang=vala>using ZLib.Utility;
 
void main() {
var str = (uint8[])"The quick brown fox jumps over the lazy dog".to_utf8();
stdout.printf("%lx\n", crc32(0, str));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,752 ⟶ 2,801:
</pre>
===Implementation===
<langsyntaxhighlight lang=vala>public class Crc32
{
private const uint32 s_generator = 0xedb88320u;
Line 2,788 ⟶ 2,837:
var crc32 = new Crc32();
stdout.printf("%x\n", crc32.get("The quick brown fox jumps over the lazy dog"));
}</langsyntaxhighlight>
 
{{out}}
Line 2,796 ⟶ 2,845:
 
=={{header|VAX Assembly}}==
<langsyntaxhighlight lang=VAX Assembly> EDB88320 0000 1 poly: .long ^xedb88320 ;crc32
00000044 0004 2 table: .blkl 16
0044 3
Line 2,819 ⟶ 2,868:
67 6F 64 20 79 7A 61 00BC
0000002B 00C3 18 len = .-msg
00C3 19 .end crc</langsyntaxhighlight>
 
=={{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,829 ⟶ 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.
<langsyntaxhighlight lang=vb>Option Explicit
Declare Function RtlComputeCrc32 Lib "ntdll.dll" _
(ByVal dwInitial As Long, pData As Any, ByVal iLen As Long) As Long
Line 2,843 ⟶ 2,933:
Debug.Assert l = &H414FA339
 
End Sub</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
Allows the resumption of calculations, useful for processing a large file with a series of buffer reads.
<langsyntaxhighlight lang=vbnet>Public Class Crc32
 
' Table for pre-calculated values.
Line 2,875 ⟶ 2,965:
End Function
 
End Class</langsyntaxhighlight>
Test:
<langsyntaxhighlight lang=vbnet> ' Returns a Byte Array from a string of ASCII characters.
Function Str2BA(Str As String) As Byte()
Return System.Text.Encoding.ASCII.GetBytes(Str)
Line 2,899 ⟶ 2,989:
Crc32.cs(Str2BA(Mid(Str, 1, 20)))
Debug.Print("Crc32 = " & HexF(Crc32.cs(Str2BA(Mid(Str, 21)), False), 8))
End Sub</langsyntaxhighlight>
Output:
<syntaxhighlight lang=text>Input = "The quick brown fox jumps over the lazy dog"
Crc32 = 414FA339
Crc32 = 414FA339</langsyntaxhighlight>
 
=={{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}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv
 
class CRC32 {
Line 2,939 ⟶ 3,044:
CRC32.init()
var crc = CRC32.compute("The quick brown fox jumps over the lazy dog")
System.print(Conv.hex(crc))</langsyntaxhighlight>
 
{{out}}
Line 2,947 ⟶ 3,052:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>code HexOut=27; \intrinsic routine
string 0; \use zero-terminated strings
 
Line 2,965 ⟶ 3,070:
];
 
HexOut(0, CRC32("The quick brown fox jumps over the lazy dog", 43))</langsyntaxhighlight>
 
{{out}}
Line 2,974 ⟶ 3,079:
=={{header|zkl}}==
Using zlib:
<langsyntaxhighlight lang=zkl>var [const] ZLib=Import("zeelib");
ZLib.calcCRC32(Data(Void,"The quick brown fox jumps over the lazy dog"));
//-->0x414fa339</langsyntaxhighlight>
 
=={{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>
1,471

edits