# Write as Binary?

Question asked by VRFuser on Apr 18, 1996
Hi all

Here's something that has me stumped, though the answer is probably
obvious to someone!---

I would like to write a number out as a specified number of
binary digits (typically 4, 8, 16, or 32).

e.g. I have a number 7 and would like to write it as   0111
(4 ascii digits).

Using the I/O transactions I can format as Octal, hex or decimal but
not binary.

About the only way I can find to even see binary is to use an
alphanumeric disply, but it is fixed width, has leading #B and has
no output anyhow.

I could write a brute force decimal-to-banry conversion, but that
seems silly, so I'm looking for a simple way.

So- any ideas?

Stan

----------------------------------------------------------------------
Stan Bischof   Hewlett Packard Company   707-577-3994  stanb@sr.hp.com
----------------------------------------------------------------------

from: Greg Goebel / HP-MXD
gvg@lvld.hp.com / 970-679-3030 / FAX 970-679-5971
to:   Stan Bischoff - VRf(?)
date: Thursday, 18 April 1996 1554 MDT

Is this for VRf?  If so, please mark the title of messages with the string
"VRf" to reduce confusion ... a header (as above) would also be useful.

>
> Hi all
>
> Here's something that has me stumped, though the answer is probably
> obvious to someone!---
>
> I would like to write a number out as a specified number of
> binary digits (typically 4, 8, 16, or 32).
>
>   e.g. I have a number 7 and would like to write it as   0111
>        (4 ascii digits).
>
> Using the I/O transactions I can format as Octal, hex or decimal but
> not binary.
>
> About the only way I can find to even see binary is to use an
> alphanumeric disply, but it is fixed width, has leading #B and has
> no output anyhow.
>
> I could write a brute force decimal-to-banry conversion, but that
> seems silly, so I'm looking for a simple way.
>
> So- any ideas?
>
>
> Stan
>
>
> ----------------------------------------------------------------------
> Stan Bischof   Hewlett Packard Company   707-577-3994  stanb@sr.hp.com
> ----------------------------------------------------------------------

[15.1] CONVERTING A NUMBER TO A BINARY STRING IN VEE

* For some odd reason (that nobody seems to recollect) you can use transaction
objects in VEE to convert a number (say, 233) to an octal string ("351") or
hex string ("0E9") ... but you can't convert one to binary string format
("011101001" and so on).

So I devised a function of my own, called "CvtBin", to do this.  It has the
overall layout:

+-----------------------------------------------------------------------+
|                             CvtBin                                    |
+---+---------------------------------------------------------------+---+
|   |       +------------------------------+                        |   |
|   |       |          To String           |                        |   |
|   |       +---+----------------------+---+      +--------+        |   |
|   |   +-->| A | WRITE TEXT a OCT EOL | R +--+-->| strLen +--+     |   |
|   |   |   +---+----------------------+---+  |   +--------+  |     |   |
|   |   |                                     |               |     |   |
|   |   |   +---------------------------------|---------------+     |   |
|   |   |   |                                 |                     |   |
|   |   |   |                  +--------------+                     |   |
|   |   |   |                  |                                    |   |
|   |   |   |   +-----------+  +-->+---------+   +-------------+    |   |
| A +---+   +-->| For Count +----->| Formula +-->| Accumulator +--->| R |
|   |           +-----------+  +-->+---------+   +-------------+    |   |
|   |                          |                                    |   |
|   |      +---------------+   |                                    |   |
|   |      |     Text      |   |                                    |   |
|   |      +---------------+   |                                    |   |
|   |      | 0000: 000     |   |                                    |   |
|   |      | 0000: 001     |   |                                    |   |
|   |      | 0000: 010     |   |                                    |   |
|   |      | 0000: 011     +---+                                    |   |
|   |      | 0000: 100     |                                        |   |
|   |      | 0000: 101     |                                        |   |
|   |      | 0000: 110     |                                        |   |
|   |      | 0000: 111     |                                        |   |
|   |      +---------------+                                        |   |
+---+---------------------------------------------------------------+---+

The Formula box is central to this function:

+-------------------------------------+
|               Formula               |
+---+------------------------+--------+
| A |                        |        |
| B | C[StrFromLen(A, B, 1)] | Result |
| C |                        |        |
+---+------------------------+--------+

The basic philosophy here is that the input value is converted to an octal
string, and then each digit ("0" through "7") is converted to its binary
string equivalent ("000" through "111"); these triplets are then concatenated
together and shot out the output pin.

In more detail:

% The input value is converted to an octal string by a To String object.

% The length of this octal string (the number of digits to be converted) is
then determined by a "strLen" string function and used to load a For Count
object, which will sequence through the digits in the octal string.

% A Text constant is used to provide an array giving the 8 possible binary
triplets needed to express the octal digits to binary.  The output of this
constant (an array), the octal string, and the output of the For Count
object is fed to a Formula object containing "C[StrFromLen(A, B, 1)]".

Since "A" is the octal string and "B" is the For Count counter value, the
"StrFromLen" function extracts each digit from the octal string in turn;
this digit is then used as an index to the array "C", which represents the
array of binary-triplet strings.

If this makes you dizzy, all it does is take each digit out of the octal
string sequentially and map it to the appropriate binary equivalent for
that digit.

% Each output triplet is summed up by the Accumulator object.  When the
For Count object counts out, the function has nothing left to do and
exits, with the final value on the output pin.

This works efficiently, but some caution must be taken in handling its output;
VEE tends to convert back and forth between strings and numbers in an
invisible fashion, and your string "1011100" will most likely evaluate to the
decimal value 1,011,100.  Be careful.

[%%]