当前位置:网站首页>将字符串转为16进制字符串并显示出来
将字符串转为16进制字符串并显示出来
2022-06-12 13:26:00 【Li.CQ】
将字符串转为16进制字符串并显示出来
弹幕协议一
// - 发送的字节
5d0000005d000000b1020000xxxxxxx00
5d000000 : 发送内容长度 (内容长度 = (5d000000 内容长度 4字节) + (b1020000 版本信息长度 4字节) (xxxxxxx)的长度 + (00 消息尾 1字节) )
5d000000 : 发送的内容长度 (内容长度 = (5d000000 内容长度 4字节) + (b1020000 版本信息长度 4字节) (xxxxxxx)的长度 + (00 消息尾 1字节) )
b1020000 : 版本信息字符 (b1020000 4字节)
xxxxxxx : 发送的内容;
00 : 尾部的固定字符
// - 字符串转data
NSData *data = [self.msgContent dataUsingEncoding:NSUTF8StringEncoding];
NSMutableString *content = [NSMutableString string];
// - 里边的固定字符
char tail = 0x00;
int version = 0x000002b1;
int length = (int)(sizeof(length) + sizeof(version) + data.length + sizeof(tail));
// - 字节序转换
tail = CFSwapInt32(tail);
version = CFSwapInt32(version);
length = CFSwapInt32(length);
// - 将数字拼接到字符串中(以16进制的形式, %08x 的作用是如果数字为 0x08, 就改为 0x00000008)
NSString *header = [NSString stringWithFormat:@"%08x%08x%08x", length, length, version];
// - 将bytes中的每个字符拼接到字符串中(以16进制的形式, %08x 的作用是如果数字为 0x08, 就改为 0x00000008)
// - 这里的用 unsigned char 而不用 char 是有原因的 例如, "我"的编码为 E6 88 91, 使用char 会变为 FFFFFFE6 FFFFFF88 FFFFFF91, 而使用 unsigned char 会变为 E68891 [资料地址](https://www.geek-share.com/detail/2764755658.html)
for (int i = 0; i < data.length; i++) {
unsigned char va = *(char *)(data.bytes + i);
[content appendFormat:@"%02x", va];
}
// - 拼接结果字符串
NSString *result = [NSString stringWithFormat:@"%@%@%02x", header, content, tail];
return result;
弹幕协议二
NSDictionary *contentDic = [self.msgContent toDictionary];
if (!contentDic) return nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:contentDic options:NSJSONWritingPrettyPrinted error:nil];
NSMutableString *content = [NSMutableString string];
int headerLenth = 16;
int contentLen = CFSwapInt32LittleToHost((int)(data.length + headerLenth));
short headerLen = CFSwapInt16LittleToHost(headerLenth);
short version = CFSwapInt16LittleToHost(1);
int operation = CFSwapInt32LittleToHost(self.operationCode);
int seq = CFSwapInt32LittleToHost(0);
NSString *header = [NSString stringWithFormat:@"%08x%04x%04x%08x%08x", contentLen, headerLen, version, operation, seq];
for (int i = 0; i < data.length; i++) {
unsigned char va = *(char *)(data.bytes + i);
[content appendFormat:@"%02x", va];
}
NSString *result = [NSString stringWithFormat:@"%@%@", header, content];
return result;
边栏推荐
- How to adapt the page size when iframe is embedded in a web page
- Further understanding of the network
- JSP jump problem, unable to display database data, and unable to jump
- Embedded system hardware composition - embedded system hardware architecture
- 颜色编码格式介绍
- Multi source BFS problem template (with questions)
- IC chip scheme fs4062b for lithium battery charging with 5V boost to 12.6V
- One line of code to implement shell if else logic
- Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
- LeetCode滑动窗口刷题总结
猜你喜欢

leetcode 47. Permutations II full permutations II (medium)

多源BFS问题 模板(附题)

torch_ About the geometric Mini batch

Innovation training (XI) summary of some bugs in the development process

Further understanding of the network

C#DBHelper_FactoryDB_GetConn

list和dict的应用

VGA display color bar and picture (FPGA)

Successfully rated Tencent t3-2, 10000 word parsing

Resume NFT platform trustrecruit joined Octopus network as a candidate application chain
随机推荐
【云原生 | Kubernetes篇】Kubernetes 网络策略(NetworkPolicy)
1414: [17noip popularization group] score
2068: [example 2.6] chicken and rabbit in the same cage
Multi source BFS problem template (with questions)
Computational hierarchy -- the problem of large numbers multiplying decimals
There was an error installing mysql. Follow the link below to CMD
简历 NFT 平台 TrustRecruit 加入章鱼网络成为候选应用链
TCP的“非”可靠性
Application of list and Dict
FFmpeg 学习指南
torch_geometric message passing network
Dameng database DM8 Windows environment installation
Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
Application of short circuit expression (||) in C language
成功跳槽阿里,进阶学习
Getting to know blob objects
m1 pod install pod lint 失败解决方案
AWLive 结构体的使用
442 authors, 100 pages! It took Google 2 years to release the new benchmark big bench | open source
Innovation training (XII) project summary