473,563 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading 16-bit raw data

Hi

I need to read raw 16 bit data from a file, where the first byte is the
most significant byte of the first data value and the second byte is
the least significant byte of the first data value (the next pair or
bytes and the next etc. specify the second and third values).

For example, if I had a file containing

0xff22
0xfe23
0x11ff
0x1000

then the two values I should read back would be:

0x01002023
and
0x00120F00

I wrote the following code snippet which I thought would read the data:

// The input and output variables are ifstream and ofstream objects
respectively.
char currentByte[1]; // A buffer for reading the file.
unsigned int thisValue;
bool oddByte = true; // The first byte is odd-numbered, the second is
even-numbered, etc.
while(input->good()) // While we can extract data from the input
file.
{
// Get the next byte, and write the next value if the byte read
is an even byte.
input->read(currentBy te, 1); // Probably inefficient, but I'll
speed things up later.
if(oddByte)
{
thisValue = (unsigned int)(*currentBy te); // Most significant byte.
*output << "No mult.: thisValue is: " << thisValue << std::endl; //
Debug noise.

thisPixel *= 256; // Equiv. to leftshift by 8 places.
*output << "Current thisValue is: " << thisVaule << std::endl; //
Debugg noise.
}
else
{
thisValue += (unsigned int)(*currentBy te); // Least significant
byte.
*output << "Current thisValue is: " << thisValue << std::endl; //
Debug noise.
*output << thisValue << " "; // Write out the computed value
in decimal.
}

// Invert oddByte.
oddByte = !oddByte;
}

If the above is a bit impenetrable, here's my thinking: each value in
the file is represented by two bytes, an odd byte (the MSB) and an even
byte (the LSB). oddByte is inverted after every read. If we just read a
MSB, then we need to left-shift the bits read by 8 binary places (I do
a multiply by 256 above, but it's the same thing). If we just read a
LSB, then we need to add this onto the result of doing the left-shift
by 8. After adding the LSB I write out the value to an output stream
(the other writes are just for debugging purposes).

I compile the above using gcc -Wall and it compiles cleanly. The code
runs OK, but I don't get out what I expect; instead I get values like:

4294967229
4294950144
4294950263
4294967241
4294953216
4294953122

which are far too big to have come from 16-bit values. (They look like
pointer addresses to me.)

It's been a long while since I've needed to do any bit-twiddling (or
used C++ in anger). Which bit of basic C/C++ have I forgotten?

Thanks,

C

Dec 15 '05 #1
8 7239
ju***@microserf .org.uk wrote:
[..]
I compile the above using gcc -Wall and it compiles cleanly. The code
runs OK, but I don't get out what I expect; instead I get values like:

4294967229
4294950144
4294950263
4294967241
4294953216
4294953122
Display them in hex.
which are far too big to have come from 16-bit values. (They look like
pointer addresses to me.)
They are just large numbers (0xffff....).
It's been a long while since I've needed to do any bit-twiddling (or
used C++ in anger). Which bit of basic C/C++ have I forgotten?

Perhaps you need to have your "currentbyt e" as _unsigned_char_ ...

V
Dec 15 '05 #2

ju***@microserf .org.uk wrote:
thisValue = (unsigned int)(*currentBy te); // Most significant byte.
*output << "No mult.: thisValue is: " << thisValue << std::endl; //
Debug noise.


It's the conversion - try instead:

thisValue = (unsigned int)(*currentBy te & 0xff)
, and it' will work.

/Patrick

Dec 15 '05 #3
On 15 Dec 2005 13:14:24 -0800, lo******@kth.se wrote:

ju***@microser f.org.uk wrote:
thisValue = (unsigned int)(*currentBy te); // Most significant byte.
*output << "No mult.: thisValue is: " << thisValue << std::endl; //
Debug noise.


It's the conversion - try instead:

thisValue = (unsigned int)(*currentBy te & 0xff)
, and it' will work.


Don't you mean:

thisValue = ((unsigned int)(*currentBy te)) & 0xff;

??

--
Bob Hairgrove
No**********@Ho me.com
Dec 15 '05 #4
Hi all, and thanks for your responses.

A few minutes after posting I tried using the "int get()" function
instead of the "get(char*) " version, and things seem to work fine.

C

Dec 15 '05 #5

Bob Hairgrove wrote:
On 15 Dec 2005 13:14:24 -0800, lo******@kth.se wrote:

ju***@microser f.org.uk wrote:
thisValue = (unsigned int)(*currentBy te); // Most significant byte.
*output << "No mult.: thisValue is: " << thisValue << std::endl; //
Debug noise.


It's the conversion - try instead:

thisValue = (unsigned int)(*currentBy te & 0xff)
, and it' will work.


Don't you mean:

thisValue = ((unsigned int)(*currentBy te)) & 0xff;


This is not necessary if currentByte were of type pointer to unsigned
char, rather than plain char! Use unsigned char for binary data.

An unsigned char will promote to signed int, or to unsigned int,
depending on whichever will hold all values of its type, including
sign.

On most platforms, unsigned char will promote to int, because it's
narrower, and so int can hold all unsigned char values.

The only remaining issue then is that the & 0xFF may be done against a
signed int zero value that doesn't have an all-bits-zero
representation. Under sign-magnitude, you are okay, because a negative
zero still has all-zero bits in the mantissa, but under one's
complement, zero can be represented by a bit pattern of all 1's.
Masking out the least significant 8 bits of that produces 255.

But even on hardware that uses one's complement for signed integers, I
wouldn't expect an unsigned char zero to promote to the negative flavor
of zero! That zero would have to be the result of some arithmetic
computation involving negative values.

So basically, you are worried to portability to one's complement
machines on which conversions are pathologically behaved.

If hardware like that were designed to day, it wouldn't see the light
of day. The reams of nonportable code that would not run on it would
seal its fate in the marketplace. :)

Dec 15 '05 #6
ju***@microserf .org.uk wrote:

I need to read raw 16 bit data from a file, where the first byte is the
most significant byte of the first data value and the second byte is
the least significant byte of the first data value (the next pair or
bytes and the next etc. specify the second and third values).

For example, if I had a file containing

0xff22
Do you mean the file contains 0xFF followed by 0x22 ?
0xfe23
0x11ff
0x1000

then the two values I should read back would be:

0x01002023
and
0x00120F00


Does anyone else follow this? I haven't had my coffee today

Dec 16 '05 #7

Old Wolf wrote:
ju***@microserf .org.uk wrote:

I need to read raw 16 bit data from a file, where the first byte is the
most significant byte of the first data value and the second byte is
the least significant byte of the first data value (the next pair or
bytes and the next etc. specify the second and third values).

For example, if I had a file containing

0xff22


Do you mean the file contains 0xFF followed by 0x22 ?
0xfe23
0x11ff
0x1000

then the two values I should read back would be:

0x01002023
and
0x00120F00


Does anyone else follow this? I haven't had my coffee today


You're right - it should of course be 0xff followed by 0x22. In his
example he used 16bit numbers, shifted the first one 8 bits and added
the next one.
(0xff22 << 8) + 0xfe23 = 0x1002023

Dec 16 '05 #8
>>Do you mean the file contains 0xFF followed by 0x22 ?
0xfe23
0x11ff

0x1000
then the two values I should read back would be:

0x01002023
and
0x00120F00


Does anyone else follow this? I haven't had my coffee today


Obviously neither had I! Sorry all---I was 'forgetting' that one hex
digit was four bits, rather than two, but I hope the example served its
purpose.

Dec 16 '05 #9

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

Similar topics

19
10283
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly...
5
18633
by: Jamie | last post by:
I have a file that was written using Java and the file has unicode strings. What is the best way to deal with these in C? The file definition reads: Data Field Description CHAR File identifier (64 bytes corresponding to Unicode character string padded with '0' Unicode characters. CHAR File format version (32 bytes corresponding...
3
2750
by: Tony Lugg | last post by:
I have an application with a document management form. When users add documents to the form, I call the API function SHGetFileInfo to get the associated large and small icons for the file. These icons are added to two ImageList objects which are bound to a ListView control, and everything looks great. I am saving the icons to a SQL Server...
10
2120
by: Johhny | last post by:
Hello All, I am working my way through learning python as a language. I am having some issues with something that looks right and does not work. I am trying to get myself more familure with reading files. Based on the tutorials at www.python.org This "should" work. but im not sure what the issue is. ===SNIP=== import string
2
3238
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish between plain ASCII files I receive and the Unicode ones would be to check if the first two bytes read 0xFFFE. But nothing I do seems to be able...
1
4272
by: JRD | last post by:
Greetings, I would like to search down through the following xml string that is returned to my calling app via a webservice. What I am trying to get is the following section from the xml string <component><section><code code="8716-3" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="VitalSigns" /><title>Vital...
4
2072
by: GeekBoy | last post by:
I am reading a file of numbers using for loops. The numbers are in a grid as follows: 8 36 14 11 31 17 22 23 17 8 9 33 23 32 18 39 23 25 9 38 14 38 4 22 18 11 31 19 16 17 9 32 25 8 1 23
4
2205
by: creeds | last post by:
Hello !! i have a text file, from where i have to perform an operation such as awk for every lines. i can count the number of lines with cat filename | wc -l , no problem with that. my scenario: i take a input from a file, i count the length ofcertian paramater with substring and do the if else condition. but i have to perform this action...
4
3352
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and i'm experiencing unexpected behavior using fread with a binary file. Look at this code: i have a jpeg image of 2290 bytes long, but fread cannot...
15
14446
by: itdevries | last post by:
Hi, I'm trying to read some binary data from a file, I've read a few bytes of the data into a char array with ifstream. Now I know that the first 4 bytes in the char array represent an integer. How do I go about converting the elements to an integer? regards, Igor
0
7665
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, 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...
0
7950
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 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...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.