I'm on Dev-C++ 4.9.9.2 default options, WinXP SP3, P4 w/ HT.

When I c-shift a value, and then back I get back distorted values. Using the standard _rotr() and _rotl.

/*somewhere in main()*/
    str = "I'm a fairy! abc xyz ABC XYZ";
    printf("\n%s\n", str.c_str());
    for(i = 0; i < str.length(); i++)
    {
          str[i] = itoc(_rotr(ctoi(str[i]), 1));
    }
    printf("%s\n", str.c_str());
    
    for(i = 0; i < str.length(); i++)
    {
          str[i] = itoc(_rotl(ctoi(str[i]), 1));
    }
    printf("%s\n", str.c_str());
/*till the end of main()*/

/*for clarity the ctoi, and itoc functions*/
inline int ctoi(char c)
{
    int i = (int)c;
    return(i);
}

inline char itoc(int i)
{
     char c = (char)i;
     return(c);
}

Am I just doing something wrong(like in the casting), or should I try another method?
Another problem might just be it's getting late, and should call it a night. lol

Recommended Answers

All 6 Replies

Can you explain what do you want to do really?
All presented code looks like an absolutely senseless coding exercise...

std::string s("I'm a fairy! abc xyz ABC XYZ");
  for(int i = 0; i < s.length(); i++){ 
     s[i] >>= 1;
     s[i] <<= 1;

Is this what your looking for ?

edit: i don't think so. Sorry. :)

Think: why someone wants to shift/rotate bits in all string chars? Is it a kind of a funny encifering?..

I was just fooling around with encryption, till I ran into this.

But the the problem seems to be:

char a = 'a';
a = _rotr(a, 16);

is doing the same as:

a >>= 16;

Clearing the half of the bits. Instead of preserving the values, and rotating it halfway.
Is there some other function, or algorithm out there I can try instead?

Perhaps I should have posted in the C forums, since I believe it is a C function(stdlib).

Can you explain what do you want to do really?
All presented code looks like an absolutely senseless coding exercise...

I agree, looks like you are trying to convert a string, and then immediately convert it back to the original, which is not really doing anything ...

But to help solve the question maybe try casting differently

char itoc(int i)
{
   return static_cast<char>( i ) ; 
}

That's funny but no need in these itoc/ctoa at all: int to char and vice versa are builtin C++ conversions.
;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.