Re: wtf is happening here @ bitwise comparison
tschmittldk <tschmittldk@googlemail.com> wrote in news:9139bceb-5be4-
4653-8b82-d92663dd18d8@l32g2000yqc.googlegroups.com:
On 22 Dez., 19:45, tschmittldk <tschmitt...@googlemail.com> wrote:
Okay thanks for all your answers. I try it tomorrow and post the code
then (I left my notebook in my student flat...). But it seems more
clearly to me now, thanks!
Okay, now here's the code:
void codevert(char *ArrayToTransform)
{
int j = 0;
char *ptr = ArrayToTransform;
while (*ptr != '\0') {
if((*ptr & 0xC0) > 0xbf)
{
if(*ptr == '\xc3')
simplifier_correct(3, ptr++);
else if(*ptr == '\xc4')
simplifier_correct(3, ptr++);
else if(*ptr == '\xc4')
simplifier_correct(3, ptr++);
else
std::cout << "E01";
}
ptr++;
}
}
This is all very brittle. *ptr is char, which is most probably a signed
type and can be negative. (*ptr & 0xC0) is int and appears to be positive
and of the desired value even if *ptr is negative, this is more by chance
and not very portable. 0xbf is int and positive, '\xc3' is char and
negative.
I would rewrite this code about like this:
const unsigned char *ptr = reinterpret_cast<unsigned char*>
(ArrayToTransform);
while (*ptr) {
if((*ptr & 0xC0) > 0xbf)
{
if(*ptr == 0xc3)
// ...
hth
Paavo