We are facing problem while creating compressed file at iOS Device Document Directory, .tgz file is in Hex string transferring from pin-pad device to iPad iOS App at TCP socket layer. We used below HexToString function to convert that hex string and make file with .tgz. but at the end file is corrupted.

Can anyone please help us here, how to create compress file at iOS level with below hex string ? Please suggest us any code changes.

Note :- we had tried multiple NSStringEncoding technique, like ASCII, Unicode, Utf8, etc.

HEX String:-

1F8B08003058A8620203EDCA3B0A80301045D1594A5660265FB7E036065422A8453282CB57B4B2B112419CD3DCE2BD6966DD8F54925E4A975B62D22551EE741A2A5E199E80BBE8F1681DFDA5270BC6DB60D1398735A0092E0650082F580A53566A6F36F7BFFBFDA39A01841042FCD0062C8057FA00080000

we are using Xcode Version:13.1 and IOS Version 15.1 and above.

//Below function we used for creating .tgz file
//fileName here is abc.tgz which is compress file type
//content here is hex string mention aboved

+ (void)writeToLogFile:(NSString*)content fileName:(NSString*)fileNameString{
    content = [NSString stringWithFormat:@"%@",content];
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:fileNameString];

    NSData *fileOriginalString = [self HextoString:content];
    NSData *fileData = [fileOriginalString dataUsingEncoding:NSASCIIStringEncoding];

    ***//In alternative we also tried direct hex string to NSData type by calling below commentented method but it still failing
    //NSData *fileData = [self dataFromHexString:content];***


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSError *error = nil;
        [fileData writeToFile:fileName options:NSDataWritingAtomic error:&error];
        NSLog(@"Write returned error: %@", [error localizedDescription]);
    });
}

//Below function we used for Hex to String conversion

+(NSString*)HextoString:(NSString*)string{
   @try{
      NSMutableString * StrResult = [[NSMutableString alloc] init];
      int i = 0;
      while (i < [string length]){
         NSString * hexChar = [string substringWithRange: NSMakeRange(i, 2)];
         int value = 0;
         sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
         [StrResult appendFormat:@"%c", (char)value];
         i+=2;
      }
      return StrResult;
   }
   @catch (NSException *exception){
      [AELoggerManager info:[NSString stringWithFormat:@" %s EXCEPTION ::%@",__FUNCTION__,exception]];
   }
}

+ (NSData *)dataFromHexString:(NSString *) string {
    if([string length] % 2 == 1){
        string = [@"0"stringByAppendingString:string];
    }

    const char *chars = [string UTF8String];
    int i = 0, len = (int)[string length];

    NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
    char byteChars[3] = {'\0','\0','\0'};
    unsigned long wholeByte;

    while (i < len) {
        byteChars[0] = chars[i++];
        byteChars[1] = chars[i++];
        wholeByte = strtoul(byteChars, NULL, 16);
        [data appendBytes:&wholeByte length:2];
    }
    return data;

}

0

There are 0 best solutions below