From: "Robert I. Eachus" <rieachus@attbi.com>
Subject: Re: Octal Conversion in VB
Date: Sun, 07 Sep 2003 20:14:50 GMT
Date: 2003-09-07T20:14:50+00:00 [thread overview]
Message-ID: <3F5B91AA.4040203@attbi.com> (raw)
In-Reply-To: m37k4koa3j.fsf@insalien.org
Ludovic Brenta wrote:
> Here is a general algorithm that does what you want. Note that you
> need some mathematical background to understand why there are
> log(N)/log(8) octal digits in any integer number.
I think chrissy was actually looking to convert (ASCII?) characters into
octal triplets. Something more like what follows. (I almost feel I
should apolgize for all the formatting and usage code, because it tends
to obscure what is going on. On the other hand, the conversion to octal
not hex or decimal is chosen by the number in the line with the 8 is
base comment. (Actually for decimal I'd omit the ten and change the
dimensions of Temp, but that is a detail. ;-)
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure Octal is -- a program to convert text strings into octal.
Temp: String(1..6);
begin
if Argument_Count /= 1
then
Put_Line(" Usage: octal ""string""");
Put_Line(" If the string does not contain blanks or other special
command line");
Put_Line(" characters, the parentheses may be omitted.");
Set_Exit_Status(Failure);
return;
end if;
for I in Argument(1)'Range loop
if Col >= 80 then New_Line; end if;
Put(Temp,Character'Pos(Argument(1)(I)),8); -- 8 is base
pragma Debug(Put_Line('"' & Temp & '"'));
if Temp(2) = ' '
then Temp(3..4) := "00";
elsif Temp(1) = ' '
then Temp(3) := '0';
end if; -- change leading blanks to zeros
Put(' ');
Put(Temp(3..5));
end loop;
end Octal;
with output:
E:\Ada\Test>octal "This is a very long string..."
octal "This is a very long string..."
124 150 151 163 040 151 163 040 141 040 166 145 162 171 040 154 157
156 147 040
163 164 162 151 156 147 056 056 056
E:\Ada\Test>octal foobar
octal foobar
146 157 157 142 141 162
E:\Ada\Test>octal
octal
Usage: octal "string"
If the string does not contain blanks or other special command line
characters, the parentheses may be omitted.
--
Robert I. Eachus
"As far as I'm concerned, war always means failure." -- Jacques Chirac,
President of France
"As far as France is concerned, you're right." -- Rush Limbaugh
next prev parent reply other threads:[~2003-09-07 20:14 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-09-07 15:30 Octal Conversion in VB chrissy
2003-09-07 16:08 ` Ludovic Brenta
2003-09-07 20:14 ` Robert I. Eachus [this message]
2003-09-07 20:22 ` David C. Hoos
2003-09-07 23:07 ` Robert I. Eachus
2003-09-08 2:46 ` chrissy
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox