473,398 Members | 2,427 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,398 software developers and data experts.

extracting integer from array of char

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.
Nov 13 '05 #1
2 3116
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #2

"Mantorok Redgormor" <ne*****@tokyo.com> 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).


Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: hedylogus | last post by:
I need to convert a char array to its corresponding unsigned integer value. For example, if char=x (equivalent to 120) char=y (=121) char=z (=122) then my integer value would be 120121122. ...
7
by: cpp_weenie | last post by:
Given a std::string of the form "default(N)" where N is an integer number of any length (e.g. the literal string might be "default(243)"), what is the quickest way to extract the characters...
3
by: Bill Reyn | last post by:
I am a Java programmer, a newbie with c++, struggling a bit... Why does the code below return the starting address for an integer array, but for a char array it does not return the starting...
27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
12
by: shailashri_sk | last post by:
Hi, int *p; p++; here p now increments itself with the size of integer. similarly, I wanted to know, how to declare an pointer to an array ( say array of integers) where in it we do a p++ it...
13
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
16
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
4
by: msosno01 | last post by:
I have Java client that connects to C++ server. The client sends integer in binary using DataOutputStream write function. I am reading these data into buffer. I have to convert this buffer back...
1
by: Slain | last post by:
I am a beginner and have some confusion with respect to pointers and strings. It seems that the pointers with dealing with integer arrays behave differently, as opposed to strings. Can some one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.