Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Copy 4 byte block into a char array

Reply
Thread Tools

Copy 4 byte block into a char array

 
 
bg_ie@yahoo.com
Guest
Posts: n/a
 
      06-21-2006
Hi,

I have an array as follows -

char arr[100];

Now I wish to copy the following int -

int tmp = 0x01020304;

into this array, at element[12].

This following would have the same result -

ThePacket3[12] = 1;
ThePacket3[13] = 2;
ThePacket3[14] = 3;
ThePacket3[15] = 4;

How can I do this without breaking up my int into 4 bytes and assigning
each element one by one?

Thanks,

Barry.

 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      06-21-2006
(E-Mail Removed) wrote:
> ThePacket3[12] = 1;
> ThePacket3[13] = 2;
> ThePacket3[14] = 3;
> ThePacket3[15] = 4;
>
> How can I do this without breaking up my int into 4 bytes and assigning
> each element one by one?


Write a function or macro.

It's called "modular design" where instead of copy/pasting the same
code snippet you found on snippets.org you actually build up a program
from a design where you abstract out common functionality to a body,
almost a "library" if you will, of support code for your application or
project.

Tom

 
Reply With Quote
 
 
 
 
Nelu
Guest
Posts: n/a
 
      06-21-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> Hi,
>
> I have an array as follows -
>
> char arr[100];
>
> Now I wish to copy the following int -
>
> int tmp = 0x01020304;
>
> into this array, at element[12].
>
> This following would have the same result -
>
> ThePacket3[12] = 1;
> ThePacket3[13] = 2;
> ThePacket3[14] = 3;
> ThePacket3[15] = 4;
>
> How can I do this without breaking up my int into 4 bytes and assigning
> each element one by one?
>


You could probably try this:

memcpy(&arr[12],&tmp,sizeof(int));

Just make sure you know exactly what you're doing. For example int
doesn't need to be 4 byte long.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      06-21-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I have an array as follows -
>
> char arr[100];
>
> Now I wish to copy the following int -
>
> int tmp = 0x01020304;
>
> into this array, at element[12].
>
> This following would have the same result -
>
> ThePacket3[12] = 1;
> ThePacket3[13] = 2;
> ThePacket3[14] = 3;
> ThePacket3[15] = 4;
>
> How can I do this without breaking up my int into 4 bytes and assigning
> each element one by one?


You can't, portably.

1. The endianness of a particular machine may cause the bytes to be assigned
to different chars than what you've listed above.
2. element[12] may not have proper alignment for an int, causing a crash.
3. sizeof(int) may not be 4, causing more than 4 elements of your array to
be modified.

This is why folks write macros (which assign the bytes in a specific order)
for such needs.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
James
Guest
Posts: n/a
 
      06-21-2006
On Wed, 21 Jun 2006 13:17:46 -0700, bg_ie wrote:
> int tmp = 0x01020304;
>
> into this array, at element[12].
>
> This following would have the same result -
>
> ThePacket3[12] = 1;
> ThePacket3[13] = 2;
> ThePacket3[14] = 3;
> ThePacket3[15] = 4;
>
> How can I do this without breaking up my int into 4 bytes and assigning
> each element one by one?
>


if you know that you're on a big-endian machine and sizeof char * 4 ==
sizeof int, you could theoretically do:

int *slice;
slice = &arr[12];
*slice = tmp;

but that's obviously unportable as hell

 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      06-21-2006
Stephen Sprunk wrote:
> > How can I do this without breaking up my int into 4 bytes and assigning
> > each element one by one?

>
> You can't, portably.


....sorta.

In my projects I try to autodetect certain platforms and use memcpy if
possible, otherwise yeah I extract bytes and store in order.

Tom

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-21-2006
(E-Mail Removed) writes:
> I have an array as follows -
>
> char arr[100];
>
> Now I wish to copy the following int -
>
> int tmp = 0x01020304;
>
> into this array, at element[12].
>
> This following would have the same result -
>
> ThePacket3[12] = 1;
> ThePacket3[13] = 2;
> ThePacket3[14] = 3;
> ThePacket3[15] = 4;
>
> How can I do this without breaking up my int into 4 bytes and assigning
> each element one by one?


int isn't necessarily 4 bytes, bytes aren't necessarily 8 bits, and
the value 0x01020304 isn't necessarily stored in the order 1, 2, 3, 4;
it's commonly 4, 3, 2, 1, but it could theoretically be in any of the
24 possible orders.

If you want to break the value of tmp down into 4 8-bit quantities,
and store then in successive elements of your array (it it called
"arr" or "ThePacket3"?), the only portable way to do it is by copying
each byte individually, after extracting them using shifts and masks.

If you're willing to make non-portable assumptions about how an int is
represented, you can use memcpy(), but your code will break when
ported to a system with different byte ordering.

If you're trying to guarantee network byte ordering, there are
functions that will handle this for you ("ntohl" and friends), but
they're not part of standard C. If your system has them, searching
your documentation for "ntohl" should be enough to get you started.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-22-2006
On 21 Jun 2006 21:00:19 +0000, Nelu <(E-Mail Removed)> wrote:

>(E-Mail Removed) writes:
>
>> Hi,
>>
>> I have an array as follows -
>>
>> char arr[100];
>>
>> Now I wish to copy the following int -
>>
>> int tmp = 0x01020304;
>>
>> into this array, at element[12].
>>
>> This following would have the same result -
>>
>> ThePacket3[12] = 1;
>> ThePacket3[13] = 2;
>> ThePacket3[14] = 3;
>> ThePacket3[15] = 4;
>>
>> How can I do this without breaking up my int into 4 bytes and assigning
>> each element one by one?
>>

>
>You could probably try this:
>
>memcpy(&arr[12],&tmp,sizeof(int));
>
>Just make sure you know exactly what you're doing. For example int
>doesn't need to be 4 byte long.


And make sure that an int on the system is big-endian otherwise the
result will not be as specified.


Remove del for email
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      06-22-2006
On Wed, 21 Jun 2006 16:26:03 -0500, James <(E-Mail Removed)>
wrote:

>On Wed, 21 Jun 2006 13:17:46 -0700, bg_ie wrote:
>> int tmp = 0x01020304;
>>
>> into this array, at element[12].
>>
>> This following would have the same result -
>>
>> ThePacket3[12] = 1;
>> ThePacket3[13] = 2;
>> ThePacket3[14] = 3;
>> ThePacket3[15] = 4;
>>
>> How can I do this without breaking up my int into 4 bytes and assigning
>> each element one by one?
>>

>
>if you know that you're on a big-endian machine and sizeof char * 4 ==
>sizeof int, you could theoretically do:
>
> int *slice;
> slice = &arr[12];
> *slice = tmp;


You need a cast on the pointer assignment and the code will invoke
undefined behavior if arr[12] is not properly aligned for an int.

>
>but that's obviously unportable as hell



Remove del for email
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-22-2006
(E-Mail Removed) wrote:
>
> I have an array as follows -
>
> char arr[100];
>
> Now I wish to copy the following int -
>
> int tmp = 0x01020304;
>
> into this array, at element[12].
>
> This following would have the same result -
>
> ThePacket3[12] = 1;
> ThePacket3[13] = 2;
> ThePacket3[14] = 3;
> ThePacket3[15] = 4;
>
> How can I do this without breaking up my int into 4 bytes and assigning
> each element one by one?


Assuming your specs are accurate, and you change the type of tmp to
unsigned long, you can do it independant of endianism etc. by using
values, not bit patterns:

unsigned char ThePacket[100];
unsigned long tmp;
int i;

for (i = 3; i >= 0; i--, tmp /= 256)
ThePacket[12 + i] = tmp % 256;

You should be using unsigned items for both tmp and ThePacket. tmp
must be a long to guarantee 32 bits available.

If it can the compiler will probably optimize the modulo and
division operations into shifts and masks. Writing portable code
isn't all that hard, is it?

Notice that the values 3 and 12 above can be defined as FIELDSZ and
FIELDLOCN, or whatever nomenclature suits you.

--
"I don't know where bin Laden is. I have no idea and really
don't care. It's not that important." - G.W. Bush, 2002-03-13
"No, we've had no evidence that Saddam Hussein was involved
with September the 11th." - George Walker Bush 2003-09-17


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
How to byte[ ] --> char[ ] and char[ ] --> byte[ ]? Richard Java 11 02-01-2006 07:34 AM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM



Advertisments