1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-30 19:46:56 +00:00
transmission/macosx/NSDataAdditions.mm
2023-02-11 14:49:42 -06:00

33 lines
1.1 KiB
Text

// This file Copyright © 2022-2023 Transmission authors and contributors.
// It may be used under the MIT (SPDX: MIT) license.
// License text can be found in the licenses/ folder.
#import "NSDataAdditions.h"
@implementation NSData (Additions)
// hexChars from Peter, Aug 19 '14: https://stackoverflow.com/a/25378464
- (NSString*)hexString
{
char const* hexChars = "0123456789ABCDEF";
NSUInteger length = self.length;
unsigned char const* bytes = (unsigned char const*)self.bytes;
char* chars = (char*)malloc(length * 2);
if (chars == NULL)
{
// malloc returns null if attempting to allocate more memory than the system can provide. Thanks Cœur
[NSException raise:@"NSInternalInconsistencyException" format:@"failed malloc" arguments:nil];
return nil;
}
char* s = chars;
NSUInteger i = length;
while (i--)
{
*s++ = hexChars[*bytes >> 4];
*s++ = hexChars[*bytes & 0xF];
bytes++;
}
return [[NSString alloc] initWithBytesNoCopy:chars length:length * 2 encoding:NSASCIIStringEncoding freeWhenDone:YES];
}
@end