Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   char to int (http://www.go4expert.com/forums/char-to-int-t2610/)

ceus 19Jan2007 11:47

char to int
 
Hi friends this is my simple code...
i have stored 4 charecters in a buffer and compiling this code in GCC... where int is 4 bytes and if i read this charecter buffer as integer it must show 9ab8 know ??? but it is showing some junk value :( what is wrong with this code.. how to take these 4 charecters into a single integer and when i print the integer it must print 9ab8

Code:

#include<stdio.h>
#include<string.h>

int main()
{
        char buf[4]={'9','a','b','8'};
        int i;
        i=(int)buf;
        printf("%d\n",sizeof(int));
        printf("%d\n",sizeof(buf));
        printf("%c",buf[0]);
        printf("%c",buf[1]);
        printf("%c",buf[2]);
        printf("%c\n\n",buf[3]);
        printf("%d",i);
        return 0;

}


DaWei 19Jan2007 15:27

Re: char to int
 
You have a couple of issues. The character, '9', has the integral value of 57 (presuming it's ASCII--Lord knows, if it's not). The second issue revolves around endianness. Different platforms read and combine the 4 bytes of a four-byte integer in different orders. Thirdly, 'buf' is the address of the first element of the array. If you convert it to int, you are merely casting the address to an integer, not the contents.

It is clear from your code what you are trying to do. Why are you trying to do it? What is your actual requirement?

ceus 19Jan2007 16:26

Re: char to int
 
Thanks DaWei..:)
nothing special i am trying to do.. just thought char is 1 byte and int is 4 bytes then why cant i take an array of charecters into a single integer variable.... ??? :(.. what ever may be the endianness just i want the whole of the numbers in a single variable...yes in my code i am casting the address because to read an integer from that address ( that should be 4 bytes know ) why i am not getitng.. can u please gimme a simple code or some tips to do this.....

DaWei 19Jan2007 21:13

Re: char to int
 
You aren't listening.

'9' is not 9. '9' is a code for a symbol. It isn't always the same code. Maybe it's ASCII, maybe it's 3-of-5, maybe it's unicode, and maybe it's a form of EBCDIC. In ASCII, for instance, '9' is 00111001. That is not a binary 9.

If you have a little-endian machine, the contents of a four-byte array containing 1, 2, 3, 4, from element 0 to element 3, will be assembled as 3412. A big-endian machine will assemble them as 1234. Some forms of endianness will assemble them as 2143.

The fact that using an address when using the contents doesn't work does NOT make using the address work. Can't get your car started? Maybe if you go start your lawnmower, you can drive your car?

Not knowing these things is not a sin. Pretending that they don't exist simply because you don't like the facts I have presented is a dumb thing to do.

Since you don't have a specific goal in mind, such as converting sequential keystrokes like 9ab8 into a number, on the presumption (and it's merely a matter of presumption or definition) that the symbols represent a hexadecimal notation, I can't help you. Go read. Don't discard reality on the way.

ceus 22Jan2007 11:59

Re: char to int
 
Thanks for u reply..:)
i just wanted to know.... can we convert an arry of 4 charecters to int in any way... and
Quote:

Since you don't have a specific goal in mind
U think knowing things without any goal is useless ??? "Any responsible parents will store money for their childrens' future thinking that it WILL BE USEFUL" IN MY OPINION """" NO KNOWLEDGE IS USELESS"""" I am asking u guys since u know more than me and i think the question what i asked will be somehow useful in future for me...."No child will learn to walk within a single day it HAS to fall down so many times..." thanks for ur patience....:)

DaWei 22Jan2007 14:10

Re: char to int
 
Don't quote me with partial context, sonny boy. I didn't say that knowledge was useless. Read what I wrote. "Since you don't have a specific goal in mind...(relevant content here)...I can't help you." That makes eminent sense. I can't cover every possible random thought you might have.

If you want to convert strings to numbers, look up things like stringstreams, strtol, etc. The library functions heed the underlying architecture that the compiler is designed to serve. You cannot presume anything about characters in an array representing some number without considering such things. I said that above. You still aren't comprehending what you read, if you're reading it.

If you want to really understand why, get down into architecture and compare a Motorola uP with an Intel uP with a (name your favorite) uP.

ceus 22Jan2007 14:41

Re: char to int
 
ok thanks DaWei, strtol is the exact one that i was looking for....:) and my word does'nt mean to hurt u....and i am not a boy..:). i am a professional in Electronics and communications working for a company , i don have much knowledge in C or C++ thats y asking the experts help...
thanks again.. :)


All times are GMT +5.5. The time now is 20:07.