简体   繁体   English

C++:如何将 Unsigned char 数组按值而非 ASCII 转换为 CString

[英]C++: How to convert a Unsigned char array to a CString by value, not ASCII

I would appreciate a little help fixing a bug I have with the conversion of some point data I have that is currently in a unsigned character array (Technically a vector) to a CString.我将不胜感激修复我在将当前位于无符号字符数组(技术上是向量)中的一些点数据转换为 CString 时遇到的错误。

Currently my code converts the data like this:目前我的代码像这样转换数据:

std::vector <unsigned char> points;
CString strResult;
int totalpoints = 0;
// Add point data in here (x,y)
// Tick up totalpoints
strResult = CString(reinterpret_cast<char*>(&points[0]), totalpoints * 4); // I did not write this line. I'm just trying to fix it

return strResult.AllocSysString();

The issue arises when something attempts to read the data back out of strResult.当某些东西试图从 strResult 中读回数据时,就会出现问题。 Some of the data changes.部分数据发生变化。 I believe this is because when CString converts chars it try's to make the same character in wide format, even if the underlying value is different.. For example if I pass it the value 147 I will instead see the value 8339. I can get the value 147 from 8339 if I cast it back into a unsigned char, but where this data ultimately goes I don't have control of it.我相信这是因为当 CString 转换字符时,它会尝试以宽格式生成相同的字符,即使基础值不同。例如,如果我将值 147 传递给它,我将看到值 8339。我可以得到如果我将其重新转换为无符号字符,则从 8339 中的值 147,但是这些数据最终流向何处,我无法控制它。 It is expected that they will get 147 with out casting, not 8339.预计他们将在没有铸造的情况下获得 147,而不是 8339。

So The question is, how do I change the conversion to fix this?所以问题是,我如何改变转换来解决这个问题?

Update: I have been examine the hex values and when the values ending up being incorrect, it's because the high byte in the CString is set to 0x20更新:我一直在检查十六进制值,当值最终不正确时,这是因为 CString 中的高字节设置为 0x20

Update 2: It might also help clarify that existing data starts as two ints.更新 2:它也可能有助于澄清现有数据以两个整数开头。 One for x and one for y.一个用于 x,一个用于 y。 They are separated into pairs of Bytes and stored into points.它们被分成成对的字节并存储到点中。

0x4D (dec 77) is in the ASCII range (it is the character 'M' ), so it should stay the same value when converted to Unicode codepoint U+004D in a wide format. 0x4D(dec 77)在 ASCII 范围内(它是字符'M' ),因此当转换为宽格式的 Unicode 代码点 U+004D 时,它应该保持相同的值。

0x14B (dec 331), on the other hand, doesn't fit in an unsigned char to begin with.另一方面,0x14B (dec 331) 不适合unsigned char开头。

0x93 (dec 147) fits in an unsigned char , but is outside of the ASCII range, so it is subject to charset interpretation, as many different charsets map 0x93 to different Unicode codepoints. 0x93 (dec 147) 适合unsigned char ,但在 ASCII 范围之外,因此它受字符集解释的影响,因为许多不同的字符集 map 0x93 到不同的 Unicode 代码点。

For example, Unicode codepoint U+201C (dec 8220) is indeed encoded as 0x93 in many charsets, such as all of the Windows-125x charsets, but is a different value in other charsets, typically 0xAA (dec 170) but not always.例如,Unicode 代码点 U+201C(dec 8220)在许多字符集中确实被编码为 0x93,例如所有 Windows-125x 字符集,但在其他字符集中是不同的值,通常是 0xAA(dec 170),但并非总是如此。

In general, you need to know the charset that the char data was encoded in, to be able to convert it into a wide format without data loss.通常,您需要知道char数据编码的字符集,以便能够将其转换为宽格式而不会丢失数据。 But the values may change, yes.但是可能会改变,是的。 That is expected behavior during a conversion between encodings.这是编码之间转换期间的预期行为。

CString does not allow you to specify the charset to use when converting char data to a wide format. CString不允许您在将char数据转换为宽格式时指定要使用的字符集。 It always uses the user's default charset.它总是使用用户的默认字符集。 If you know the exact charset used for the char data, use MultiByteToWideChar() or equivalent to convert the data to a wide format before then assigning the result to CString .如果您知道用于char数据的确切字符集,请使用MultiByteToWideChar()或等效方法将数据转换为宽格式,然后再将结果分配给CString


Based on new information, you should NOT be casting your vector data to char* at all.根据新信息,您根本不应该将vector数据转换为char* You are trying to store binary data into a BSTR (why?).您正在尝试将二进制数据存储到BSTR (为什么?)。 Try something more like this instead:尝试更多类似的东西:

std::vector <unsigned char> points;
// fill points as needed...

CString strResult;
auto* buf = strResult.GetBufferSetLength(points.size());
std::copy(points.begin(), points.end(), buf);
strResult.ReleaseBuffer();

return strResult.AllocSysString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM