Octal function returns 3 characters???

Input: Octal number 400
Required output: Decimal equivalent
Result: 256

Input: Octal number 400
Required output: ASCII of each digit of input octal number, when taken as a character.
Result: 064 060 060, here the number 064 has base 8 so the decimal equivalent is 52. And 060 is 48 in decimal.
Result in Decimal: 52 48 48, here decimal 52 is the ASCII value of character 4 and decimal 48 is the ASCII of character 0.

Firstly - thank you very much for that function Kem - much appreciated!

Secondly, which is the more standard approach to converting to Octal - the website way, of the Xojo way?
Thanks.

[quote=132501:@Richard Summers]Firstly - thank you very much for that function Kem - much appreciated!

Secondly, which is the more standard approach to converting to Octal - the website way, of the Xojo way?
Thanks.[/quote]
What is the requirement? Convert a decimal number to octal number?

Your question is like asking whether the standard approach is walking or driving. It depends on whether you’re going from your house to the mall, or from your kitchen to your living room, right?

Octal and binary do the same thing, convert a number (usually expressed in a decimal number system or “base 10”), to another number system, octal and binary respectively. In the base 8 system numbers go like this: 1 2 3 4 5 6 7 and then 10 11 12 13 14 15 16 17 20 21 … so 10 in the octal system is 8 in the normal system (base 10). Oct is doing that conversion and returning the string representing the number in the octal system.

You can do that conversion with the ascii value (base 10) of each character too, as you are doing in that website. It’s up to you to choose what you want.

Base64 functions only work with text, not with numbers. So these are different functions, and they do different things.

Julen

Kem - what I meant was, in all practicality, a correct online converter SHOULD give the user the option of entering either a string OR a numeric. If they do not make this clear - the end-user would get an unexpected result.

To rephrase my question…would a text to Octal be more widely used (thus expected) than a numeric to Octal - (hope that made more sense) ??? But I guess you are saying it is impossible to say what someone else wants to do.

Thanks.

Although Kem’s code is probably better (certainly faster for long strings), I think this code here shows better what the function is doing:

[code]Function EncodeOct(s As String) As String

if len(s)>0 then
dim i as Integer
dim TempStr as string
For i=1 to len(s)
TempStr=TempStr+" " + format(Val(oct(asc(mid(s,i,1)))), “000”)
next
return TempStr
else
return “”
end if

End[/code]

Julen

Julen, as an aside, Len on a large string can be very slow. Use LenB at least, but I’ve timed it and comparing the variable to “” is faster in all cases.

I posted that way above already :wink:

Ok - to sum this up:

Some Octal converters will convert a string to Octal and produce one result, and another Octal converter will convert a numeric to Octal and produce a different result.

Some people will want to convert a string, and others will want to convert a numeric. So at the end of the day, it is one or the other.

Thanks.

[quote=132513:@Richard Summers]Ok - to sum this up:

Some Octal converters will convert a string to Octal and produce one result, and another Octal converter will convert a numeric to Octal and produce a different result.

Some people will want to convert a string, and others will want to convert a numeric. So at the end of the day, it is one or the other.

Thanks.[/quote]

By definition, the web site says “string to octal”, meaning the series of characters, would that be “teddy bear” or “400”.

“400” is a string, 400 is a numeric value. Val(“400”) is a numeric value, str(400) is a string…

Indeed, it is one or the other. It is important to remember that in a program.

I can’t think of a case where I’d want to convert a string to an octal, but maybe that’s just me.

Ok - so my last post is basically correct.
Thank you all very much for all your help!

[quote=132487:@Richard Summers]OK - I am now fresh and awake :slight_smile:

So if I have understood this correctly:

If I am converting a text string of 400 to Octal - it will return one value, and if I am converting a numeric string of 400 - it will return another. I never realised it would differentiate between text and numerics.

So if 33 returns an Octal of 41 and 400 returns an Octal of 064 060 060, can the result ever be more (or less) than 2 or 3 characters? Example - could an Octal ever return just one digit, or return 4 digits for a single text / numeric character?

At the moment, this is simply for educational purposes, and I am trying to understand the concept.
Thanks - I appreciate the help.[/quote]
Normally we count in base 10
Octal counts in Base 8
Both are still positional numbering systems though

So in base 10 you count 1,2,3, etc up to 9 and when you get to 10 you add a “position” and write 1 followed by the lowest digit in the numbering system - 0. Hence you write 10. Ten.
And this is 1*10^1 + 0 *10^0

In Octal you only have 8 digits (0,1,2,3,4,5,6,7) and you do the “carry” when you get to 8
So in octal you count 1,2,3,4,5,6,7 and now you have to move up to the next position so you write 1 followed by 0
So in Octal 10 means “8” in base 10
This is 1*8^1 + 0 *8^0
9 in octal is 8 + 1 or 11 - which is 9 in base 10

Base 2 is similar except it uses 2 digits (0 & 1)

And you do this EVERY day without even thinking about it
Base 60 :stuck_out_tongue:
60 seconds make 1 minute
60 minutes make 1 hour
etc :stuck_out_tongue:

NOW Can it have less than 3 digits - sure
There are some conventions for writing numbers in certain programming languages though
But it certain possible to write 10(base 8) and it makes perfect sense

Thanks Norman.
It is hard to understand this stuff sometimes, when you are not really a math oriented person :frowning:
I understand what you wrote. I was originally confused due to one converter converting text and another converting numerics.

The usual usage is converting a number to the octal representation as a string. The website is doing something that would be considered out of the ordinary. I can’t think of a real-world usage for that.

Thanks Tim.
I guess I shouldn’t really rely on websites to work as expected :slight_smile: