如何从C ++中的字符串缓冲区中删除十六进制值为0xA0的字符. [英] How to remove character with hexadecimal value 0xA0 from string buffer in c++.

查看:258
本文介绍了如何从C ++中的字符串缓冲区中删除十六进制值为0xA0的字符.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其他字符之间的字符为0xA0.我已经在字符串缓冲区strSrc中获取了此文件的内容
我需要从文件中删除此字符.
我编写了以下代码.
但是它不适用于0xA0(即使它适用于其他十六进制字符).
请告诉我我的代码有什么问题,应该怎么做才能从缓冲区中删除此字符.


I have one text file with character 0xA0 in between other characters. I have taken this file contents in string buffer strSrc
I need to remove this character from file.
I had written following code.
But it do not work for 0xA0 (even though it works for other hexadecimal characters).
Pls tell me what is wrong with my code, and what should be done to remove this character from buffer.


    string strRemoveIt= "0xA0";
long iIt = strtol(strRemoveIt.c_str (), NULL, 16);
long lsz = strlen(strSrc.c_str())+1;
char* buff1 = new char[lsz];
char* buff2 = new char[lsz];
strcpy_s(buff1, lsz, strSrc.c_str());
int i,j;
for(i=0,j=0; buff1[i]!='\0'; i++)
{
    if(buff1[i]!=iIt)
    {
        buff2[j]=buff1[i];
        j++;
    }
}
buff2[j]='\0';
ostringstream ost;
ost<<buff2;
strDst = ost.str();
delete [] buff2;
buff2=NULL;
delete [] buff1;
buff1=NULL;

推荐答案

Try
Try
#include <string>
#include <algorithm>

void my_remove(std::string& s, char a)
{ s.erase(std::remove(s.begin(),s.end(),a),s.end()); }



你可以打电话



You can call

my_remove(strSrc,'0xA0');



基本上,std :: remove是一种标准算法,该算法将所有等于给定值的值交换到容器的末尾,并为该容器返回建议的新末尾.
擦除方法将消除新旧两端之间的冲突.



Basically, the std::remove is a standard algorithm that swap everything is equal to a given value to the end of the container, returning the proposed new end for that container.
The erase method will cat away what is between the new and the old end.


在此语句中:
In this statement:
if(buff1[i]!=iIt)


您将"char"与"long"进行比较.在0xA0的转换中检查符号扩展"(在8位字节中具有符号位").

您还尝试了哪些其他十六进制值?是> = 0x80吗?


you are comparing a "char" to an "long". Check for "sign extension" in the conversion of 0xA0 (which has the "sign bit" on in the 8 bit byte).

What other hexadecimal values have you tried? Are any of them >= 0x80?


这篇关于如何从C ++中的字符串缓冲区中删除十六进制值为0xA0的字符.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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