gpt4 book ai didi

c++ - 如何将 int 转换为 ASCII char C++

转载 作者:行者123 更新时间:2023-11-28 07:39:19 25 4
gpt4 key购买 nike

我必须将 int 转换为 ASCII 字符串。基本上,我想要这个:

    int iTest = 128;
char ca[3];
??? --> **SOLUTION :** sprintf(ca,"%d",iTest);

结果:

 ca[0] = 0x31;--> Hexcode of '1'
ca[1] = 0x32;
ca[2] = 0x38;

最佳答案

使用std::stringstd::to_string :

#include <string>
#include <iostream>

using namespace std;

...

int iTest = 128;

string ca = to_string(iTest);

for (int i = 0; i < ca.size(); i++)
{
cout << hex << "0x" << (int) ca[i] << endl;
}

结果:

0x31
0x32
0x38

关于c++ - 如何将 int 转换为 ASCII char C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16145293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com