This module contains commands and constants for evaluating the CRC checksum of a file in the disk or a piece of data in the memory.
Used to get the CRC checksum of a FolderItem or a MemoryBlock.
Syntax
Result = ToringoCRC.Checksum(File, ByRef ReturnValue[, Mode])
OR
Result = ToringoCRC.Checksum(MB, ByRef ReturnValue[, Mode])
| Part |
Type |
Description |
|---|---|---|
Result |
Integer |
Returns an integer to determine the reason of failure or state of execution. You can compare it with the following values (called Result Code): 0 - Success, ReturnValue returns the correct checksum. 1 - File or MB is empty or inaccessible, ReturnValue returns ""(an empty string). 5 - Value assigned to parameter Mode is invalid; checksum has been calculated in CRC 32 mode and stored in ReturnValue . 7 - Other error occurred, usually you won't get this result, and generally ReturnValue returns "". |
ReturnValue |
String |
Standard CRC checksum returned as a hexadecimal string. |
File |
FolderItem |
The file to get checksum. |
MB |
MemoryBlock |
Data stored in MemoryBlock to get checksum. Since REALbasic converts String values to MemoryBlocks automatically, you can pass a string for this parameter as well, directly. |
Mode |
Integer |
Determines to calculate checksum in CRC 16 or CRC 32 mode. The default mode is CRC 32. Please set this parameter by passing Mode constants. |
Notes
If you want to get the CRC checksum of a file, please make sure that the file exists and is readable, and you have proper access privileges.
Besides, the File specified must be a single file. Checksum cannot handle folders, so if you passes a folder, Result will return 1. Especially, on Mac OS X, a FolderItem refers to not only a file or a regular folder, but also a bundle (Although bundles are special folders, them are treated as single files by Finder). Since a bundle is a folder actually, Checksum method cannot be used to evaluate its CRC value.
Also, this method isn't designed to handle very big disk files fast. In theory, it can only handle files less than 4 GB, which is also limited by other factors. Reading data from files will cost more time than calculating CRC, so executing speed mainly depends on the disk and file system you use, and other system factors.
Checksum method can also be used to calculate the checksum of MemoryBlock, so you needn't save data to files before calling this method. For data with other types, please populate them to MemoryBlock before calculating the checksum. See the second example in the Example section.
Example
This example gets the CRC 32 checksum of the application file currently running.
| Dim CRC32Value As String Dim ResultCode As Integer ResultCode = ToringoCRC. Checksum (App.ExecutableFile,CRC32Value) If ResultCode = 0 Then // get checksum successfully MsgBox CRC32Value End If |
This example gets the CRC 16 value of some information with different data types stored in a MemoryBlock.
| Dim FirstName As String = "Hopkins" Dim Age As Int32 = 35 // 4 bytes Dim HeightInMeter As Double = 1.84 // 8 bytes Dim PersionInfo As MemoryBlock Dim CRC16Value As String Dim ResultCode As Integer PersionInfo = NewMemoryBlock(LenB(FirstName)+4+8) PersionInfo.StringValue(0,LenB(FirstName)) = FirstName PersionInfo.Int32Value(LenB(FirstName)) = Age PersionInfo.DoubleValue(LenB(FirstName)+4) = HeightInMeter ResultCode = ToringoCRC. Checksum (PersionInfo,CRC16Value,ToringoCRC.CRC16) If ResultCode = 0 Then // get checksum successfully MsgBox CRC16Value // you should see "2714" End If |
For more examples, please see "CRC_Tool.rbp" in Examples folder.
See Also
Mode Constants of ToringoCRC Module.
Other keywords mentioned are built-in classes of REALbasic, please refer to their entries in REALbasic's Language Reference.
These module constants are used to determine the mode for calculating CRC checksum.
Constant
Use the following contants to set the Mode parameter of Checksum method.
| Name |
Value |
Description |
|---|---|---|
CRC16 |
16 |
Uses standard CRC 16 algorithm to calculate the checksum. Parameter ReturnValue of Checksum method will return the result as a string with 4 character length. |
CRC32 |
32 |
Uses standard CRC 32 algorithm to calculate the checksum. Parameter ReturnValue of Checksum method will return the result as a string with 8 character length. |
Notes
We recommend that you use the name of a constant to set the Mode parameter of Checksum method, instead of using the value of any constant directly. The scope of those constants is the module, so "ToringoCRC." should be added to them as prefix when using.
Example
Please refer to the Example section of Checksum method.
