* Octal Conversion in VB
@ 2003-09-07 15:30 chrissy
2003-09-07 16:08 ` Ludovic Brenta
0 siblings, 1 reply; 6+ messages in thread
From: chrissy @ 2003-09-07 15:30 UTC (permalink / raw)
I was playing around in VB trying to come up with a simple application
to convert entered strings of alphanumerics into octal numbers.
I took the approach of declaring a series of variables as integers,
and calling the library functions "Val" and "Str" to handle the data
conversions from the text box I was using to key in the alphanumerics
and to the text box I was assigning the resulting string.
I then wrote a series of expressions to assign values to each of the
declared variables, in sequence.
I then concatenated the resultant character string to produce the
number sequence.
Thus:
Private Sub cmdConvert_Click()
Dim A, B, C, D as Integer
A = Val(txtDecimal.Text)
B = A Mod 8
C = A \ 8 Mod 8
D = A \ (8 ^ 2)
txtOctal.Text = Str(D) + Str (C) + Str (B)
End Sub
This seemed to work, but the code looked ugly, and I'm not sure a
truth table would support the conversion to infinity. Am I right?
Is there a better and tidier way?
Hoping for some guidance
Chrissy
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Octal Conversion in VB
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
0 siblings, 1 reply; 6+ messages in thread
From: Ludovic Brenta @ 2003-09-07 16:08 UTC (permalink / raw)
chrissy_brady1@yahoo.com (chrissy) writes:
> I was playing around in VB trying to come up with a simple application
^^^^^^
See, comp.lang.ada? This illustrates my earlier point about
disposable developers and disposable software. Chrissy, this is not
against you, but why do you want a "simple" application? Is it not
better to write a "correct" and "maintainable" application instead?
> to convert entered strings of alphanumerics into octal numbers.
>
> This seemed to work, but the code looked ugly, and I'm not sure a
> truth table would support the conversion to infinity. Am I right?
>
> Is there a better and tidier way?
>
>
> Hoping for some guidance
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.
HTH
--
Ludovic Brenta.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
procedure Decimal_To_Octal is
function Read_Number return Integer is
S : String (1 .. 100);
Last : Natural;
Result : Integer;
begin
Get_Line (S, Last);
Get (From => S (1 .. Last), Item => Result, Last => Last);
return Result;
end Read_Number;
Number_To_Convert : Integer := Read_Number;
begin
Put (Item => Number_To_Convert); -- First print it as decimal
Put (Item => Number_To_Convert, Base => 8); -- Print as octal.
Put_Line;
-- An alternative that doesn't use the Ada representation for octals:
declare
Number_Of_Digits : constant Integer :=
1 + Integer (Log (Float (Number_To_Convert)) / Log (8.0));
Octal_Digits : array (1 .. Number_Of_Digits) of Integer := (others => 0);
begin
for I in reverse Octal_Digits'Range loop
Octal_Digits (I) := Number_To_Convert mod 8;
Number_To_Convert := Number_To_Convert / 8;
end loop;
-- Now loop forward to print them
for I in Octal_Digits'Range loop
Put (Item => Octal_Digits (I), Width => 1);
end loop;
end;
exception
when Data_Error =>
Put_Line ("Please input a number in decimal.");
end Decimal_To_Octal;
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Octal Conversion in VB
2003-09-07 16:08 ` Ludovic Brenta
@ 2003-09-07 20:14 ` Robert I. Eachus
2003-09-07 20:22 ` David C. Hoos
2003-09-08 2:46 ` chrissy
0 siblings, 2 replies; 6+ messages in thread
From: Robert I. Eachus @ 2003-09-07 20:14 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Octal Conversion in VB
2003-09-07 20:14 ` Robert I. Eachus
@ 2003-09-07 20:22 ` David C. Hoos
2003-09-07 23:07 ` Robert I. Eachus
2003-09-08 2:46 ` chrissy
1 sibling, 1 reply; 6+ messages in thread
From: David C. Hoos @ 2003-09-07 20:22 UTC (permalink / raw)
"Robert I. Eachus" <rieachus@attbi.com> wrote in message
news:3F5B91AA.4040203@attbi.com...
<snip>
> 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;
Didn't you mean ""quotation marks" instead of "parentheses"?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Octal Conversion in VB
2003-09-07 20:22 ` David C. Hoos
@ 2003-09-07 23:07 ` Robert I. Eachus
0 siblings, 0 replies; 6+ messages in thread
From: Robert I. Eachus @ 2003-09-07 23:07 UTC (permalink / raw)
David C. Hoos wrote:
> "Robert I. Eachus" <rieachus@attbi.com> wrote in message
> news:3F5B91AA.4040203@attbi.com...
> <snip>
>
>> 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;
>
> Didn't you mean ""quotation marks" instead of "parentheses"?
>
Oops! Yes. That is why code reviews are important. There are some bugs
that no amount of testing can catch. ;-)
--
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Octal Conversion in VB
2003-09-07 20:14 ` Robert I. Eachus
2003-09-07 20:22 ` David C. Hoos
@ 2003-09-08 2:46 ` chrissy
1 sibling, 0 replies; 6+ messages in thread
From: chrissy @ 2003-09-08 2:46 UTC (permalink / raw)
"Robert I. Eachus" <rieachus@attbi.com> wrote in message news:<3F5B91AA.4040203@attbi.com>...
> 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 actually wanted to produce a string of alphanumerics representing
the integer value of a Base 10 integer, converted to Base 8. Thus the
ASCII string entered to a text box in VB "94" ultimately returns the
string "136".
Thanks so much for the suggestion. It's ingenious, but I'm stuck with
VB for the moment. Another friend suggested the following for VB:
Dim DecimalStr As String
Dim OctalStr As String
Dim HexadecimalStr As String
DecimalStr = InputBox ("Enter decimal value")
OctalStr = Oct$(DecimalStr)
HexadecimalStr = Hex$(DecimalStr)
MsgBox (OctalStr)
MsgBox (HexadecimalStr)
As a safety mechanism to avoid problems with null values, you could
use these lines instead of the corresponding ones above:
OctalStr = Oct$(DecimalStr & " ")
HexadecimalStr = Hex$(DecimalStr & " ")
This of course does much more than I'd asked, as it's handling
hexadecimals as well.
The same friend also offered me a solution in Pascal:
I've attached a generic base conversion program in Pascal for bases
from 2 to 36 but there's no error handling. These are fairly standard
algorithms. The programing term is 'cliches'. It converts from any
base to any base.
Louie.
-----------------------------------------------------------------------------
Tabs set to 4 spaces
{
baseconvert.pas - Base convertor of integers
Luis Esteban 8 September 2003
Created without error checking
}
program BaseConvertor (input, output);
var
InputNumber : string;
OutputNumber : string;
InputBase : integer;
OutputBase : integer;
Value : longint;
function StringToInteger (Number : string; Base : integer) : longint;
var
Accumulator : longint;
i : integer;
begin
Accumulator := 0;
for i := 1 to Length(Number) do
begin
{writeln ('Number[', i:1, '] = ', Number[i]);}
if Number[i] in ['0'..'9'] then
begin
Accumulator := Accumulator * Base +
(ord(Number[i]) - ord('0'));
end
else
if Number[i] in ['A'..'Z'] then
begin
Accumulator := Accumulator * Base + (10 +
ord(Number[i]) - ord('A'));
end
else
if Number[i] in ['a'..'z'] then
begin
Accumulator := Accumulator * Base + (10 +
ord(Number[i]) - ord('a'));
end
else
begin
writeln ('Ignoring ''', Number[i], '''');
end;
end;
StringToInteger := Accumulator;
end;
function ReverseString (s : string) : string;
var
i : integer;
t : string;
begin
t := '';
for i := Length(s) downto 1 do
begin
t := t + s[i];
end;
ReverseString := t;
end;
function IntegerToString (Number : longint; Base : integer) : string;
var
r : string;
NewDigit : integer;
begin
r := '';
while Number > 0 do
begin
NewDigit := Number mod Base;
if NewDigit < 10 then
begin
r := r + chr(NewDigit+ord('0'));
end
else
if NewDigit < 36 then
begin
r := r + chr(NewDigit+ord('A')-10);
end;
Number := Number div Base;
end;
IntegerToString := ReverseString (r);
end;
begin
write ('Enter number: ');
readln (InputNumber);
write ('Enter input base: ');
readln (InputBase);
write ('Enter output base: ');
readln (OutputBase);
writeln;
Value := StringToInteger(InputNumber, InputBase);
OutputNumber := IntegerToString(Value, OutputBase);
writeln (InputNumber,'(',InputBase,') = ',Value,'(10) =
',OutputNumber,'(',OutputBase,')');
end.
Again this is more than I'd asked for. I'm so impressed with the
software community. You guys are just so generous!!
With Lots of Thanks and other Sincere signals of fratitude
Chrissy
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-09-08 2:46 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2003-09-07 20:22 ` David C. Hoos
2003-09-07 23:07 ` Robert I. Eachus
2003-09-08 2:46 ` chrissy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox