473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17412
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_Keit h) 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
12926
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 the end? I haven't been able to do it so far. Is there a string function I can use? Can anyone help? Charles L
9
3360
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
2120
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 believe me, to some of the veterans out there, it'll be a piece of cake) one of which takes a character array and capitalizes where necessary - eg. begining of the string, after a period. Names and such aren't taken into account. Like I said,...
9
3739
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 array, not the address. thanks.
3
20279
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'; buffer = 'B'; buffer = 'C';
14
4086
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
2155
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
10168
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 character array, and stops as soon as it encounters a null within the character array. I ended up having to define a dummy struct
1
4228
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
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.