Create array of bytes in C

Discussion in 'Mac Programming' started by cea, May 27, 2010.

  1. cea macrumors newbie

    Joined:
    Oct 26, 2009
    #1
    Hi,
    I does not have programming background. In the following Obj-C code i create an array of bytes but i must convert bytes into string Hex format to work further. It is not a good idea. So i would like to create an array in C directly without converting to string. How can i do it? Thank you!

    Something like this in Obj-C + C:

    NSData *data = [NSData dataWithContentsOfFile:file];
    const unsigned char *bytes = [data bytes];
    for (int i = 0; i < [bytes count]; i++) {
    unsigned char array = (int)bytes; // Something like this in C
    }
    printf("array: %d", array);


    PHP:
        NSData *data = [NSData dataWithContentsOfFile:file];
        const 
    unsigned char *bytes = [data bytes];
        
    NSMutableArray *array = [NSMutableArray array];
        
        for (
    int i 0< [bytes count]; i++) {
            
    NSString *bytesAsString = [NSString stringWithFormat:@"%02x", (int)bytes[i]];
            [array 
    addObject:bytesAsString];
        }
        
        
    NSLog(@"%@", array);
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    It seems to me that the second line gets an array of bytes. I don't think you're expressing where you need the data to start and what you want to do with it clearly enough.

    -Lee
     
  3. cea thread starter macrumors newbie

    Joined:
    Oct 26, 2009
    #3
    Yes, the secound line gets an array of bytes, then i loop the array to get array components and put them in a new array. I do not know how to store array components in a new array in C.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    What is the C type you want your bytes in?

    -Lee
     

Share This Page