By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

Octal Integer Constants

P: n/a
Hi,

I have a whole bunch of integer constants that are best given in octal
(PDP-1 opcodes). It makes a huge difference in readability and authenticity
if these
can be entered in octal. I haven't found a way to do this. Is there one?

Thanks,
Gary
May 26 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Fri, 25 May 2007 19:03:48 -0700, Gary Brown <ga********@charter.net>
wrote:
I have a whole bunch of integer constants that are best given in octal
(PDP-1 opcodes). It makes a huge difference in readability and
authenticity
if these can be entered in octal. I haven't found a way to do this. Is
there one?
C# doesn't have octal literals. However, a couple of possible workarounds:

* VB does support octal literals (use "&O" before the number) so you
could define your constants in a VB.NET class and reference them from C#

* Depending on how the literals are to be used, you could just enter
them as hexadecimal literals that read visually as octal (sort of like
BCD) and then have some code that preprocesses them by taking out the
4-bit gaps in the binary values. I think this would work best if you're
using the literals to initialize an array or something like that, but even
if you're using them for named constants, you could "fake it" by instead
defining fields that are preprocessed at run-time (sorry if that sounds
oxymoronic...I hope you get what I mean :) ).

Using them as named constants, I think you'd have to gain a lot of
readability to justify the hack, since you'd sacrifice the minor
performance gain actual literal constants get you, as well as the
read-only nature of constants, and of course you'd have to have a block of
code somewhere that goes through and does the run-time preprocessing. All
of that's a pain; I would probably just write the hex, and if readability
was an issue I'd include the octal value in a comment next to it.

But for initializing an array, I think even the second method would work
pretty well. :)

Why you're looking for "authenticity" in source code, especially when
writing PDP-related code in C#, I'm not really clear on. But I'll take
your word for it that that's a desirable goal for some reason. :)

Pete
May 26 '07 #2

P: n/a
On Fri, 25 May 2007 20:48:44 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
* Depending on how the literals are to be used, you could just
enter them as hexadecimal literals that read visually as octal (sort of
like BCD) and then have some code that preprocesses them by taking out
the 4-bit gaps in the binary values. [...]
Of course, I meant 1-bit gaps. Sorry if my mistyping confused things. :(
May 26 '07 #3

P: n/a
"Gary Brown" <ga********@charter.netwrote in message
news:O9**************@TK2MSFTNGP03.phx.gbl...
Hi,

I have a whole bunch of integer constants that are best given in octal
(PDP-1 opcodes). It makes a huge difference in readability and
authenticity if these
can be entered in octal. I haven't found a way to do this. Is there one?
How about this:

const int O_11 = 9;
const int O_10167 = 4215;

--
LTP

:)
May 26 '07 #4

P: n/a
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 25 May 2007 20:48:44 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
>[...]
* Depending on how the literals are to be used, you could just
enter them as hexadecimal literals that read visually as octal (sort of
like BCD) and then have some code that preprocesses them by taking out
the 4-bit gaps in the binary values. [...]

Of course, I meant 1-bit gaps. Sorry if my mistyping confused things. :(
Or (possibly better) enter them as strings and write your own routine to
convert them to integers in octal.
May 26 '07 #5

P: n/a
>I have a whole bunch of integer constants that are best given in octal
>(PDP-1 opcodes). It makes a huge difference in readability and
authenticity
if these can be entered in octal. I haven't found a way to do this. Is
there one?

* Depending on how the literals are to be used, you could just enter
them as hexadecimal literals that read visually as octal (sort of like
BCD) and then have some code that preprocesses them by taking out the
That is essentially what I did. I had

const int add = 0400000,

which I transformed to a call

ConvertOp(SW, "add", 0400000);

with a macro. "Convert" converted the decimal 0400000 to hex then
output it with the mnemonic to a file. I pasted the file contents back
into the source yielding

const int add = 0x20000, // 0400000

Using Basic as someone suggested was also a good idea but I
don't have Basic installed.

It would have been so much easier if literals beginning with 0 were
octal as I originally thought.

Thanks,
Gary
May 26 '07 #6

P: n/a
It would have been so much easier if literals beginning with 0 were
octal as I originally thought.
I think you've stumbled onto the only legitimate use of octal within the
past 30 years! :)
May 26 '07 #7

P: n/a
Gary Brown wrote:
Hi,

I have a whole bunch of integer constants that are best given in octal
(PDP-1 opcodes). It makes a huge difference in readability and authenticity
if these
can be entered in octal. I haven't found a way to do this. Is there one?
Surprisingly:

private const int c1 = 011;

does not work as expected.

But:

private static readonly int c2 = Convert.ToInt32("011", 8);

could be a possible workaround.

Arne
May 26 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.