当前位置:网站首页>将字符串转为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;
边栏推荐
- Successfully rated Tencent t3-2, 10000 word parsing
- C language structure
- One line of code to implement shell if else logic
- [Title brushing] Super washing machine
- Octopus network progress monthly report | may 1-May 31, 2022
- Installation of pagoda
- How to adapt the page size when iframe is embedded in a web page
- 单向环形链表实现约瑟夫环
- VGA display color bar and picture (FPGA)
- Pytorch framework
猜你喜欢

How to adapt the page size when iframe is embedded in a web page

torch_ About the geometric Mini batch

Actual combat | realizing monocular camera ranging by skillfully using pose solution

IC chip scheme fs4062b for lithium battery charging with 5V boost to 12.6V

Volume mount and mirror creation

Build an embedded system software development environment - build a cross compilation environment

Overview of embedded system 2- composition and application of embedded system

镜像扫描工具预研

imagemagick:a gentle introduction to magick++

Implementing tensorflow deep learning framework similarflow with numpy
随机推荐
verilog-mode的简要介绍
Wechat web developer tools tutorial, web development issues
Realization of Joseph Ring with one-way ring linked list
单向环形链表实现约瑟夫环
Getting started with NVIDIA Jetson nano Developer Kit
章鱼网络进展月报 | 2022.5.1-5.31
"Non" reliability of TCP
[cloud native | kubernetes] actual combat of ingress case
Resume NFT platform trustrecruit joined Octopus network as a candidate application chain
在 Debian 10 上独立安装MySQL数据库
Computational hierarchy -- the problem of large numbers multiplying decimals
基于华为云鲲鹏弹性云服务器ECS部署openGauss数据库【这次高斯不是数学家】
C language array and pointer
VGA display color bar and picture (FPGA)
Openstack network
C language [23] classic interview questions [2]
Innovation training (XI) summary of some bugs in the development process
Symbolic constant, const qualifier
NVIDIA Jetson Nano Developer Kit 入门
R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting bar plot, adding significance level and jitter points