• C

char and int

int d=255; char c;
c=d;
d=c;
printf ("%d",d);

Can you explain why d became -1 instead of 255 ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi learn,

char is 1 byte ... MSB is signed bit - yes we have signed chars and unsigned chars .. default is signed
255 is  11111111 ... which is binary for -1 when stored in a signed char ... Hence the result you see.

Cheers!
sunnycoder
Author Commented:
Thank you!
So, how to do something like:
char c='c'; int i; int a=..., b=...;
i=a*c+b;
c=(i-b)/a;
printf ("%c",c);

Commented:
>So, how to do something like:
Like what? I could not understand what you are trying to accomlpish here. The code you wrote is fine with the usual glitch of downcasting ... You may lose data as you did with the code in the original question.
Author Commented:
Sorry, I didn't make that clear.
char c='c'; int i; int a=..., b=...;
i=a*c+b;            try to code a char
c=(i-b)/a;           try to uncode that
printf ("%c",c);   hope we can pirnt out the original char 'c'.
Of cause that is not correct whenever i=a*c+b>=255.
Now, can you show me how to do the coding-uncoding based on the above idea.

Commented:
assign the char to an int before using it for coding ... The obvious way is to have more memory than 8 bits so that you can handle values greater than 255.

char c='c';
int temp = c; //c will get upcasted automatically
int i; int a=..., b=...;
i=a*temp+b;
temp=(i-b)/a;
c=(char) temp;
printf ("%c",c);
Author Commented:
There is a problem. I want to code 'c' into another char, not a integer....say
i=a*c+b;
charI=(char) i;
then uncode charI back to 'c'
(Sorry, I did not tell you that before.)
Commented:
8 bits can hold values upto 255 (even for that you need to use unsigned char).... If you are going to store more than that in 8 bits, you will lose information ... There is no way to avoid that ...

What are you trying to accomplish .. may be there is a better way to do it.
Commented:
unsigned char c='c';
int i;
int a=1234, b=567;
i=a*c+b;
c=(i-b)/a;
printf ("%c",c);
Author Commented:
ozo,

Thank you.  sunnycoder has suggested the same idea.....what I want is to use a*c+b to code 'c' into another character (not a integer) and decode it back to 'c' later. Perhaps is is difficult.
Commented:
>> a*c+b to code 'c' into another character
addition of 'b' will work, if you add a modulo operation to it to get it back in the range < 256. But the multiplication won't guarantee that the encoding is reversible.

Is there any specific reason that you want to use this encoding scheme ?
What's the purpose of this encoding ?

Your answers to these two questions will help us find a good solution for you.
Commented:
if (a*b)%256  == 1 then you can do
c *= a;
then reverse it with
c *= b;
assuming characters are 8 bits
Commented:
do you know what is the problem ?? the problem is from integer buffer overflow;
i should be great in size than a and b
so insted of int i, set long int i

#include <stdio.h>

void main() {
char c = 'c';
long int i;
int a = 100, b = 200;
i = a * c + b;
c = (i - b) / a;
printf("c = %c\n", c);
}

it will print c again ;)

Ahmed Samieh
Commented:
#include <stdio.h>
main(){
unsigned char c = 'c';
int a=25;
int b=41;  /*  (a*b)%256  == 1  */
c *= a;
c *= b;
printf("c = %c\n", c);
}

Experts Exchange Solution brought to you by