Author |
Message |
Yanick Bourbea #1 / 19
|
 copying unsigned long into char[4] or char*
*** post for FREE via your newsreader at post.newsfeed.com *** How i can do that ? -----= Posted via Newsfeed.Com, Uncensored Usenet News =----- http://www.*-*-*.com/ - The #1 Newsgroup Service in the World! -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
|
Sat, 23 Jul 2005 00:41:54 GMT |
|
 |
Allan Bruc #2 / 19
|
 copying unsigned long into char[4] or char*
Quote: > *** post for FREE via your newsreader at post.newsfeed.com *** > How i can do that ?
First, unsigned long is not necessarily 4 Bytes, and a char isnt necessarily 1 Byte. I /think/ (I am new to programming). You would delare as: unsigned long int number; char char_array[4]; then you can copy the number to the array like: char_array[0] = number & 0x000000ff char_array[1] = number & 0x0000ff00 char_array[2] = number & 0x00ff0000 char_array[3] = number & 0xff000000 Allan
|
Sat, 23 Jul 2005 00:41:46 GMT |
|
 |
Marius Bernkle #3 / 19
|
 copying unsigned long into char[4] or char*
Quote:
>> *** post for FREE via your newsreader at post.newsfeed.com *** >> How i can do that ? > First, unsigned long is not necessarily 4 Bytes, and a char isnt > necessarily 1 Byte.
Actually, a char is a byte. A byte, however, is not necessarily an octet. Marius -- <URL: http://home.ifi.uio.no/~mariube/ > Life sucks. Death swallows.
|
Sat, 23 Jul 2005 00:58:54 GMT |
|
 |
Kevin Hand #4 / 19
|
 copying unsigned long into char[4] or char*
Quote:
> *** post for FREE via your newsreader at post.newsfeed.com *** > How i can do that ?
I would suggust you talk to post.newsfeed.com about how to do that. Only standard C is covered by this newsgroup. Free posting is off-topic for comp.lang.c
|
Sat, 23 Jul 2005 01:58:09 GMT |
|
 |
Ryan Henness #5 / 19
|
 copying unsigned long into char[4] or char*
Quote:
> *** post for FREE via your newsreader at post.newsfeed.com *** > How i can do that ?
It is generally considered poor style to ask a question only in the subject line of your post, as some newsreaders don't show the subject when viewing the body. To answer your question: the most straightforward and readable way is to use the library routine memcpy(), with the following prototype and header file: #include <string.h> void memcpy(void *dest, const void *src, size_t bytes); Use it like this: long my_long = 2354723; unsigned char my_uchar_buffer[sizeof my_long] = {0}; memcpy(my_uchar_buffer, &my_long, sizeof my_long); HTH, Ryan. Note: You shouldn't assume a long is four bytes; use sizeof instead. Also, if you're "copying into char*", be sure point the char* at sufficient memory first, perhaps via malloc.
|
Sat, 23 Jul 2005 01:54:00 GMT |
|
 |
Dan P #6 / 19
|
 copying unsigned long into char[4] or char*
Quote: >How i can do that ?
unsigned long foo = 123456789; unsigned char buff[sizeof foo]; memcpy(buff, &foo, sizeof foo); Of course, if you insist on copying 4 bytes, regardless of the actual size of a long, you have to replace sizeof foo by the magical constant 4. This may cause problems in the unlikely event that sizeof(long) < 4 on a hosted implementation (on freestanding implementations you can't rely on the existence of memcpy). Dan -- Dan Pop DESY Zeuthen, RZ group
|
Sat, 23 Jul 2005 02:05:10 GMT |
|
 |
Martin Ambuh #7 / 19
|
 copying unsigned long into char[4] or char*
Quote:
> *** post for FREE via your newsreader at post.newsfeed.com *** > How i can do that ?
Don't hide your question in the subject line. #include <stdio.h> #include <string.h> #include <limits.h> int main(void) { unsigned long ul = 037700066655ul; unsigned char c[sizeof ul]; size_t i; memcpy(c, &ul, sizeof ul); printf("The unsigned long had %#lo (%#lx, %lu)\n", ul, ul, ul); printf("The chars are:\n"); for (i = 0; i < sizeof ul; i++) printf("%2u: %#o (%#x, %u)\n", (unsigned) i, c[i], c[i], c[i]); return 0; Quote: }
The unsigned long had 037700066655 (0xff006dad, 4278218157) The chars are: 0: 0255 (0xad, 173) 1: 0155 (0x6d, 109) 2: 0 (0, 0) 3: 0377 (0xff, 255)
|
Sat, 23 Jul 2005 03:12:31 GMT |
|
 |
Mike Fischbei #8 / 19
|
 copying unsigned long into char[4] or char*
Quote: > How i can do that ? > copying unsigned long into char[4] or char*
Look up the C concept "union" in your favorite C textbook or reference manual. mike --
Any opinions expressed are mine and not necessarily those of any other entity. They may not even be mine.
|
Sat, 23 Jul 2005 03:44:06 GMT |
|
 |
Daniel Fo #9 / 19
|
 copying unsigned long into char[4] or char*
Quote:
>> *** post for FREE via your newsreader at post.newsfeed.com *** > I /think/ (I am new to programming). You would delare as: > unsigned long int number; > char char_array[4]; > then you can copy the number to the array like: > char_array[0] = number & 0x000000ff > char_array[1] = number & 0x0000ff00 > char_array[2] = number & 0x00ff0000 > char_array[3] = number & 0xff000000
Sorry, this won't work at all. You're missing some shifts. -Daniel
|
Sat, 23 Jul 2005 08:21:25 GMT |
|
 |
Daniel Fo #10 / 19
|
 copying unsigned long into char[4] or char*
Quote:
>> How i can do that ? >> copying unsigned long into char[4] or char* > Look up the C concept "union" in your favorite > C textbook or reference manual.
Thats not what a union is for. - Daniel
|
Sat, 23 Jul 2005 10:28:47 GMT |
|
 |
Barry Schwar #11 / 19
|
 copying unsigned long into char[4] or char*
On Mon, 03 Feb 2003 19:44:06 GMT, Mike Fischbein Quote:
>> How i can do that ? >> copying unsigned long into char[4] or char* >Look up the C concept "union" in your favorite >C textbook or reference manual. > mike
And promptly invoke undefined behavior? <<Remove the del for email>>
|
Sat, 23 Jul 2005 11:10:02 GMT |
|
 |
Jan Engelhard #12 / 19
|
 copying unsigned long into char[4] or char*
Quote: >>> How i can do that ? >>> copying unsigned long into char[4] or char* >>Look up the C concept "union" in your favorite >>C textbook or reference manual. >And promptly invoke undefined behavior?
What undefined behavior? union { char a[4]; long b; } c; c.b = 1234567; -- read c.a[0..3]; but be sure to deal with endianess.
|
Sat, 23 Jul 2005 15:34:07 GMT |
|
 |
pete #13 / 19
|
 copying unsigned long into char[4] or char*
Re: copying unsigned long into char[4] or char* Quote:
> How i can do that ?
#include <stdio.h> #include <stddef.h> int main(void) { size_t byte; long unsigned data; unsigned char array[sizeof data]; data = 0x12345678; puts("\ndata = 0x12345678;\n"); byte = sizeof data; while (byte--) { array[byte] = ((unsigned char*)&data)[byte]; } printf("The lowest addressable byte of data, " "has a value of 0x%x\n", array[0]); for (byte = 1; byte != sizeof data; ++byte) { printf("The next addressable byte of data, " "has a value of 0x%x\n", array[byte]); } return 0; Quote: }
-- pete
|
Sat, 23 Jul 2005 18:06:05 GMT |
|
 |
CBFalcone #14 / 19
|
 copying unsigned long into char[4] or char*
Quote:
> >>> How i can do that ? > >>> copying unsigned long into char[4] or char* > >>Look up the C concept "union" in your favorite > >>C textbook or reference manual. > >And promptly invoke undefined behavior? > What undefined behavior? > union { char a[4]; long b; } c; c.b = 1234567; -- read c.a[0..3]; > but be sure to deal with endianess.
As the man said - undefined behaviour. --
Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address!
|
Sat, 23 Jul 2005 21:07:46 GMT |
|
 |
Dan P #15 / 19
|
 copying unsigned long into char[4] or char*
Quote: >>>> How i can do that ? >>>> copying unsigned long into char[4] or char* >>>Look up the C concept "union" in your favorite >>>C textbook or reference manual. >>And promptly invoke undefined behavior? >What undefined behavior? >union { char a[4]; long b; } c; c.b = 1234567; -- read c.a[0..3]; >but be sure to deal with endianess.
The type char can have trap representations. c.b = 1234567; may well store a trap representation in one of the char's and, when you try to read it... The technique works reliably and portably only for arrays of unsigned char, which cannot have trap representations. Dan -- Dan Pop DESY Zeuthen, RZ group
|
Sat, 23 Jul 2005 20:49:53 GMT |
|
|