CRC-32: Difference between revisions
m
→{{header|11l}}: `(-)` -> `~`
Thundergnat (talk | contribs) m (fix markup) |
Alextretyak (talk | contribs) m (→{{header|11l}}: `(-)` -> `~`) |
||
(10 intermediate revisions by 9 users not shown) | |||
Line 33:
F crc32(buf, =crc = UInt32(0))
crc =
L(k) buf
crc = (crc >> 8) (+) :crc_table[(crc [&] F'F) (+) k.code]
R
print(hex(crc32(‘The quick brown fox jumps over the lazy dog’)))</syntaxhighlight>
Line 388:
{{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}}==
Line 543 ⟶ 559:
=={{header|C++}}==
<syntaxhighlight lang=cpp>#include <
#include <
#include <cstdint>
#include <numeric>
#include <concepts>
#include <algorithm>
// These headers are only needed for main(), to demonstrate.
#include <iomanip>
#include <iostream>
#include <
inline constexpr auto crc_table = []() {
std::array<std::
std::generate(retval.begin(), retval.end(),
[n = std::uint32_t{ 0 }]() mutable {
auto c = n++;
for (std::uint8_t k = 0; k < 8; ++k) {
if (c & 1) c = std::uint32_t{ 0xedb88320 } ^ (c >> 1);
else c >>= 1;
}
return c;
return retval;
}();
[[nodiscard]] constexpr std::uint32_t crc(const std::ranges::input_range auto& rng)
noexcept requires std::convertible_to<std::ranges::range_value_t<decltype(rng)>, std::uint8_t> {
return ~std::accumulate(std::ranges::begin(rng), std::ranges::end(rng),
~std::uint32_t{ 0 } & std::uint32_t{ 0xff'ff'ff'ffu },
[](std::uint32_t checksum, std::uint8_t value)
{ return crc_table[(checksum ^ value) & 0xff] ^ (checksum >> 8); });
}
int main() {
constexpr std::string_view str = "The quick brown fox jumps over the lazy dog";
std::cout << std::hex << std::setw(8) << std::setfill('0') << crc(
}</syntaxhighlight>
{{out}}
<pre>
Line 1,197 ⟶ 1,191:
=={{header|Java}}==
<syntaxhighlight lang=Java>
import java.util.zip.CRC32;
</syntaxhighlight>
<syntaxhighlight lang=Java>
public static void main(String[] args) throws IOException {
String string = "The quick brown fox jumps over the lazy dog";
CRC32 crc = new CRC32();
crc.update(string.getBytes());
System.out.printf("%x", crc.getValue());
}
</syntaxhighlight>
{{out}}
<pre>
414fa339
</pre>
=={{header|JavaScript}}==
Line 1,822 ⟶ 1,819:
printf("%0x\n", crc32(0, s, #s))
</syntaxhighlight>
Output:
<pre>414fa339</pre>
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang=pascal>
Program CheckCRC;
{$IFDEF fpc}{$mode Delphi}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils,crc;
function CrcString(const mystring: string) : longword;
var
crcvalue: longword;
begin
crcvalue := crc32(0,nil,0);
result := crc32(crcvalue, @mystring[1], length(mystring));
end;
var
mytext: string;
begin
myText := 'The quick brown fox jumps over the lazy dog';
writeln('crc32 = ', IntToHex(CrcString(mytext), 8));
end.</syntaxhighlight>
Output:
<pre>crc32 = 414FA339</pre>
=={{header|Perl}}==
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,945 ⟶ 2,994:
Crc32 = 414FA339
Crc32 = 414FA339</syntaxhighlight>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import hash.crc32
fn main() {
text := "The quick brown fox jumps over the lazy dog"
result := crc32.sum(text.bytes())
println(result.hex())
}
</syntaxhighlight>
{{out}}
<pre>
414fa339
</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=
class CRC32 {
Line 3,018 ⟶ 3,082:
ZLib.calcCRC32(Data(Void,"The quick brown fox jumps over the lazy dog"));
//-->0x414fa339</syntaxhighlight>
=={{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>
|