当前位置:网站首页>将字符串转为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;
边栏推荐
- Install MySQL database independently on Debian 10
- torch_ geometric message passing network
- Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题
- It is enough to read this article. Web Chinese development
- 创新实训(十二)项目总结
- C language implementation of string and memory library functions
- Teach you how to create SSM project structure in idea
- [cloud native | kubernetes] learn more about ingress
- TCP的“非”可靠性
- 成功跳槽阿里,进阶学习
猜你喜欢
Overview of embedded system 2- composition and application of embedded system
Resume NFT platform trustrecruit joined Octopus network as a candidate application chain
Successful job hopping Ali, advanced learning
创新实训(十一)开发过程中的一些bug汇总
Getting to know blob objects
leetcode 47. Permutations II full permutations II (medium)
[wechat applet development] Part 1: development tool installation and program configuration
章鱼网络进展月报 | 2022.5.1-5.31
Unittest framework
How to adapt the page size when iframe is embedded in a web page
随机推荐
1004: character triangle
Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
2061: [example 1.2] trapezoidal area
Deploy opengauss database based on Huawei cloud Kunpeng elastic ECS [Gauss is not a mathematician this time]
章鱼网络进展月报 | 2022.5.1-5.31
Pre research of image scanning tool
It is enough to read this article. Web Chinese development
Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
【微信小程序开发】第1篇:开发工具安装及程序配置
2064: [example 2.1] exchange value
Resume NFT platform trustrecruit joined Octopus network as a candidate application chain
Chrome debugging tool
基于华为云鲲鹏弹性云服务器ECS部署openGauss数据库【这次高斯不是数学家】
The goods are full. You must take this knowledge
m1 pod install pod lint 失败解决方案
Online picture material
verilog-mode的简要介绍
Stm32f1 and stm32subeide programming example - device driver -dht11 temperature sensor driver
C#DBHelper_ FactoryDB_ GetConn
Successfully rated Tencent t3-2, 10000 word parsing