Octal function returns 3 characters???

Hi,
I thought the idea of the Octal function in Xojo was to return 8 characters (hence the word OCTAL).
According to the LR however, the octal function retuns 400 when a value of 256 is entered???

Can anyone shed some light?
Every text to Octal converter I have found returns 8 characters - but not the Xojo Octal function??

Thank you all in advance.

Octal is base 8. So 400 octal is really 48^2 + 08^1 + 08^0 which is 464 or 256 base 10.

This is along the same lines as Hex or Bin.

http://en.wikipedia.org/wiki/Octal

Thanks, but unfortunately, all those answers were over my head :frowning:
Do you mean that I need to use the Octal function, and then convert that result into Binary, in order to display the 8 digits which all online text to Octal converters display?

I simply need to return 8 digits which all online converters display.

Thanks guys.

That would be good if I could convert VB to Xojo :frowning:

Please share the web link(s) and sample input used and also the expected output for each such sample input.

What http://www.unit-conversion.info/texttools/octal/ does is simply list the ascii value of each character in octal. And the other way around.

Function Text2Octal(s as string) As string dim result as string if s = "" then return s for i as integer = 1 to len(s) result = result+oct(asc(mid(s,i,1)))+" " next return result End Function

Function Octal2Text(s as string) As string dim result as string if s = "" then return s dim myarray(-1) as string myarray = split(s," ") for i as integer = 0 to myarray.Ubound result = result+chr(val("&o"+myarray(i))) next return result End Function

Thanks Michel - I will take a look at your 2 functions.

The general concept seems to make absolutely no sense.
Example:

The site I looked at (which is now down), returned 8 characters for the value of 400.
The link Michel just posted returns 9 characters for the value of 400.
Xojo returns 3 characters for the value of 400.

How on earth are they all returning different results, if they are all converting text to octal?

Thanks.

Maybe post a link to those sites

[quote=132446:@Richard Summers]The general concept seems to make absolutely no sense.
Example:

The site I looked at (which is now down), returned 8 characters for the value of 400.
The link Michel just posted returns 9 characters for the value of 400.
Xojo returns 3 characters for the value of 400.

How on earth are they all returning different results, if they are all converting text to octal?

[/quote]

If you be so kind to post the link to the site that returns 8 characters, maybe an explanation can be found.

I think you fail to understand one thing : when you type “400” at the site I posted, what it does is to actually post the octal numeric value of the ascii value of each character in “400” : 4, then 0, then zero again : 064 060 060

Xojo does a numeric conversion of 400 : 620. No string involved.

Who knows what your site does…

Richard, you are confusing Binary and Octal. Consider the value 33 in various forms

Binary: 100001
Octal: 41
Decimal: 33
Hex: 21

[quote=132456:@Tim Hare]Richard, you are confusing Binary and Octal. Consider the value 33 in various forms

Binary: 100001
Octal: 41
Decimal: 33
Hex: 21[/quote]

He is even confusing the string representation with the numeric one. Must have been awfully tired after a sleepless night, it was 5:00 AM in the UK when he posted :wink:

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.

I can’t reproduce your results. Whether I use Oct( 400 ) or Oct( "400" ), I get back the string “620”.

If I break down “400” to it’s characters and convert the Asc of each to octal, then I get back something similar to your result. That would like an EncodeOct function, but that doesn’t exist natively in Xojo.

Kem - the following website: converts 400 into 064 060 060.
Michel kindly explained it was due to an ASCII text string being converted to Octal. bUt you get 620 for both text AND numeric?

This is what is confusing me?
Different people get different results?

Thanks.

Yes, me too, 620 whether it is a number or text.

A little off topic, wouldn’t that be a bug? The documentation only mentions integer as value passed to the fucntion.

Julen

Also digressing slightly - but if Octal differentiates between text and numerics - do Binary and Base64 also both differentiate between text and numeric values passed to them respectively?

For example - does a text string of 25 converted to binary produce a different value than if a numeric of 25 gets converted to binary??

Thanks.

The results are consistent, but the function that emulates the website doesn’t exist in Xojo so you’d have to write it.

Within Xojo, Oct takes a double or a string representation of a double, then converts that to a string representation of an Octal value. As I mentioned, this is along the same lines of what Bin and Hex do (take a number and convert it to a string representation of the value in base 2 and base 16, respectively).

The function that would do the same as the web site is:

Function EncodeOct(s As String, withSpaces As Boolean = False) As String
  if s = "" then
    return ""
  end if
  
  dim source() as string = s.Split( "" )
  dim arr() as string
  for i as integer = 0 to source.Ubound
    dim value as string = Oct( source( i ).Asc )
    arr.Append Right( "000" + value, 3 )
  next i
  
  dim space as string = if( withSpaces, " ", "" )
  dim r as string = join( arr, space )
  
  return r
End Function