FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Others/CRC32.java at master · primary-code-test/Java · GitHub
primary-code-test
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Others
/
CRC32.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (22 loc) · 776 Bytes
Breadcrumbs
Java
/
Others
/
CRC32.java
Copy path
File metadata and controls
27 lines (22 loc) · 776 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package
Others
;
import
java
.
util
.
BitSet
;
/** Generates a crc32 checksum for a given string or byte array */
public
class
CRC32
{
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
Integer
.
toHexString
(
crc32
(
"Hello World"
)));
}
public
static
int
crc32
(
String
str
) {
return
crc32
(
str
.
getBytes
());
}
public
static
int
crc32
(
byte
[]
data
) {
BitSet
bitSet
=
BitSet
.
valueOf
(
data
);
int
crc32
=
0xFFFFFFFF
;
// initial value
for
(
int
i
=
0
;
i
<
data
.
length
*
8
;
i
++) {
if
(((
crc32
>>>
31
) &
1
) != (
bitSet
.
get
(
i
) ?
1
:
0
))
crc32
= (
crc32
<<
1
) ^
0x04C11DB7
;
// xor with polynomial
else
crc32
= (
crc32
<<
1
);
}
crc32
=
Integer
.
reverse
(
crc32
);
// result reflect
return
crc32
^
0xFFFFFFFF
;
// final xor value
}
}
Back
|
FazBrowse Home
|
New Git URL