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??
Thanks, but unfortunately, all those answers were over my head
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.
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?
[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.
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
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.
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?
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??
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