我正在尝试为Uint8变量分配一个字符串,它无法进行类型转换或类似的操作,所以我首先要做的就是通过文件进行分配,这样我首先将字符串写入stderr .txt,然后使用fscanf读取字符串.我使用fscanf是因为您可以选择读取哪种类型,所以我将成功将字符串转换为Uint8,对吗?错误的!然后,我发现程序在执行过程中不会写入普通字符,因此,如果字符串包含例如"Test",则Uint8将包含(!µ @""之类的内容,因为该文件不包含普通字符.执行过程中的字符. 现在我的问题很简单:我如何为Uint8变量分配一个字符串,以便如果该字符串包含"Test",那么Uint8变量将表示"Test"?(当然,其格式也不同) 所以我想这样使用它: Uint8数据; 字符串str; 数据= str; *做任何事情*
I am trying to assign a string to a Uint8 variable, it didn''t work to typecast or anything like that so what I tried to do first was to assign via a file so that I would first write the string to stderr.txt and then use fscanf to read the string. I used fscanf because you can choose what type to read with and so then I would have successfully converted the string to Uint8, right? Wrong! I then found out that the program doesn''t write normal characters during execution so that if the string contained for example "Test", then the Uint8 would contain something like "(!µ@" because the file didn''t contain normal characters during execution. Now my question is simply this: how do I assign a string to a Uint8 variable so that if the string contains "Test", then the Uint8 variable will conatin "Test"?(but in a diffrent format of course) So i would like to use it like this: Uint8 data; string str; data = str; *do whatever*
推荐答案来自 MSDN [ ^ ]: From MSDN[^]: UINT8 一个8位无符号整数(范围:0到255十进制).由于UINT8是无符号的,因此它的第一位(最高有效位(MSB))不保留用于签名. 在BaseTsd.h中声明此类型,如下所示: UINT8 An 8-bit unsigned integer (range: 0 through 255 decimal). Because a UINT8 is unsigned, its first bit (Most Significant Bit (MSB)) is not reserved for signing. This type is declared in BaseTsd.h as follows: typedef unsigned char UINT8;
因此,您不能将string分配给UINT8变量.但是,您可以将char分配给UINT8变量.
So you cannot assign a string to an UINT8 variable. You can however assign a char to an UINT8 variable.
Uint8* pdata; string str; pData = new Uint8[str.length() + 1]; memcpy (pdata, str.c_str(), str.length() + 1);
请记住,您无法以尝试的方式将一种类型转换为另一种类型; string和Uint8完全不兼容.
Remember you cannot cast from one type to another in the way you tried; a string and a Uint8 are not in any way compatible.
更多推荐
如何为Uint8变量分配字符串?
发布评论