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

BYTE datatype used for an unknown amount of data

Hi,

I am looking to find out how to deal with the BYTE datatype in VC, as
used below:

This struct is part of the API for a Garmin GPS unit:

typedef struct
{
unsigned char mPacketType; // 0 1 byte
unsigned char mReserved1; // 1 1 byte
unsigned short mReserved2; // 2,3 2 bytes
unsigned short mPacketId; // 4,5 2 bytes
unsigned short mReserved3; // 6,7 2 bytes
unsigned long mDataSize; // 8-11 4 bytes
BYTE mData[1]; // 12+ x bytes
} Packet_t;

The last element mData holds a variable amount of data the size of
which is stored in mDataSize. I am unsure how to deal with it.

In one case, I need to store an unsigned short in it, so I have
written:

mDataSize = sizeof(unsigned short);
mData[0] = 5;

But this does not seem to work... can anyone help?

(Visual Studio .NET 2002, Intel Centrino)

thanks,

Aengus.

Nov 17 '05 #1
2 1387
Hi Aengus
<ae****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi,

I am looking to find out how to deal with the BYTE datatype in VC, as
used below:

This struct is part of the API for a Garmin GPS unit:

typedef struct
{
unsigned char mPacketType; // 0 1 byte
unsigned char mReserved1; // 1 1 byte
unsigned short mReserved2; // 2,3 2 bytes
unsigned short mPacketId; // 4,5 2 bytes
unsigned short mReserved3; // 6,7 2 bytes
unsigned long mDataSize; // 8-11 4 bytes
BYTE mData[1]; // 12+ x bytes
} Packet_t;

The last element mData holds a variable amount of data the size of
which is stored in mDataSize. I am unsure how to deal with it.

In one case, I need to store an unsigned short in it, so I have
written:

mDataSize = sizeof(unsigned short);
mData[0] = 5;

But this does not seem to work... can anyone help?

(Visual Studio .NET 2002, Intel Centrino)

thanks,

Aengus.


Your code would cast the int value 5 to a BYTE and assign it to mData.

Try this:

Packet_t* p = (Packet_t*)AllocSomeHow(sizeof(Packet_t) - sizeof(BYTE) +
sizeof(int);
*((int*)(&p->mData)) = 5;

Nov 17 '05 #2
Thanks Marcus,

That seems to work perfectly. It was an unsigned short, rather than an
int, so the code now reads:

Packet_t* p = (Packet_t*)malloc(sizeof(Packet_t) - sizeof(BYTE) +
sizeof(unsigned short));

*((unsigned short*)(&p->mData)) = (unsigned short)5;

Now to understand that...

Nov 17 '05 #3

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

Similar topics

4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
2
by: Tommie Nygren | last post by:
For different reasons I am reading an array of bytes from a file in my ..jar-file and from this I want to create a PNG image using Image.createImage(myArray, 0 , myArray.length). I had a...
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
16
by: Samuel Thomas | last post by:
Hello Friends, I understand(could be wrong) that the smallest chunk of memory is called a word. If that is correct, that means if I am using a 32 bit OS a word is 4 bytes. So that's why the size...
9
by: rahul | last post by:
Hi!, I have a java program that receives data via sockets from a C program. The java program has to recieve the data in byte format. I need to send character arrays from the C side after...
1
by: al | last post by:
hi, I have a table that contain a field "amount" its datatype is numeric(18,4) in sql server 2000 I have created a Datagrid, when I edit a record, I get an error like this one BC30456:...
9
by: Pohihihi | last post by:
How can I change object returned from database to a byte array e.g. byte b = row; also how can I save a byte to database row?
45
by: Ajay | last post by:
Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
7
by: StupidScript | last post by:
>From the manual "Storage Requirements": "ENUM('value1','value2',...) =1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)" This seems to mean: "a" = 1 byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.