convert a function

L

Larry

Hi,

I would need to convert the following class. It basically converts a
numebr into a binary char 4 bytes long.

#include <sstream>

class hexPack
{
public:
hexPack(int value) : m_value(value) { }
friend std::eek:stream& operator<<(std::eek:stream& os, const hexPack& pack)
{
return os.put((pack.m_value >> 24) & 0xFF).put((pack.m_value >> 16) &
0xFF).put((pack.m_value >> 8) & 0xFF).put(pack.m_value & 0xFF);
}
private:
unsigned m_value;
};

I am using it to save a number into a preference file:

// Write Packed Data //
//
{
std::stringstream k;

std::eek:fstream hfile;
hfile.open(szPrefFile, std::ios::eek:ut|std::ios::binary);
if(hfile.bad())
return -1;

k << hexPack(20); // Top
k << hexPack(25); // Left
k << hexPack(650); // Width
k << hexPack(450); // Height

hfile.write(k.str().c_str(), 4*4);

hfile.close();
}
///////////////////////

Now, since I didn't write that class I would need to figure out a way to
code the viceversa class

thanks
 
T

tonydee

class hexPack
{
public:
 hexPack(int value) : m_value(value) { }
 friend std::eek:stream& operator<<(std::eek:stream& os, const hexPack& pack)
 {
  return os.put((pack.m_value >> 24) & 0xFF).put((pack.m_value >> 16) &
0xFF).put((pack.m_value >> 8) & 0xFF).put(pack.m_value & 0xFF);
 }
private:
 unsigned m_value;
};

Now, since I didn't write that class I would need to figure out a way to
code the viceversa class

std::ifstream hfile... // open your input...

unsigned char a, b, c, d;
if (hfile >> a >> b >> c >> d)
{
int original_value = a << 24 | b << 16 | c << 8 | d;
// do whatever you like with it...
}
 
M

Maxim Yegorushkin

std::ifstream hfile... // open your input...

unsigned char a, b, c, d;
if (hfile>> a>> b>> c>> d)
{
int original_value = a<< 24 | b<< 16 | c<< 8 | d;
// do whatever you like with it...
}

That won't work since there is no whitespace written between hex bytes.

A more direct way:

std::cout << std::hex << value;
std::cin >> std::hex >> value;
 
T

Thomas J. Gritzan

Am 14.02.2010 00:43, schrieb Maxim Yegorushkin:
That won't work since there is no whitespace written between hex bytes.

It won't work but not because of missing white space.

ostream::put writes unformatted output, that is, the chars are written
directly as binary and not converted to the textual representation of
the numbers. To read, use istream::get and not the >>-operator.
A more direct way:

std::cout << std::hex << value;
std::cin >> std::hex >> value;

This outputs the value in textual (hexadecimal) representation and might
not be what the OP wants or needs.
 
T

tonydee

Am 14.02.2010 00:43, schrieb Maxim Yegorushkin:
It won't work but not because of missing white space.

ostream::put writes unformatted output, that is, the chars are written
directly as binary and not converted to the textual representation of
the numbers. To read, use istream::get and not the >>-operator.

The case for which >> doesn't work is when the binary representation
being parsed happens to contain whitespace characters (e.g. 0x23 0x22,
0x21, 0x20 would load a, b and c correctly, but skip over 0x20 then
fail to find a value for d.

get() will indeed read them where >> skips them - I should have used
that.

It's not true that >> expects "textual representations" though... it
parses non-whitespace binary representations correctly, not that
that's useful in this case.

Cheers,
Tony
 
J

James Kanze

[...]
On Feb 14, 9:13 am, "Thomas J. Gritzan" <[email protected]>
wrote:
The case for which >> doesn't work is when the binary
representation being parsed happens to contain whitespace
characters (e.g. 0x23 0x22, 0x21, 0x20 would load a, b and c
correctly, but skip over 0x20 then fail to find a value for d.
get() will indeed read them where >> skips them - I should
have used that.
It's not true that >> expects "textual representations"
though... it parses non-whitespace binary representations
correctly, not that that's useful in this case.

The purpose of the >> operators in iostream is to parse text
into whatever is called for. They operate on text, and only on
text.
 
T

tonydee

The purpose of the >> operators in iostream is to parse text
into whatever is called for.  They operate on text, and only on
text.

That's interesting. Can you cite something (e.g. in the Standard)
explaining this? Seems to me that operator>> to a char is a special
case, namely of lib.istream::extractors "character extraction
templates", whereas other operator>> overloads are
lib.istream.formatted "Formatted input". Simply doing a prior
"mystream >> std::noskipws;" seems like it should make my earlier code
reliable. Quick sanity check with Sparcworks and GNU compilers
worked. (Without noskipws, the characters it gets confused by are
those matching isspace(), namely ASCII 9 through 13 and 32.)

(My assumed context here is ifstream, rather than all possible
instantiations of basic stream templates).

Regards,
Tony
 
J

James Kanze

That's interesting. Can you cite something (e.g. in the Standard)
explaining this?

They're "formatting" extractors. And the only formats supported
are textual formats.
Seems to me that operator>> to a char is a special
case, namely of lib.istream::extractors "character extraction
templates", whereas other operator>> overloads are
lib.istream.formatted "Formatted input".

The standard doesn't make any exceptions for operator>> to a
char. It behaves exactly like any other formatting extractor.
Simply doing a prior "mystream >> std::noskipws;" seems like
it should make my earlier code reliable. Quick sanity check
with Sparcworks and GNU compilers worked. (Without noskipws,
the characters it gets confused by are those matching
isspace(), namely ASCII 9 through 13 and 32.)
(My assumed context here is ifstream, rather than all possible
instantiations of basic stream templates).

Well, it's rather rare to read from an ifstream; you normally
only deal with the istream base when reading. Not that that
changes anything. Because a char is such a basic type, the
"formatting" done by operator>> is rather trivial. But using it
to read "bytes" (raw, unformatted data) will still confuse the
reader; for such data, there are unformatted reader functions.

Note too that in fact, all data is "formatted", in the sense
that it has a format. The unformatted functions are their so
that you can read unformatted data, then parse it according to
some unsupported format. If you're reading a file with a binary
format, of course, you'll want to write your own istream and
ostream, to handle the binary format.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top