Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to represent a SPACE and a BLANK in HEX in C++?

75 views
Skip to first unread message

ssmi...@gmail.com

unread,
Nov 20, 2013, 8:04:42 PM11/20/13
to
How to represent a SPACE and a BLANK in HEX in C++?

I am using '0x20' for SPACE and '0x00' for BLANK in hex in C++. Am I correct?

Thanks

Alf P. Steinbach

unread,
Nov 20, 2013, 8:19:29 PM11/20/13
to
On 21.11.2013 02:04, ssmi...@gmail.com wrote:
> How to represent a SPACE and a BLANK in HEX in C++?
>
> I am using '0x20' for SPACE and '0x00' for BLANK in hex in C++. Am I correct?

Since in practice all character encodings that you're likely to
encounter are extensions of ASCII, you can just output a space character
in hex in order to check the supposition about that.

cout << hex << +' ' << endl;

Regarding "blank" the same method will work provided that you can find
the "blank" on the keyboard and type it into your source code.

However, I recommend using just literal characters, like ' ' for space
and whatever it is that you have in mind for "blank".


Cheers & hth.,

- Alf

Victor Bazarov

unread,
Nov 20, 2013, 8:20:25 PM11/20/13
to
On 11/20/2013 8:04 PM, ssmi...@gmail.com wrote:
> How to represent a SPACE and a BLANK in HEX in C++?
>
> I am using '0x20' for SPACE and '0x00' for BLANK in hex in C++. Am I correct?

If by "BLANK" you mean NUL, then, AFA ASCII goes, yes. Overkill for 0,
of course. The literal 0 is in fact octal, but the value is the same as
decimal zero or hex (0x0).

The single quotes, hopefully, are only for this post, and you're not
using them in a C++ program.

V
--
I do not respond to top-posted replies, please don't ask
Message has been deleted

Alf P. Steinbach

unread,
Nov 21, 2013, 5:05:48 AM11/21/13
to
On 21.11.2013 03:33, Stefan Ram wrote:
> ssmi...@gmail.com writes:
>> How to represent a SPACE and a BLANK in HEX in C++?
>
> Crosspost, but do not multipost! Do not shout! (Do not use
> uppercase without need.)
>
> »Space« and »blank« are just two different names for the
> same character.

Oh, I didn't think of that (that the OP could have a misconception
here). Hm.


> A hexadecimal numeral is just used for
> textual I/O, not for storage. Storage eventually always
> uses sequences of bits.
>
> When there is onely one character »space« to be stored, but
> no other characters, no runtime storage bits are needed in
> this special case. The program shows an instance r of
> »struct representation« storing a space with 0 bits.
>
> #include <iostream>
> #include <ostream>
> #include <cstdlib>
>
> struct representation {};
>
> ::std::ostream & operator<<
> ( ::std::ostream & stream, representation const & )
> { stream << ' '; return stream; }
>
> int main()
> { representation r;
> ::std::cout << sizeof r << '\n';
> ::std::cout << '[' << r << ']' << '\n'; }
>
> (»sizeof r« was intended to be 0, but it is 1 here.)

Well, every most derived object has size >0, in order to have a unique
address. Further in that direction, the standard guarantees that you get
a unique pointer when you allocate an array of dynamic size 0.

However, a base class sub-object can have size 0. This is known as the
"empty base class optimization", and as I recall it's used for e.g.
std::function and possibly std::unique_ptr (size dependent on the type
of deleter). I don't exactly recall the detailed rules and limitations.

I /think/ that your idea here was in the direction of the
information-theoretical, of bits as a measure of the information content.

Just to recap that, with n ordinary bits you have 2^n bitpatterns, and
so can represent a choice from 2^n possibilities. Turning that around,
if there are N equally likely possibilities for a choice, then you need
log(N)/log(2) bits to represent the choice. And around 1946 Claude
Shannon generalized that and said that instead of N possibilities, we
can think of probability of occurrence 1/N (which is roughly just
another view of the same situation), which then says that one needs
log(N)/log(2) bits to represent the occurrence of something that has
probability 1/N, or just, -log(P)/log(2) bits needed for probability P,
which then can be viewed as if the occurrence of something with
probability P carries -log(P)/log(2) bits of information.

The O(n log n) limit for sorting then follows directly by considering
sorting to be a choice from n! permutations -- and so on.

The whole thing getting much less clear again when one considers how
probability depends on knowledge.

Back to the program, if it always produces the same text, then that text
conveys no information and so corresponds to 0 info-bits. Which means
that ideally it can be reduced to 0 real bits, or thereabouts. Which
might seem impossible, but since the text is then well known it can be
denoted by a single symbol, or even just by the execution of the program
with no output.

And that again getting much less clear when one considers a program that
produces text by means of a /system/, such as repeating a character N
times. Then the question is, how much can one reduce the total size of
the code + data? And turning that around, and asking that of a program
to produce some given pattern, might be a better measure of information
content, but AFAIK we do not yet have any coherent theory of this side
of things (Stephen Wolfram made some beginnings).

Anyway, upshot, you can't get away with zero storage for producing a
single space character; it might seem that you might get close to zero
storage for the limit of average storage per character when you let the
program produce N of them; but then you're really getting into the
problem of information content, how much that's an artifact of the
pattern system's match to the pattern producing device (C++ program).

James Moe

unread,
Nov 21, 2013, 12:39:35 PM11/21/13
to
On 11/20/2013 06:04 PM, ssmi...@gmail.com wrote:
> How to represent a SPACE and a BLANK in HEX in C++?
> I am using '0x20' for SPACE and '0x00' for BLANK in hex in C++. Am I correct?
>
What is the context of your question?
As others have noted, space and blank are synonyms in C/C++ for the
space character, 0x20/32/040. 0x00/0/000 is the C string NUL terminator.
Perhaps you are thinking of whitespace in general? isspace() returns
true for space, htab, vtab, formfeed, carriage return and linefeed.
isblank() is more restrictive by only considering space or htab.
I could drone on about the marvels of space but without a sense of
your application it would be silly.

--
James Moe
jmm-list at sohnen-moe dot com

nick_keigh...@hotmail.com

unread,
Dec 8, 2013, 10:01:07 AM12/8/13
to
On Thursday, 21 November 2013 17:39:35 UTC, James Moe wrote:
> On 11/20/2013 06:04 PM, ssmi...@gmail.com wrote:
>
> > How to represent a SPACE and a BLANK in HEX in C++?
> > I am using '0x20' for SPACE and '0x00' for BLANK in hex in C++. Am I
> > correct?

probably not. 0x20 is ASCII for space. C++ is obliged to use ASCII (though in these latter days its becoming increasingly unlikely to be anything except ASCII or unicode). C++ doesn't have a concept of <blank>. What is <blank>?

In Fortran (if memory serves me right) a <blank> was a <space>.

On paper tape (and cards?) it was an unpunched section, so I assume that would code as 0x00. I suppose some ancient file format uses <blank>

You need to work out or explain to us what a <blank> is and why you wish to use one. This might enable you to work out how to encode it. You may need to distinguish its internal representation from its external representation.

> What is the context of your question?
> As others have noted, space and blank are synonyms in C/C++

really? Is blank mentioned in the standard? <pause> nope it doesn't appear in the index to my copy of the C++ Standard. is this a C++11 thingy?

Jorgen Grahn

unread,
Dec 8, 2013, 10:12:55 AM12/8/13
to
On Sun, 2013-12-08, nick_keigh...@hotmail.com wrote:
> On Thursday, 21 November 2013 17:39:35 UTC, James Moe wrote:
>> On 11/20/2013 06:04 PM, ssmi...@gmail.com wrote:
>>
>> > How to represent a SPACE and a BLANK in HEX in C++?
>> > I am using '0x20' for SPACE and '0x00' for BLANK in hex in C++. Am I
>> > correct?

> probably not. 0x20 is ASCII for space. C++ is obliged to use ASCII
> (though in these latter days its becoming increasingly unlikely to be
> anything except ASCII or unicode).

Clearly you meant to write "... is not obliged to ...".

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Scott Lurndal

unread,
Dec 9, 2013, 11:15:33 AM12/9/13
to
nick_keigh...@hotmail.com writes:
>On Thursday, 21 November 2013 17:39:35 UTC, James Moe wrote:
>> On 11/20/2013 06:04 PM, ssmi...@gmail.com wrote:
>>
>> > How to represent a SPACE and a BLANK in HEX in C++?
>> > I am using '0x20' for SPACE and '0x00' for BLANK in hex in C++. Am I
>> > correct?
>
>probably not. 0x20 is ASCII for space. C++ is obliged to use ASCII (though in these latter days its becoming increasingly unlikely to be anything except ASCII or unicode). C++ doesn't have a concept of <blank>. What is <blank>?
>
>In Fortran (if memory serves me right) a <blank> was a <space>.
>
>On paper tape (and cards?) it was an unpunched section, so I assume that would code as 0x00. I suppose some ancient file format uses <blank>
>

The EBCDIC NUL byte (value 0x00) was punched as 12-0-9-8-1. The EBCDIC BLANK/SPACE (0x40)
was punched as a blank (unpunched) column on Hollerith punched cards.

The ASCII NUL byte (value 0x00) was represented by an unpunched region of paper tape. The
ASCII BLANK/SPACE (value 0x20) was punched as 10100000 (where 1 indicates a punch and
even parity was used).
0 new messages