将int和字符串转换为char*。

时间:2022-11-03 16:23:51

This is a little hard I can't figure it out.

这有点难,我搞不清楚。

I have an int and a string that I need to store it as a char*, the int must be in hex

我有一个int和一个字符串,我需要将它存储为char*, int必须在十六进制中

i.e.

即。

int a = 31;
string str = "a number";

I need to put both separate by a tab into a char*.

我需要把两个标签分开放到一个char*中。

Output should be like this:

输出应如下:

1F      a number

5 个解决方案

#1


20  

With appropriate includes:

用适当的包括:

#include <sstream>
#include <ostream>
#include <iomanip>

Something like this:

是这样的:

std::ostringstream oss;
oss << std::hex << a << '\t' << str << '\n';

Copy the result from:

复制的结果:

oss.str().c_str()

Note that the result of c_str is a temporary(!) const char* so if your function takes char * you will need to allocate a mutable copy somewhere. (Perhaps copy it to a std::vector<char>.)

注意,c_str的结果是一个临时的(!)const char*,因此,如果函数接受char*,则需要在某个地方分配一个可变副本。(或者复制到std::vector )

#2


4  

Try this:

试试这个:

int myInt = 31;
const char* myString = "a number";
std::string stdString = "a number";

char myString[100];

// from const char*
sprintf(myString, "%x\t%s", myInt, myString);

// from std::string   :)
sprintf(myString, "%x\t%s", myInt, stdString.c_str());

#3


2  

#include <stdio.h>

char display_string[200];
sprintf(display_string,"%X\t%s",a,str.c_str());

I've used sprintf to format your number as a hexadecimal.

我用sprintf将数字格式化为十六进制。

#4


1  

str.c_str() will return a null-terminated C-string.

string. c_str()将返回一个以null结尾的C-string。

Note: not answering the main question since your comment indicated it wasn't necessary.

注意:没有回答主要问题,因为你的评论表明没有必要。

#5


0  

those who write "const char* myString = "a number";" are just lousy programmers. Being not able to get the C basics - they rush into C++ and start speaking about the things they just don't understand.

那些写“const char* myString =”a number“的人都是差劲的程序员。由于无法获得C基础知识,他们匆忙地进入c++,开始谈论他们不理解的东西。

"const char *" type is a pointer. "a number" - is array. You mix pointers and arrays. Yes, C++ compilers sometimes allow duct typing. But you must also understand - if you do duct typing not understanding where your "ductivity" is - all your program is just a duct tape.

“const char *”类型是一个指针。“一个数字”-是数组。混合指针和数组。是的,c++编译器有时允许管道类型。但是你也必须明白——如果你做管道输入而不知道你的“管道性”在哪里——你的程序只是管道胶带。

#1


20  

With appropriate includes:

用适当的包括:

#include <sstream>
#include <ostream>
#include <iomanip>

Something like this:

是这样的:

std::ostringstream oss;
oss << std::hex << a << '\t' << str << '\n';

Copy the result from:

复制的结果:

oss.str().c_str()

Note that the result of c_str is a temporary(!) const char* so if your function takes char * you will need to allocate a mutable copy somewhere. (Perhaps copy it to a std::vector<char>.)

注意,c_str的结果是一个临时的(!)const char*,因此,如果函数接受char*,则需要在某个地方分配一个可变副本。(或者复制到std::vector )

#2


4  

Try this:

试试这个:

int myInt = 31;
const char* myString = "a number";
std::string stdString = "a number";

char myString[100];

// from const char*
sprintf(myString, "%x\t%s", myInt, myString);

// from std::string   :)
sprintf(myString, "%x\t%s", myInt, stdString.c_str());

#3


2  

#include <stdio.h>

char display_string[200];
sprintf(display_string,"%X\t%s",a,str.c_str());

I've used sprintf to format your number as a hexadecimal.

我用sprintf将数字格式化为十六进制。

#4


1  

str.c_str() will return a null-terminated C-string.

string. c_str()将返回一个以null结尾的C-string。

Note: not answering the main question since your comment indicated it wasn't necessary.

注意:没有回答主要问题,因为你的评论表明没有必要。

#5


0  

those who write "const char* myString = "a number";" are just lousy programmers. Being not able to get the C basics - they rush into C++ and start speaking about the things they just don't understand.

那些写“const char* myString =”a number“的人都是差劲的程序员。由于无法获得C基础知识,他们匆忙地进入c++,开始谈论他们不理解的东西。

"const char *" type is a pointer. "a number" - is array. You mix pointers and arrays. Yes, C++ compilers sometimes allow duct typing. But you must also understand - if you do duct typing not understanding where your "ductivity" is - all your program is just a duct tape.

“const char *”类型是一个指针。“一个数字”-是数组。混合指针和数组。是的,c++编译器有时允许管道类型。但是你也必须明白——如果你做管道输入而不知道你的“管道性”在哪里——你的程序只是管道胶带。