Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > extracting integer from array of char

Reply
Thread Tools

extracting integer from array of char

 
 
Mantorok Redgormor
Guest
Posts: n/a
 
      10-19-2003
If I have an array of char and there is an integer located in that
char that was assigned by:
int foo=10;
*(int *)&array[position] = foo;

How can I extract that integer? Any portable way?
I can assign using the above method but can't retrieve it with the
above method.

Also, what is a portable method of sending and receiving a struct that
contains bitfields across a network to two different machines? one is
linux on x86 the other is solaris-sparc.. any idea? Very frustrating
problem.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-19-2003
Mantorok Redgormor wrote:
>
> If I have an array of char and there is an integer located in
> that char that was assigned by:
> int foo=10;
> *(int *)&array[position] = foo;


Bad. Casts are evil. Very likely to cause UB due to alignment
restrictions. Assuming array is an array of char, all you need
is:

array[position] = foo;

It's up to you to ensure that foo is in the range a char can
hold. You find out by testing against the values in limits.h.

>
> How can I extract that integer? Any portable way?
> I can assign using the above method but can't retrieve it with
> the above method.


by:
foo = array[position];
>
> Also, what is a portable method of sending and receiving a
> struct that contains bitfields across a network to two different
> machines? one is linux on x86 the other is solaris-sparc.. any
> idea? Very frustrating problem.


Extract each field, convert it to a text representation, and send
that. The binary image on one machine need not have any
particular relationship to the binary image on another machine.

The recommended method even allows you to follow the process
during operation

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      10-19-2003

"Mantorok Redgormor" <(E-Mail Removed)> wrote in message
> If I have an array of char and there is an integer located in that
> char that was assigned by:
> int foo=10;
> *(int *)&array[position] = foo;
>
> How can I extract that integer? Any portable way?
>

This is pretty horrible. If the same program does the assignment and
extraction, you can use memcpy()
memcpy(&foo, &array[position], sizeof(int)).
>
> I can assign using the above method but can't retrieve it with the
> above method.
>

That's surprising. If the compiler allows a cast one way it should allow it
in the reverse direction.
>
> Also, what is a portable method of sending and receiving a struct that
> contains bitfields across a network to two different machines? one is
> linux on x86 the other is solaris-sparc.. any idea? Very frustrating
> problem.
>

You are not guaranteed the same alignment for any structs (except within the
same program, of course).
You need to break the structure down into its elements, and send it one byte
at a time (all integers 32 bits big-endian, all chars ascii, all floats in
32-bit IEEE format, etc).




 
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
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(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
char->integer, integer->char commands christophergraber@gmail.com Perl Misc 5 11-22-2005 07:13 AM



Advertisments