base64, big-endian, no zeros

My server devs ask me to send them some data encoded with base64 with this rules:

1. big-endian byte order

2. no extra zero bytes

3. base64 string


for example:

10005000 → «mKol»

1234567890 → «SZYC0g»

I did some spaghetti code, and it's work. But maybe somebody have more elegant solution?

+ (NSString*)encodeBigEndianBase64:(uint32_t)value {
   
    char *bytes = (char*) &value;
    int len = sizeof(uint32_t);
   
    char *reverseBytes = malloc(sizeof(char) * len);
    unsigned long index = len - 1;
   
    for (int i = 0; i < len; i++)
        reverseBytes[index--] = bytes[i];
   
    int offset = 0;
   
    while (reverseBytes[offset] == 0) {
        offset++;
    }
   
    NSData *resultData;
   
    if (offset > 0) {
       
        int truncatedLen = (len - offset);
        char *truncateBytes = malloc(sizeof(char) * truncatedLen);
       
        for (int i = 0; i < truncatedLen ; i++)
            truncateBytes[i] = reverseBytes[i + offset];
        resultData = [NSData dataWithBytes:truncateBytes length:truncatedLen];
        free(truncateBytes);
       
    } else {
       
        resultData = [NSData dataWithBytes:reverseBytes length:len];
    }
    free(reverseBytes);
    return [[resultData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength] stringByReplacingOccurrencesOfString:@"=" withString:@""];
   
}
base64, big-endian, no zeros
 
 
Q