C 语言中利用单引号表示整数

背景 在进行与竟品的性能对比中,需要知道竟品的部分参数,与竟品对齐参数后再进行性能测试。 在 hook TikTok 的摄像头输出参数时发现,PixelFormatType 的值是整数 875704422,这样的一串整数很难知道其含义。 分析 从 key PixelFormatType 可以猜出是 kCVPixelFormatType 枚举中的一个,kCVPixelFormatType 枚举中大部分的值是以 '420f' 这样的单引号形式来表示,通过逐一对比知道,原来 875704422 对应的就是 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange 的值,也就是 '420f'。 那么,为什么 875704422 == '420f' ? 原来,可以通过 ascii 编码进行转换,以 '420f' 为例: 4/2/0/f 的 ascii 编码分别为 52/50/48/102 ,转换为二进制分别为 00110100/00110010/00110000/01100110,合并起来 00110100001100100011000001100110 转换为十进制,刚好就是 875704422。 为方便看出对应的字符串,写了以下转换算法: 转换算法 NSString * changeToTypeStr(int value) { char *str = malloc(10); int count = 0; while (value > 0) { char ch = (char)(value & 0xFF); str[count] = ch; value = value >> 8; count ++; } if (count > 1) { int left = 0, right = count - 1; while (left < right) { char tmp = str[left]; str[left] = str[right]; str[right] = tmp; left ++; right --; } } str[count] = '\0'; NSString *res = [[NSString alloc] initWithUTF8String:str]; free(str); return res; }

September 1, 2021 · Darren Ou