Hi,
I am reading a file that contains a bunch of unsigned longs.
The number itself is broken down in bits, for example
bit 31-24 is a certain code and bit 7-6 represents a certain state.
am I right in doing the following
// read the unsigned long from the file
unsigned long ulDataFromFile = 0;
/*
... read it
*/
// define some values, code and state
unsigned short usCode = 0; // size = 7 'cause we are reading bit 31-24
unsigned char ucState = 0; // size = 1 'cause we are reading bit 7-6
// use the unsigned long to read the data itself.
memcpy( &usCode, ((unsigned char*)ulDataFro mFile)+24, sizeof(unsigned
char) ); // read bit 31-24
memcpy( &ucState , ((unsigned char*)ulDataFro mFile)+7, sizeof(unsigned
short ) ); // read bit 7-6
Am I right?
I cannot really test the data as I am not sure what values to expect inside
the unsigned long
Many thanks in advance.
Simon 4 5518
Simon schrieb: Hi,
I am reading a file that contains a bunch of unsigned longs. The number itself is broken down in bits, for example
bit 31-24 is a certain code and bit 7-6 represents a certain state.
am I right in doing the following
// read the unsigned long from the file unsigned long ulDataFromFile = 0; /* ... read it */ // define some values, code and state unsigned short usCode = 0; // size = 7 'cause we are reading bit 31-24 unsigned char ucState = 0; // size = 1 'cause we are reading bit 7-6
The actual sizes don't matter as long as they're large enough to hold
the value. BTW, an unsigned char is still enough to hold 7 bits... // use the unsigned long to read the data itself. memcpy( &usCode, ((unsigned char*)ulDataFro mFile)+24, sizeof(unsigned char) ); // read bit 31-24
No, this reads a byte from 24 *bytes* after the start of ulDataFromFile:
undefined behavious
memcpy( &ucState , ((unsigned char*)ulDataFro mFile)+7, sizeof(unsigned short ) ); // read bit 7-6
Similarly here.
Am I right?
I'm afraid you aren't.
// right shift by 24 bits to discard bits 0-23
// then mask out anything beyond bit 31 (which is now bit 7)
usCode = ( ulDataFromFile >> 24 ) & 0xff;
// right shift by 6 bits to discard bits 0-5
// then mask out anything beyond bit 7 (which is now bit 1)
ucState = ( ulDataFromFile >> 6 ) & 0x03; I cannot really test the data as I am not sure what values to expect inside the unsigned long
You can easily try by setting ulDataFromFile to a made-up value and
compare the results to what you'd expect...
e.g. if ulDataFromFile is 0x12345678, usCode must afterwards be 0x12 and
ucState must be 0x01
HTH,
Malte
"Simon" <sp********@myo ddweb.com> wrote in message
news:3a******** *****@individua l.net Hi,
I am reading a file that contains a bunch of unsigned longs. The number itself is broken down in bits, for example
bit 31-24 is a certain code and bit 7-6 represents a certain state.
am I right in doing the following
// read the unsigned long from the file unsigned long ulDataFromFile = 0; /* ... read it */ // define some values, code and state unsigned short usCode = 0; // size = 7 'cause we are reading bit 31-24 unsigned char ucState = 0; // size = 1 'cause we are reading bit 7-6
// use the unsigned long to read the data itself. memcpy( &usCode, ((unsigned char*)ulDataFro mFile)+24, sizeof(unsigned char) ); // read bit 31-24 memcpy( &ucState , ((unsigned char*)ulDataFro mFile)+7, sizeof(unsigned short ) ); // read bit 7-6
Am I right?
I cannot really test the data as I am not sure what values to expect inside the unsigned long
Many thanks in advance.
Simon
Try this:
#include <cmath>
class BitExtractor
{
public:
BitExtractor(un signed long data) : databits(data)
{}
unsigned long Extract(size_t lowBit, size_t highBit) const
{
int digitCount = highBit - lowBit + 1;
unsigned long mask = (unsigned long)pow((int)2 , digitCount) - 1;
return (databits>>lowB it) & mask;
}
private:
unsigned long databits;
};
Use it as follows:
BitExtractor be(ulDataFromFi le);
// to get bits from index 6 to 7
unsigned long six_to_seven = be.Extract(6,7) ;
// to get bits from index 24 to 31
unsigned long twentyfour_to_t hirtyone = be.Extract(24,3 1);
--
John Carson
<snip> John Carson
Many thanks John and Malte, I guess I was confused with Bytes and bits.
Simon
Malte Starostik wrote: Simon schrieb: bit 31-24 is a certain code and bit 7-6 represents a certain state.
unsigned long ulDataFromFile = 0;
memcpy( &usCode, ((unsigned char*)ulDataFro mFile)+24, sizeof(unsigned char) ); // read bit 31-24
No, this reads a byte from 24 *bytes* after the start of ulDataFromFile: undefined behavious
That would be: ((unsigned char *)&ulDataFromFi le)+24 . The OP code
is far worse than that. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dietrich Epp |
last post by:
Are there any good modules for reading a bitstream? Specifically, I
have a string and I want to be able to get the next N bits as an
integer. Right now I'm using struct.unpack and bit operations, it's a
bit kludgy but it gets the right results.
Thanks in advance.
|
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 someone can
help
Thanks
|
by: Ben |
last post by:
Hi,
I need to write some data types into an array of unsigned chars.
These are basically "signals" within a message, so each signal will have
a start bit and a length. The signals will also have a type, one of:
unsigned long
signed long
float
double
|
by: Mantorok Redgormor |
last post by:
Would a portable method of handling 32 bits of data from a file, be to
use something like unsigned long(I need to read in 32-bits of data)?
or is there some smaller type that guarantees at least 32 bits of
value bits?
Also, off topic but does anyone know how to count all frames from an
mp3 file? If each frame size is always the same then that would be
easier and I could just read in all records of a fixed size. I believe
this is how the...
|
by: Digital Puer |
last post by:
Hi, suppose I have an unsigned long long. I would like to extract
the front 'n' bits of this value and convert them into an integer.
For example, if I extract the first 3 bits, I would get an int between
0 and 7 (=2^3-1). Could someone please help out?
I can assume the largest returned value fits in an int. Also,
I'm on a big-endian PPC (AIX), in case that matters.
Ideally, I'd like to implement a prototype like:
int...
| |
by: steve yee |
last post by:
i want to detect if the compile is 32 bits or 64 bits in the source
code itself. so different code are compiled respectively. how to do
this?
|
by: gene kelley |
last post by:
I have an application where I need to read some header information found in 3
different types of file types. Two of the file types were fairly straight forward as
the items to read in the header are at least 8 bits (one byte), so, I'm able to step
through a file stream with a binary reader and retrive the data. The last file type,
however, has me stumped at the moment. The header spec specifies the item lengths in
bits. Most of the...
|
by: Jm.GlezdeRueda |
last post by:
Hi all,
Im trying to read a 24bit bmp with fread, and i have some problems.. I
want to read the whole structure in one time, but i dont know why, it
only reads the first member well..
I have two questions..
1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1,
sizeof(struct bmp), 1, fin); i have a Segment violation ??
|
by: rsk |
last post by:
Hi Friends,
My requirement is as follows;
A file is consisting of data in hexadecimal format(i.e a 32 bit data for
example like "0xdeadbeef").
I have to read each of such data into my 'c' code
and i need to assign them to a 32 bit integer array.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |