By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,874 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

How to assign long to character array and later extract it

P: n/a
Hi,

How to assign long to character array and later extract it

Nov 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ro******@gmail.com wrote:
How to assign long to character array and later extract it
Your question is a bit vague (you can't assign a long to a char
array), so I can just assume that you want to know how to store
the bits that make up a long in the elements of a char array
and later to retrieve the value - and that's rather simple to be
done using the memcpy() function:

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

int main( void )
{
long int a = 1234;
char c[ sizeof a ]; /* ok since sizeof is evaluated at compile time */

printf( "long variable is set to %ld\n", a );

/* Copy the long to the char array */
memcpy( c, &a, sizeof a );

/* Do something in between with the long */
a = -76543;
printf( "But now it's %ld\n", a );

/* Get the long back from where it's stored in the char array
and print it to demonstrate that it did work */
memcpy( &a, c, sizeof a );
printf( "And here it's %ld again\n", a );

return 0;
}
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Nov 6 '06 #2

P: n/a
ro******@gmail.com writes:
How to assign long to character array and later extract it
Please explain more clearly what you're trying to do. Do you want to
store a character string in decimal, a sequence of bytes, a sequence
of '0' and '1' characters representing bits, or what?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 6 '06 #3

P: n/a
ro******@gmail.com wrote:
Hi,

How to assign long to character array and later extract it
#include <stdio.h>
#include <string.h>
#include <limits.h>

int main(void)
{
long lval = LONG_MAX / 2 + LONG_MAX / 4 + LONG_MAX / 8 + 1;
/* if you don't like it choose your own value */
long newval = 0;
unsigned char carray[sizeof lval];
size_t ndx;
printf
("The original long has a value of %ld, and its size is %lu\n",
lval, (unsigned long) sizeof lval);
memcpy(carray, &lval, sizeof lval);
printf("The character array contains these values:\n");
for (ndx = 0; ndx < sizeof lval; ndx++)
printf(" carray[%lu] = %u (%#o)\n", (unsigned long) ndx,
carray[ndx], carray[ndx]);
memcpy(&newval, carray, sizeof lval);
printf("The value of the reconstituted new long value is %ld\n",
newval);
return 0;
}

[output on one implementation]
The original long has a value of 1879048190, and its size is 4
The character array contains these values:
carray[0] = 254 (0376)
carray[1] = 255 (0377)
carray[2] = 255 (0377)
carray[3] = 111 (0157)
The value of the reconstituted new long value is 1879048190
Nov 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.