更新时间:2022-10-15 09:54:08
// strBusi code = @64-37-81-d4-39-6d
NSArray的* tmp_arr = [strBusi code componentsSeparatedByString:@ - ];
NSMutableData * commandToSend = [[NSMutableData的alloc]初始化];
unsigned char型whole_byte;
CHAR byte_chars [3] = {'\\ 0','\\ 0','\\ 0'};
INT I;
对于(I = 0; I&下; [tmp_arr计数];我++){
byte_chars [0] = [[tmp_arr objectAtIndex:ⅰ] characterAtIndex:0];
byte_chars [1] = [[tmp_arr objectAtIndex:ⅰ] characterAtIndex:1];
whole_byte =与strtol(byte_chars,NULL,16);
[commandToSend对appendBytes:放大器; whole_byte长度:1];
}
返回commandToSend;
这commandToSend然后被转换为base64数据。
I have a piece of code in vb. I need to convert array of bytes to base 64 string. Following is the vb code.
If arrLicence.Count > 0 Then
LicenceBytes = CType(Array.CreateInstance(GetType(Byte),6), Byte())
LicenceBytes(0) = Convert.ToByte(arrLicence(0).ToString(), 16) LicenceBytes(1) = Convert.ToByte(arrLicence(1).ToString(), 16) LicenceBytes(2) = Convert.ToByte(arrLicence(2).ToString(), 16) LicenceBytes(3) = Convert.ToByte(arrLicence(3).ToString(), 16) LicenceBytes(4) = Convert.ToByte(arrLicence(4).ToString(), 16) LicenceBytes(5) = Convert.ToByte(arrLicence(5).ToString(), 16) LicenceString = Convert.ToBase64String(LicenceBytes) '6 byteArray - passed by the user - Base64Encoded
I need its equivalent in iphone. I tried with NSData and base64 convertion but result defers.
I have used this link for conversion. http://www.cocoadev.com/index.pl?BaseSixtyFour
This is really urgent and need to solve asap. I tried by creating individual bytes using memcpy
and then creating an array but with no success.
What I have tried is as follows:
NSData *d1 =[@"64" dataUsingEncoding:NSUTF16StringEncoding];
NSData *d2 = [@"37" dataUsingEncoding:NSUTF16StringEncoding];
NSData *d3 = [@"81" dataUsingEncoding:NSUTF16StringEncoding];
NSData *d4 = [@"d4" dataUsingEncoding:NSUTF16StringEncoding];
unsigned char *buffer = (unsigned char*)malloc(8);
buffer[0] = [d1 bytes] ;
buffer[1] = [d2 bytes] ;
buffer[2] = [d3 bytes] ;
buffer[3] = [d4 bytes] ;
NSData *data = [NSData dataWithBytes:buffer length:4];
NSString *str = [self encodeBase64WithData:data];
free(buffer);
This results in IJCgkA==
while code in .NET returns ZDeB1A==
Please note that the conversion is for first four bytes of arrLicence and the input is 64, 37, 81, d4
//strBusiCode = @"64-37-81-d4-39-6d";
NSArray *tmp_arr = [strBusiCode componentsSeparatedByString:@"-"];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [tmp_arr count]; i++) {
byte_chars[0] = [[tmp_arr objectAtIndex:i] characterAtIndex:0];
byte_chars[1] = [[tmp_arr objectAtIndex:i] characterAtIndex:1];
whole_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&whole_byte length:1];
}
return commandToSend;
This commandToSend is then converted to base64 data.