我有11个十六进制的数,如何把他们拼到一起?

林头头03 2019-06-30 03:44:30
如果我有s[11] == {0x5,0xc5,0xb3,0xea.......} 11个数,如何把他们拼接成 05c5b3....的这种形式呢
...全文
294 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-06
  • 打赏
  • 举报
回复
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html
羅昇 2019-11-30
  • 打赏
  • 举报
回复

#include<iostream>
#include<string.h>
using namespace std;
int main() {
    int s[11]{ 0x5, 0xc5, 0xb3, 0xea };
    char str[100];
    for (int i = 0; i < 4; i++) 
        sprintf(str + i * 2, %02x", (usigned char)s[i]);
    printf("%s", str);
    return 0;
}
kingson1111 2019-11-29
  • 打赏
  • 举报
回复
我觉得用个FOR就可以了· test_1 = '易燃易爆炸' bytes = test_1.encode('utf-8')#编码 你们要的十六进制数一长串~~~~巴拉巴拉巴拉什么玩意儿的··· bytes_3 = str(bytes)[2:-1] #去掉头子和尾巴· bytes_4 = [] #建个空列表 for i in bytes_3: if i == '\\' or i == 'x': continue else:bytes_4.append(i) 把东西放进去 bytes_5 = ''.join(bytes_4) #变成一个玩意儿了~ 我是这样的~看大神们了·~
自信男孩 2019-07-03
  • 打赏
  • 举报
回复
引用 1 楼 Italink 的回复:
使用itoa

#include<iostream>
#include<string.h>
using namespace std;
int main() {
int s[11]{ 0x5, 0xc5, 0xb3, 0xea };
char str[100]="";
char tmp[10];
for (int i = 0; i < 4; i++)
sprintf(&str[strlen(str)], "%02s", itoa(s[i], tmp, 16));
printf("%s", str);
return 0;
}

直接用%02x就可以了,使用snprintf和itoa反而把问题复杂化了
自信男孩 2019-07-03
  • 打赏
  • 举报
回复
引用 2 楼 lin5161678 的回复:
[quote=引用 1 楼 Italink 的回复:]
使用itoa

#include<iostream>
#include<string.h>
using namespace std;
int main() {
int s[11]{ 0x5, 0xc5, 0xb3, 0xea };
char str[100]="";
char tmp[10];
for (int i = 0; i < 4; i++)
sprintf(&str[strlen(str)], "%02s", itoa(s[i], tmp, 16));
printf("%s", str);
return 0;
}
既然已经知道sprintf 为什么不直接 %02x
[/quote]
++
直接用0x02x就可以了
YapingXin 2019-07-02
  • 打赏
  • 举报
回复
printf / sprinf
赵4老师 2019-07-01
  • 打赏
  • 举报
回复
#include<iostream>
#include<string.h>
using namespace std;
int main() {
    int s[11]{ 0x5, 0xc5, 0xb3, 0xea };
    char str[100];
    char tmp[10];
    str[0]=0;
    for (int i = 0; i < 4; i++) 
        sprintf(str, "%s%02x",str,(usigned char)s[i]);
    printf("%s", str);
    return 0;
}
轻箬笠 2019-07-01
  • 打赏
  • 举报
回复
引用 2 楼 lin5161678 的回复:
[quote=引用 1 楼 Italink 的回复:]
使用itoa

#include<iostream>
#include<string.h>
using namespace std;
int main() {
int s[11]{ 0x5, 0xc5, 0xb3, 0xea };
char str[100]="";
char tmp[10];
for (int i = 0; i < 4; i++)
sprintf(&str[strlen(str)], "%02s", itoa(s[i], tmp, 16));
printf("%s", str);
return 0;
}
既然已经知道sprintf 为什么不直接 %02x
[/quote]
我也这么觉得,没有用itoa的必要

#include<iostream>
#include<string.h>
using namespace std;
int main() {
int s[] = { 0x5, 0xc5, 0xb3, 0xea };
char str[1024]={};
for (int i = 0; i < ARRAYSIZE(s); i++)
sprintf(&str[strlen(str)], "%02x", s[i]);
printf("%s", str);
return 0;
}
lin5161678 2019-06-30
  • 打赏
  • 举报
回复
引用 1 楼 Italink 的回复:
使用itoa

#include<iostream>
#include<string.h>
using namespace std;
int main() {
int s[11]{ 0x5, 0xc5, 0xb3, 0xea };
char str[100]="";
char tmp[10];
for (int i = 0; i < 4; i++)
sprintf(&str[strlen(str)], "%02s", itoa(s[i], tmp, 16));
printf("%s", str);
return 0;
}
既然已经知道sprintf 为什么不直接 %02x
Italink 2019-06-30
  • 打赏
  • 举报
回复
使用itoa

#include<iostream>
#include<string.h>
using namespace std;
int main() {
int s[11]{ 0x5, 0xc5, 0xb3, 0xea };
char str[100]="";
char tmp[10];
for (int i = 0; i < 4; i++)
sprintf(&str[strlen(str)], "%02s", itoa(s[i], tmp, 16));
printf("%s", str);
return 0;
}

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧