🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #583

Re: hex data from a file

#583
From: David Ballentine
Date: Thu, 20 Jan 2000 00:00
55 lines
2995 bytes
Alex Sramek <losal4@mminternet.com> wrote in message
news:s8f7ufsreh6162@corp.supernews.com...
> Hey, can anyone tell me how to read plain old hex data from a file?  Do
you
> use fstream?  What methods do you use?  What kind of variables do you read
> it into?
>
> Thanks!
>
> --
> -Alex Sramek
>

Dear Alex,

I'm not clear as to what you mean by reading "hex data from a file".

In either C or C++, a file is regarded as a store of bytes, and a stream is
used to read those bytes from the file into memory for further manipulation.

If you wish to interpret the bits contained within one byte as two
successive hex digits, then you may do so, if you write the code yourself. I
don't believe there are any C/C++ standard functions to extract the hex
digits from a byte.

A way to do so would be to bit-wise AND the byte [11110000] with the byte
from the stream, then shift it right four bits; to extract the low-order hex
digit, do a bit-wise AND of [00001111] and the byte from the stream.

Unless it's for a programming course assignment or in writing a debugger,
dealing with hexadecimal digits is rarely necessary, since they're really
just an artificial concept invented because we humans aren't terribly adept
at dealing with binary numbers. Computers only really understand binary
numbers.

But mortals such as we would much rather deal with CB-0E-A8-DF than the 32
digit binary number:
  11001011000011101010100011011111

I think you would be well-served if you would post the code of your program
so far, and possibly a listing of your data file.

Regards,
Dave
===================================
David Ballentine, Assistant Professor, Computer Technology
Westmoreland County Community College, Youngwood PA 15697
ballendr@westol.com  ballend@astro.westmoreland.cc.pa.us
www.theGnats.com     www.westmoreland.cc.pa.us
FAQ for acllc-c++:   http://www.raos.demon.co.uk/acllc-c++/faq.html

> "You couldn't hear a dump truck driving through a nitroglycerine plant."
> --Uncle Louis, "Christmas Vacation"
>
>
Technical Details
Message-ID: <868e3g$jv5$1@bgtnsc02.worldnet.att.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <s8f7ufsreh6162@corp.supernews.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114592,83e5b246eafba510
X-Google-Attributes: gid114592,public
X-Google-Thread: 109fba,83e5b246eafba510
X-Google-Attributes: gid109fba,public
X-Google-Thread: 103c8d,83e5b246eafba510
X-Google-Attributes: gid103c8d,public
From: "David Ballentine" <DaveBallentine@worldnet.att.net>
Subject: Re: hex data from a file
Date: 2000/01/20
Message-ID: <868e3g$jv5$1@bgtnsc02.worldnet.att.net>
X-Deja-AN: 575534103
References: <s8f7ufsreh6162@corp.supernews.com>
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
X-Complaints-To: abuse@worldnet.att.net
X-Trace: bgtnsc02.worldnet.att.net 948419504 20453 12.76.91.55 (21 Jan 2000 01:51:44 GMT)
Organization: AT&T WorldNet Services
X-MSMail-Priority: Normal
NNTP-Posting-Date: 21 Jan 2000 01:51:44 GMT
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cppbuilder.language,comp.lang.c++