将字符串值转换为cstring意味着cstring的结果不是字符串的全长? [英] Converting string values into cstring means the result of cstring is not get full length of the string?

查看:50
本文介绍了将字符串值转换为cstring意味着cstring的结果不是字符串的全长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解码条形码时,条形码值的结果保存在



std :: string str_data 条形码值的结果应该是这样的



str_data = [)> {RS} 0603 {GS}(51)KD-78F0443GBGAHAX / 01032 / T6 (53)1256(54)15204D1

1E4-00210A002(55)2015/05/15(56)PARTIAL(57)T。(58)2(59)1(5D)02(5F )

1519 1519XP001(5H)JAPAN(5J)JAPAN(5L)JAPAN(5M)6255-3544(67)0(68)U

PD78F0443GB-GAH-AX (69)002G1S5X @(6B)0001102866(6I)LG-151(6K)JP(6L

)JP(6M)JP {RS} {EOT}



但是结果的值是在结果值中创建一个空格,比如

[)> 00 0603 (51)KD-78F0443GBGAHAX / 01032 / T6(53)1256(54)15204D1

1E4-00210A002(55)2015/05/15(56)PARTIAL(57)T。(58 )2(59)1(5D)02(5F)

1519 1519XP001(5H)JAPAN(5J)JAPAN(5L)JAPAN(5M)6255-3544(67)0(68)U

PD78F0443GB-GAH-AX(69)002G1S5X @(6B)0001102866(6I)LG-151(6K)JP(6L

)JP(6M)JP



空值以粗体字母突出显示。



另一个问题是将此std :: string值转换为CString意味着我正在 [)>此结果只是因为获取空值而忽略了剩余的值。



所以,我需要以正确的方式保存这个结果值..



什么我试过了:



我试图使用以下代码擦除空格

While decoding the barcode the result of the barcode value is saved in a

std::string str_data the result of the barcode value should be like this

str_data=[)>{RS}0603{GS}(51)KD-78F0443GBGAHAX/01032/T6(53)1256(54)15204D1
1E4-00210A002(55)2015/05/15(56)PARTIAL(57)T.(58)2(59)1(5D)02(5F)
1519 1519XP001(5H)JAPAN(5J)JAPAN(5L)JAPAN(5M)6255-3544(67)0(68)U
PD78F0443GB-GAH-AX(69)002G1S5X@(6B)0001102866(6I)LG-151(6K)JP(6L
)JP(6M)JP{RS}{EOT}

but the value of the result is creating an null space in the result value like
[)>000603?(51)KD-78F0443GBGAHAX/01032/T6(53)1256(54)15204D1
1E4-00210A002(55)2015/05/15(56)PARTIAL(57)T.(58)2(59)1(5D)02(5F)
1519 1519XP001(5H)JAPAN(5J)JAPAN(5L)JAPAN(5M)6255-3544(67)0(68)U
PD78F0443GB-GAH-AX(69)002G1S5X@(6B)0001102866(6I)LG-151(6K)JP(6L
)JP(6M)JP

the null values are highlight in the Bold letters.

and another problem is while converting this std::string value into CString means am getting "[)>" this result only because of the getting null value it is omitting the remaining values.

so, i need the proper way to save this result value..

What I have tried:

I tried to erase the null space using the following code

while(1)
     {
      int letter=a_strdata.find('\0');
      if(letter==-1)
      {
       break;
      }
      a_strdata= a_strdata.erase(letter,1);
     }





删除空格并给出结果字符串的全长,但我需要结果null空格或正确的转换。



It removes the null space and gives the full length of the string of the result but i need the result with the null space or proper conversion.

推荐答案

第一个问题似乎是你的示例字符串是如何打印出来的。



括号中包含控制代码缩写的部分(请参阅控制字符 - 维基百科 [ ^ ])。因此可能是这些是由打印功能创建的占位符。



{RS} 例如可能是控制代码0x1E(RS =记录分隔符)的占位符。



所以你必须首先检查你的 std :: string 确实包含 {RS} 或字节0x1E。要检查这个,请打印第一个字母的十六进制代码。



我认为我是对的,因为这可以解释为什么 CString 以不同的方式打印。



下一项检查将打印 CString 。它们可能与 std :: string 相同。然后输出只有一个问题,但没有分配。



正如CPallini在评论中已经提到的那样,你可能也会遇到Unicode构建问题。为避免这种情况,如果字符串仅包含ASCII字符(代码小于0x80),则应使用 CStringA 而不是 CString

The first problem seems to be how your example strings are printed out.

There are parts with control code abbreviations enclosed by parentheses (see Control character - Wikipedia[^]). So it might be that these are placeholders created by the print function.

{RS} for example might be a place holder for the control code 0x1E (RS = Record Separator).

So you have to check first if your std::string really contains {RS} or the byte 0x1E. To check this print the hex code for the first letters.

I assume that I'm right because that would explain why the CString is printed in a different way.

The next check would be printing out the hex values from the CString. They might be the same as for the std::string. Then there was only a problem with the output but not with the assignment.

As already mentioned by CPallini in the comments you might also get problems with Unicode builds. To avoid this you should use CStringA instead of CString if the strings contain only ASCII characters (codes less than 0x80):
CStringA str = a_strdata.c_str();





如果 CStringA 仍然与 std :: string 不同,检查输入字符串中是否有NULL字节。如果是这样,您不能使用 CString 。然后你必须使用二进制缓冲区。



一个简单的检查:



If the CStringA is still not the same as the std::string check if there are NULL bytes in the input string. If so, you can't use CString. Then you have to use a binary buffer.

A simple check:

CStringA str = a_strdata.c_str();
for (int i = 0; i < str.GetLength(); i++)
    printf("%02X %02X\n", a_strdata.at(i), str.GetAt(i));


一个简单的解决方案是遍历你的str_data并将每个字符添加到CString。



A simple solution is to loop through your str_data and add each char to the CString.

CString c_string;

for (int index = 0; index < str_data.length(); index++){
    c_string += str_data[index];
}



但我认为正确的解决方案是,std :: string是字符,你的CString是多字节的。

所以最好的解决方案应该是转换构造函数:


But I think the correct solution is, that the std::string are chars and your CString is multi-byte.
So the best solution should be the conversion constructor:

CString s = CString(str_data);//conversion constructor


这篇关于将字符串值转换为cstring意味着cstring的结果不是字符串的全长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆