473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to assign long to character array and later extract it

Hi,

How to assign long to character array and later extract it

Nov 6 '06 #1
3 17384
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at...
9
by: Christian Kandeler | last post by:
Hi, if I want to store the string "123456" in a variable of type char, I can do it like this: char s = "123456"; Or like this: char s = { '1', '2', '3', '4', '5', '6', '\0' };
6
by: funmachine | last post by:
Hi there, everyone! I'm a student studying computer animation. But alas, in order to graduate I have to take a programming class and it's kicking my butt. I have two projects left to write (and...
9
by: Wondering Wanderer | last post by:
hi, If I declare a character array ( say, char arr ), how do I see the address of the first element of the array ? simply using cout << arr or cout << &arr displays the entire string in the...
3
by: Jim Langston | last post by:
Is it possible to initialize a std::string with a character array, not neccessarily null terminated? I.E. Given something like this: char buffer; buffer = 0x01; buffer = 0x00; buffer = 'A';...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
2
by: quietforever | last post by:
How do you convert a character array into a integer array? For example: If the character array is entered as'12' then how can get an integer array that contains the integers 1 and 2?
2
by: overdrigzed | last post by:
Hello! I wanted to get the full contents of a character array stored in a struct, i.e. _fields_ = however, ctypes seems to try to return struct.array as a Python string rather than a...
1
by: dissectcode2 | last post by:
HI - I have a 2d array of uint32's : uint32 x; and I want that array to be assigned to a certain address (which is a set of registers) : uint32 *addr = 0x12345678;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.