iOS Question CRC32 on ios

ldb68

Member
Licensed User
I need help with a crc32 function on IOS

From these link
http://stackoverflow.com/questions/4115059/get-crc-checksum-of-an-nsdata-in-objective-c
http://ios-blog.co.uk/tutorials/quick-tips/quick-tip-converting-nsstring-to-nsdata/

I try this code

B4X:
Dim NativeMe As NativeObject = Me
Log(NativeMe.RunMethod("Crc32_String:", Array ("test")).AsString)

#If OBJC
#import <zlib.h>
- (NSString*) Crc32_String: (NSString*) strdata {

//NSString* strdata = @"1234567890";
NSData* data = [strdata dataUsingEncoding:NSUTF8StringEncoding];
data = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];
unsigned long result = crc32(0, data.bytes, data.length);
NSString *str = [NSString stringWithFormat:@"%qi",result];
return str;
}
#end if
The result is non corretct
 
Last edited:

Erel

Administrator
Staff member
Licensed User
1. Add a reference to iAcrhiver library.
2. Add this objc code:
B4X:
#If OBJC
#import <zlib.h>
- (long long) crc32: (B4IArray*) data {
long long result = crc32(0, data.bytesData.bytes, data.Length);
return result;
}
#end if
Call it with:
B4X:
Dim nativeMe As NativeObject = Me
Dim data() As Byte = "test".GetBytes("UTF8")
Dim res As Long = nativeMe.RunMethod("crc32:", Array(data)).AsNumber
The result is correct.
 

ldb68

Member
Licensed User
1. Add a reference to iAcrhiver library.
2. Add this objc code:
B4X:
#If OBJC
#import <zlib.h>
- (long long) crc32: (B4IArray*) data {
long long result = crc32(0, data.bytesData.bytes, data.Length);
return result;
}
#end if
Call it with:
B4X:
Dim nativeMe As NativeObject = Me
Dim data() As Byte = "test".GetBytes("UTF8")
Dim res As Long = nativeMe.RunMethod("crc32:", Array(data)).AsNumber
The result is correct.
Thank You

Always the best
 
Top