473,387 Members | 1,592 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,387 software developers and data experts.

Combining char array into other data types

It's me again:

I have to write some classes that have a pointer to chars and must combine them into other data types. For example:

Expand|Select|Wrap|Line Numbers
  1. void IcInterface::setData(char *inBufr)
  2. {
  3.      float mach = *inBufr;
  4.      inBufr += 4;
  5.      float altitude = *inBufr;
  6.      inBufr += 4;
  7.      short runwayNumber = *inBufr;
  8. }
  9.  
Every method I tried gave me a run-time error on the target machine (a PPC runnning vxWorks).

Any ideas?

Thanks in advance.
Feb 7 '07 #1
5 2048
willakawill
1,646 1GB
Hi, is this one of the ways to tried to make it work?
Expand|Select|Wrap|Line Numbers
  1. void IcInterface::setData(char *inBufr)
  2. {
  3.    float mach = float(*inBufr);
  4.    inBufr += 4;
  5.    float altitude = float(*inBufr);
  6.    inBufr += 4;
  7.    short runwayNumber = short(*inBufr);
  8. }
Feb 7 '07 #2
Hi, is this one of the ways to tried to make it work?
Expand|Select|Wrap|Line Numbers
  1. void IcInterface::setData(char *inBufr)
  2. {
  3.    float mach = float(*inBufr);
  4.    inBufr += 4;
  5.    float altitude = float(*inBufr);
  6.    inBufr += 4;
  7.    short runwayNumber = short(*inBufr);
  8. }
I couldn't get past:

float mach = float(*inBufr);

I tried casting, but didn't get any farther. Moving the data into an array didn't help either. The only one that worked was:

bufrPtr = reinterpret_cast<bool *>( inBufr );

But it only casts a char to bool.
Feb 7 '07 #3
willakawill
1,646 1GB
What could you not get past. The code that I posted works on my machine. What are the values stored in your char array?
Feb 7 '07 #4
horace1
1,510 Expert 1GB
I couldn't get past:

float mach = float(*inBufr);

I tried casting, but didn't get any farther. Moving the data into an array didn't help either. The only one that worked was:

bufrPtr = reinterpret_cast<bool *>( inBufr );

But it only casts a char to bool.
perhaps you have an old C++ compiler, try the C cast
Expand|Select|Wrap|Line Numbers
  1. void IcInterface::setData(char *inBufr)
  2. {
  3.    float mach = (float)*inBufr;
  4.    inBufr += 4;
  5.    float altitude = (float)*inBufr;
  6.    inBufr += 4;
  7.    short runwayNumber = (short)*inBufr;
  8. }
  9.  
Feb 7 '07 #5
What could you not get past. The code that I posted works on my machine. What are the values stored in your char array?
I am getting data from the user interface (a Java program running on a PC) via TCP. When I received Mach which I know to be 0.63, the routine displayed 63. The altitude was 24905, but displayed 70.

I finally got it to work using the following:
Expand|Select|Wrap|Line Numbers
  1. float IcInterface::getMach()
  2. {
  3.   return getFloat(buffer_, 0);      //mach
  4. }
  5.  
  6. float IcInterface::getAltitude()
  7. {  
  8.   return getFloat(buffer_, 4);     //altitude;
  9. }
  10.  
  11. float getFloat(char* buf, int from)
  12. {
  13.     buf+=from;
  14.  
  15.     float tempVal;
  16.     memcpy(&tempVal, buf , sizeof(float));
  17.  
  18.     return tempVal;
  19. }
The code for boolean and short data is similar. The trick was to use memcpy instead of a cast.

Thanks for helping me out.
Feb 8 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
4
by: syncman | last post by:
I want to do a lot of data manipulation and passing. The buffer I have always used before STL was a simple char buf . I can pass its address easily. void f (char *buf) I can assign to...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
28
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
1
by: jackmejia | last post by:
Hello, I been working on an application that need to build a data package to send it over the network, I have an unsigned short as the data length, this is two bytes. On the server side I am...
12
by: Martin Wells | last post by:
I'm trying to come up with a fully-portable macro for supplying memset with an unsigned char rather than an int. I'm going to think out loud as I go along. . . I'll take a sample system before I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.