C++ 将 char* 转换为 uint8_t

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25360893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:18:23  来源:igfitidea点击:

Convert char* to uint8_t

c++chardata-conversionuint8t

提问by Evans Belloeil

I transfer message trough a CAN protocol.

通过 CAN 协议传输消息

To do so, the CAN message needs data of uint8_t type. So I need to convert my char* to uint8_t. With my research on this site, I produce this code :

为此,CAN 消息需要 uint8_t 类型的数据。所以我需要将我的 char* 转换为 uint8_t。通过对本网站的研究,我生成了以下代码:

    char* bufferSlidePressure = ui->canDataModifiableTableWidget->item(6,3)->text().toUtf8().data();//My char*

    /* Conversion */
    uint8_t slidePressure [8];
    sscanf(bufferSlidePressure,"%c",
        &slidePressure[0]);

As you may see, my char*must fit in sliderPressure[0].

如您所见,我char*必须适合sliderPressure[0].

My problem is that even if I have no error during compilation, the data in slidePressure are totally incorrect. Indeed, I test it with a char* = 0and I 've got unknow characters ... So I think the problem must come from conversion.

我的问题是,即使我在编译过程中没有错误,slidePressure 中的数据也完全不正确。事实上,我用 a 测试它,char* = 0我有未知的字符......所以我认为问题必须来自转换。

My datas can be Bool, Uchar, Ushort and float.

我的数据可以Bool, Uchar, Ushort and float

Thanks for your help.

谢谢你的帮助。

回答by carmellose

Is your string an integer? E.g. char* bufferSlidePressure = "123";?

你的字符串是整数吗?例如char* bufferSlidePressure = "123";

If so, I would simply do:

如果是这样,我会简单地做:

uint8_t slidePressure = (uint8_t)atoi(bufferSlidePressure);

Or, if you need to put it in an array:

或者,如果你需要把它放在一个数组中:

slidePressure[0] = (uint8_t)atoi(bufferSlidePressure);

Edit: Following your comment, if your data could be anything, I guess you would have to copy it into the buffer of the new data type. E.g. something like:

编辑:按照您的评论,如果您的数据可以是任何内容,我想您必须将其复制到新数据类型的缓冲区中。例如:

/* in case you'd expect a float*/
float slidePressure;
memcpy(&slidePressure, bufferSlidePressure, sizeof(float));

/* in case you'd expect a bool*/
bool isSlidePressure;
memcpy(&isSlidePressure, bufferSlidePressure, sizeof(bool));

/*same thing for uint8_t, etc */

/* in case you'd expect char buffer, just a byte to byte copy */
char * slidePressure = new char[ size ]; // or a stack buffer 
memcpy(slidePressure, (const char*)bufferSlidePressure, size ); // no sizeof, since sizeof(char)=1

回答by Andy Newman

uint8_t is 8 bits of memory, and can store values from 0 to 255

uint8_t 是 8 位内存,可以存储 0 到 255 的值

char is probably 8 bits of memory

char 可能是 8 位内存

char * is probably 32 or 64 bits of memory containing the address of a different place in memory in which there is a char

char * 可能是 32 或 64 位内存,其中包含内存中不同位置的地址,其中有一个字符

First, make sure you don't try to put the memory address (the char *) into the uint8 - put what it points to in:

首先,确保您不要尝试将内存地址(char *)放入 uint8 - 将它指向的内容放入:

char from;
char * pfrom = &from;
uint8_t to;
to = *pfrom;

Then work out what you are really trying to do ... because this isn't quite making sense. For example, a float is probably 32 or 64 bits of memory. If you think there is a float somewhere in your char * data you have a lot of explaining to do before we can help :/

然后找出你真正想做的事情……因为这不太合理。例如,浮点数可能是 32 或 64 位内存。如果您认为 char * 数据中的某处存在浮点数,那么在我们提供帮助之前,您需要进行大量解释:/

回答by CashCow

char *is a pointer, not a single character. It is possible that it points to the character you want.

char *是一个指针,而不是单个字符。它可能指向您想要的字符。

uint8_tis unsigned but on most systems will be the same size as a charand you can simply cast the value.

uint8_t是无符号的,但在大多数系统上与 a 的大小相同char,您可以简单地转换该值。

You may need to manage the memory and lifetime of what your function returns. This could be done with vector< unsigned char>as the return type of your function rather than char *, especially if toUtf8() has to create the memory for the data.

您可能需要管理函数返回的内存和生命周期。这可以vector< unsigned char>作为函数的返回类型而不是 来完成char *,特别是如果 toUtf8() 必须为数据创建内存。

Your question is totally ambiguous.

你的问题完全模棱两可。

ui->canDataModifiableTableWidget->item(6,3)->text().toUtf8().data();

That is a lot of cascading calls. We have no idea what any of them do and whether they are yours or not. It looks dangerous.

这是很多级联调用。我们不知道他们做了什么,也不知道他们是否属于你。看起来很危险。

回答by CashCow

More safe example in C++ way

C++ 中更安全的例子

char* bufferSlidePressure = "123";
std::string buffer(bufferSlidePressure);
std::stringstream stream;

stream << str;
int n = 0;

// convert to int
if (!(stream >> n)){
    //could not convert
}

Also, if boost is availabe

此外,如果提升可用

int n = boost::lexical_cast<int>( str )