Click here to Skip to main content
14,334,458 members
Rate this:
Please Sign up or sign in to vote.
See more:
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++)
ostringstream ost;
strDst = ost.str();
delete [] buff2;
delete [] buff1;
Sergey Chepurin 3-Aug-11 6:31am
1. Your code doesn't work for any character. Solution 2 works perfect to remove any character, including no-break space (0xA0).
2. To convert from C-string (char*) to basic_ctring (string) use: string basicstring(buff2);
Rate this:
Please Sign up or sign in to vote.

Solution 2

#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

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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

In this statement:

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?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100