There's a special function for this: atol ()
Here's an example of how to use it:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
long li;
char ch[] = "1100033884";
li = atol (ch);
return 0;
}
Niek
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
If buffer contains the binary representation of the long then you can just do a simple assignment. But you need to consider the Big/Little Endean problem if the data comes from another source such as across the internet.
long x = *(long *)buffer;
or use memcpy
long x;
memcpy(&x, buffer, sizeof(long));
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
or use a union
int main()
{
typedef unsigned char byte ;
union
{
byte buffer[ sizeof(long) ] ;
long key ;
};
// ...
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287