Converting char to int on UNIX C

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3085 Views - Last Post: 28 June 2008 - 04:23 PM Rate Topic: -----

#1 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Converting char to int on UNIX C

Posted 27 June 2008 - 06:32 AM

I have this unsigned char variable that converts right in Windows but not UNIX with the same code. I was using new=(int)characterv;. Is there a more reliable way to make this conversion on UNIX C? C not C++. Thanks.

This post has been edited by nombre: 27 June 2008 - 07:40 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Converting char to int on UNIX C

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 08:04 AM

The unsigned char to int will work as posted. One problem you may see though is that on different platforms the character values may be different from what you expect. (ASCII vs EBCDIC)

note that you should not use the variable name "new". Although I don't think it is a reserved word in C it is in C++.
Was This Post Helpful? 0
  • +
  • -

#3 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 08:28 AM

I'm getting 7's instead of 3's. Do you know why and how I can resolve this? Is there a way to bitwise convert?

This post has been edited by nombre: 27 June 2008 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 04:28 PM

The cast should do a bitwise copy of the binary data. i.e the binary data should not change from one location to another to another (if it does then your compiler is very odd and not standard).

If you would like to verify this, here is a simple program to look at the binary data:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char * outputBinaryForInt(int valueIn, char *buffer);
char * outputBinaryForChar(unsigned char valueIn, char *buffer);

int main() {
    char buffer[129];
    unsigned char initialValue;
    int integerValue;
    int i;
    srand(time(NULL));
    for (i = 1; i < 10; ++i) {
        initialValue = (rand() % 255);
        integerValue = (int)initialValue;
        printf("char = %s ", outputBinaryForChar(integerValue, buffer));
        printf("int = %s\n", outputBinaryForInt(integerValue, buffer));
    }
    return 0;
}

//Note that the output of this is actually "backwards"
// i.e. the LSB is on the left.
char * outputBinaryForInt(int valueIn, char *buffer) {
     int i;
     char *ptr = buffer;
     for (i=0; i < 8 * sizeof(int); ++i) {
         *ptr++ = (valueIn % 2) + '0';
         valueIn >>= 1;
     }
     *ptr = 0;
     return buffer;
}

//Note that the output of this is actually "backwards"
// i.e. the LSB is on the left.        
char * outputBinaryForChar(unsigned char valueIn, char *buffer) {
     int i;
     char *ptr = buffer;
     for (i=0; i < 8 * sizeof(unsigned char); ++i) {
         *ptr++ = (valueIn % 2) + '0';
         valueIn >>= 1;
     }
     *ptr = 0;
     return buffer;
}


If the output does not match for char and ints then there is something very odd with your compiler.

This post has been edited by NickDMax: 27 June 2008 - 04:29 PM

Was This Post Helpful? 1
  • +
  • -

#5 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 05:19 PM

It's the program I'm working on. It's very old C. So how do I convert the different responses? They are different. I don't know if its just the conversion from char to int.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 05:25 PM

So the program I gave shows a difference between the binary for the int and the char?

Can you please paste the results into a post?

This is highly interesting.

One way you can try is something like this:

integerValue = 0 + initialValue;

This uses C's promotion. In the arithmetic.
Was This Post Helpful? 0
  • +
  • -

#7 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 05:52 PM

int = 00000000000000000000000000000000
 char = 0000000000000000000000000000000000000000
 0
0
Press any key to continue . . .
int = 000000000000000000000000000000000000000001100000000000000000000000000000
 char = 000000000000000000000000000000000000000001100000000000000000000000000000
01100000
 0
0
Press any key to continue . . .
int = 00000000000000000000000000000000000000000110000000000000000000000000000001
10000000100000000000000000000000000000
 char = 000000000000000000000000000000000000000001100000000000000000000000000000
011000000010000000000000000000000000000000100000
 0
0
Press any key to continue . . .
int = 00000000000000000000000000000000000000000110000000000000000000000000000001
100000001000000000000000000000000000000010000011000000000000000000000000000000
 char = 000000000000000000000000000000000000000001100000000000000000000000000000
01100000001000000000000000000000000000000010000011000000000
 0
0
Press any key to continue . . .



This is the output on Windows Vista.
Was This Post Helpful? 0
  • +
  • -

#8 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 05:58 PM

int = 00000000000000000000000000000000
 char = 0000000000000000000000000000000000000000
 0
0
sh: pause: not found
int = 000000000000000000000000000000000000000001100000000000000000000000000000
 char = 00000000000000000000000000000000000000000110000000000000000000000000000001100000
 0
0
sh: pause: not found
int = 0000000000000000000000000000000000000000011000000000000000000000000000000110000000100000000000000000000000000000
 char = 000000000000000000000000000000000000000001100000000000000000000000000000011000000010000000000000000000000000000000100000
 0
0
sh: pause: not found
int = 00000000000000000000000000000000000000000110000000000000000000000000000001100000001000000000000000000000000000000010000011100000000000000000000000000000
 char = 0000000000000000000000000000000000000000011000000000000000000000000000000110000000100000000000000000000000000000001000001110000000000000000000000000000011100000
 0
0
sh: pause: not found
int = 000000000000000000000000000000000000000001100000000000000000000000000000011000000010000000000000000000000000000000100000111000000000000000000000000000001110000000100000
0
 char = 000000000000000000000000000000000000000001100000000000000000000000000000011000000010000000000000000000000000000000100000111000000000000000000000000000001110000000100000

 25363980
2
sh: pause: not found
int = 0000000000000000000000000000000000000000011000000000000000000000000000000110000000100000000000000000000000000000001000001110000000000000000000000000000011100000
 char = 0000000000000000000000000000000000000000011000000000000000000000000000000110000000100000000000000000000000000000001000001110000000000000000000000000000011100000
 3
3
sh: pause: not found
int = 0000000000000000000000000000000000000000011000000000000000000000000000000110000000100000000000000000000000000000001000001110000000000000000000000000000011100000
 char = 0000000000000000000000000000000000000000011000000000000000000000000000000110000000100000000000000000000000000000001000001110000000000000000000000000000011100000
 0
0
sh: pause: not found
Segmentation Fault(coredump)


This is the output to Sun solaris sparc.
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 06:00 PM

Wow... this is interesting... that is the output of the program I provided? (with a delay inserted between outputs I take it).

What is odd is that each iteration seems to have a different size... can you paste the exact code that generated this output?
Was This Post Helpful? 0
  • +
  • -

#10 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 06:04 PM

   unsigned char car; char *ptr = buffer;  unsigned int carint;												  

																car=(MEM[segtab[i+MAXSEGMENTS].membase + x].opcode);
				carint=car;

				for (r=0; r < 8 * sizeof(unsigned int); ++r)
				{
					*ptr++ = (carint % 2) + '0';
					carint >>= 1;
				}
				*ptr = 0;
				printf("int = %s\n ", buffer);
				for (r=0; r < 8 * sizeof(unsigned char); ++r)
				{
					*ptr++ = (car % 2) + '0';
					car >>= 1;
				}
				*ptr = 0;
				printf("char = %s\n ", buffer);
				printf("%d\n", carint);
				printf("%u\n", car);
				system("pause");

car is the unsigned char.
carint is the unsigned int.

It crashes the program. For some reason it doesn't complete.
It's not everything but it would take to long to explain.

I think the reason is covered here but there is no solution. Char to int. http://www.cs.umd.ed...Data/ascii.html

This post has been edited by nombre: 27 June 2008 - 06:09 PM

Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 06:28 PM

Well you have a little bug in your program (you have to reset ptr=buffer).

also using "system("pause")" works on windows but will not work in other environments.

try this:
	char buffer[1024];
	unsigned char car; 
	char *ptr = buffer;
	unsigned int carint, r;												  

	//car=(MEM[segtab[i+MAXSEGMENTS].membase + x].opcode);
	car = 255;
	carint=car;

	for (r=0; r < 8 * sizeof(unsigned int); ++r)
	{
		*ptr++ = (carint % 2) + '0';
		carint >>= 1;
	}
	*ptr = 0;
	printf("int  = %s\n", buffer);
	ptr = buffer;
	for (r=0; r < 8 * sizeof(unsigned char); ++r)
	{
		*ptr++ = (car % 2) + '0';
		car >>= 1;
	}
	*ptr = 0;
	printf("char = %s\n", buffer);
	printf("%d\n", carint);
	printf("%u\n", car);


Was This Post Helpful? 0
  • +
  • -

#12 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 06:33 PM

int = 00000000000000000000000000000000
 char = 00000000
 0
0
Press any key to continue . . .
int = 0000000001100000000000000000000000000000
 char = 01100000
 0
0
Press any key to continue . . .
int = 0110000000100000000000000000000000000000
 char = 00100000
 0
0
Press any key to continue . . .
int = 0010000011000000000000000000000000000000
 char = 11000000
 0
0
Press any key to continue . . .
int = 1100000000100000000000000000000000000000
 char = 00100000
 0
0
Press any key to continue . . .
int = 0010000011000000000000000000000000000000
 char = 11000000
 0
0
Press any key to continue . . .
int = 1100000000100000000000000000000000000000
 char = 00100000
 0
0
Press any key to continue . . .
int = 0010000011000000000000000000000000000000
 char = 11000000
 0
0
Press any key to continue . . .
int = 1100000000000000000000000000000000000000
 char = 00000000
 0
0
Press any key to continue . . .
int = 0000000011100000000000000000000000000000
 char = 11100000
 0
0
Press any key to continue . . .
int = 1110000010000000000000000000000000000000
 char = 10000000
 0
0
Press any key to continue . . .
int = 1000000001000000000000000000000000000000
 char = 01000000
 0
0
Press any key to continue . . .
int = 0100000010000000000000000000000000000000
 char = 10000000
 0
0
Press any key to continue . . .
int = 1000000001000000000000000000000000000000
 char = 01000000
 0
0
Press any key to continue . . .
int = 0100000000100000000000000000000000000000
 char = 00100000
 0
0
Press any key to continue . . .
int = 0010000011000000000000000000000000000000
 char = 11000000
 0
0
Press any key to continue . . .
int = 1100000010100000000000000000000000000000
 char = 10100000
 0
0
Press any key to continue . . .


This is my Windows output.

This post has been edited by nombre: 27 June 2008 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#13 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 06:39 PM

int = 00000000000000000000000000000000
 char = 00000000
 0
0
sh: pause: not found
int = 0000000001100000000000000000000000000000
 char = 01100000
 0
0
sh: pause: not found
int = 0110000000100000000000000000000000000000
 char = 00100000
 0
0
sh: pause: not found
int = 0010000011100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 1110000000100000000000000000000000000000
 char = 00100000
 0
0
sh: pause: not found
int = 0010000011100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 1110000000100000000000000000000000000000
 char = 00100000
 0
0
sh: pause: not found
int = 0010000011100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 1110000000000000000000000000000000000000
 char = 00000000
 0
0
sh: pause: not found
int = 0000000011100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 1110000010000000000000000000000000000000
 char = 10000000
 0
0
sh: pause: not found
int = 1000000011100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 1110000010000000000000000000000000000000
 char = 10000000
 0
0
sh: pause: not found
int = 1000000011100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 1110000000100000000000000000000000000000
 char = 00100000
 0
0
sh: pause: not found
int = 0010000011100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 1110000010100000000000000000000000000000
 char = 10100000
 0
0
sh: pause: not found


Simulation begins...

...Simulation ends.

This is my Sparc output. Do you have any idea how I could solve my problem? I'm to lazy to take out the system('pause") right now.

This post has been edited by nombre: 27 June 2008 - 06:43 PM

Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 06:48 PM

		//car=(MEM[segtab[i+MAXSEGMENTS].membase + x].opcode);
		car = i;
		carint=car;
		 ptr = buffer;  
		for (r=0; r < 8 * sizeof(unsigned int); ++r)
		{
			*ptr++ = (carint % 2) + '0';
			carint >>= 1;
		}
		*ptr = 0;
		printf("int  = %s\n", buffer);
		ptr = buffer;
		for (r=0; r < 8 * sizeof(unsigned char); ++r)
		{
			*ptr++ = (car % 2) + '0';
			car >>= 1;
		}
		*ptr = 0;
		printf("char = %s\n", buffer);
		printf("%d\n", carint);
		printf("%u\n", car);
		system("pause");


You need to make sure that you are resetting the ptr variable though each loop. That is why you are seeing the shifts in the bits. If you were to have used the functions I wrote you would have been alright.
Was This Post Helpful? 0
  • +
  • -

#15 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Converting char to int on UNIX C

Posted 27 June 2008 - 07:00 PM

int = 00000000000000000000000000000000
 char = 00000000
 0
0
Press any key to continue . . .
int = 01100000000000000000000000000000
 char = 01100000
 0
0
Press any key to continue . . .
int = 00100000000000000000000000000000
 char = 00100000
 0
0
Press any key to continue . . .
int = 11000000000000000000000000000000
 char = 11000000
 0
0
Press any key to continue . . .
int = 00100000000000000000000000000000
 char = 00100000
 0
0
Press any key to continue . . .
int = 11000000000000000000000000000000
 char = 11000000
 0
0
Press any key to continue . . .
int = 00100000000000000000000000000000
 char = 00100000
 0
0
Press any key to continue . . .
int = 11000000000000000000000000000000
 char = 11000000
 0
0
Press any key to continue . . .
int = 00000000000000000000000000000000
 char = 00000000
 0
0
Press any key to continue . . .
int = 11100000000000000000000000000000
 char = 11100000
 0
0
Press any key to continue . . .
int = 10000000000000000000000000000000
 char = 10000000
 0
0
Press any key to continue . . .
int = 01000000000000000000000000000000
 char = 01000000
 0
0
Press any key to continue . . .
int = 10000000000000000000000000000000
 char = 10000000
 0
0
Press any key to continue . . .
int = 01000000000000000000000000000000
 char = 01000000
 0
0
Press any key to continue . . .
int = 00100000000000000000000000000000
 char = 00100000
 0
0
Press any key to continue . . .
int = 11000000000000000000000000000000
 char = 11000000
 0
0
Press any key to continue . . .
int = 10100000000000000000000000000000
 char = 10100000

Windows
int = 00000000000000000000000000000000
 char = 00000000
 0
0
sh: pause: not found
int = 01100000000000000000000000000000
 char = 01100000
 0
0
sh: pause: not found
int = 00100000000000000000000000000000
 char = 00100000
 0
0
sh: pause: not found
int = 11100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 00100000000000000000000000000000
 char = 00100000
 0
0
sh: pause: not found
int = 11100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 00100000000000000000000000000000
 char = 00100000
 0
0
sh: pause: not found
int = 11100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 00000000000000000000000000000000
 char = 00000000
 0
0
sh: pause: not found
int = 11100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 10000000000000000000000000000000
 char = 10000000
 0
0
sh: pause: not found
int = 11100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 10000000000000000000000000000000
 char = 10000000
 0
0
sh: pause: not found
int = 11100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 00100000000000000000000000000000
 char = 00100000
 0
0
sh: pause: not found
int = 11100000000000000000000000000000
 char = 11100000
 0
0
sh: pause: not found
int = 10100000000000000000000000000000
 char = 10100000
 0
0
sh: pause: not found

Simulation begins...

...Simulation ends.


Sparc
Any clue?

The fourth one changes and so do further ones with 3 going to 7. I thought about bit shifting but there are legitimate 7's.

This post has been edited by nombre: 27 June 2008 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2