Link to home
Start Free TrialLog in
Avatar of learn
learn

asked on

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 ?
Avatar of sunnycoder
sunnycoder
Flag of India image

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
Avatar of learn
learn

ASKER

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);

>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.
Avatar of learn

ASKER

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.


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);
Avatar of learn

ASKER

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.)
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.
unsigned char c='c';
int i;
int a=1234, b=567;
i=a*c+b;
c=(i-b)/a;
printf ("%c",c);
Avatar of learn

ASKER

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.
>> 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.
if (a*b)%256  == 1 then you can do
c *= a;
then reverse it with
c *= b;
assuming characters are 8 bits
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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I see you already accepted ozo's answer, but can you still tell us what the purpose of this encoding is ? Depending on the application you intend for this code, the accepted solution could be a good solution or not.