• 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 ?
learnAsked:
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.

sunnycoderCommented:
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
learnAuthor 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);

sunnycoderCommented:
>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.
learnAuthor 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.


sunnycoderCommented:
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);
learnAuthor 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.)
sunnycoderCommented:
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.
ozoCommented:
unsigned char c='c';
int i;
int a=1234, b=567;
i=a*c+b;
c=(i-b)/a;
printf ("%c",c);
learnAuthor 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.
Infinity08Commented:
>> 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.
ozoCommented:
if (a*b)%256  == 1 then you can do
c *= a;
then reverse it with
c *= b;
assuming characters are 8 bits
asm4picCommented:
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
ozoCommented:
#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

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.