How to output a octal coded number?

i write a C extension to Ruby, some methods are dealing with octal
number (bytes in english ?) for the file permission :

ie something like that :

0777 or 0644 the first 0 (zero) meaning the following is a number
coded in octal.

that’s OK for input of octal number if for example, from ruby the user
write :

this_file.perms = 0777 (then with the first 0)

i get the right number

however, for the time being, i’m returning, for the same value :

777 instead of 0777 when the user wants to read the perms, then my
question how to return within a C est to Ruby an integer coded in
octal ???

thought, at that time i’m usinbg INT2FIX ( )

best

Yvon

On Aug 14, 2007, at 01:49, unbewust wrote:

write :

thought, at that time i’m usinbg INT2FIX ( )

The easiest way will probably be to use sprintf. From C, use
rb_f_sprintf.

On 14 août, 11:31, Eric H. [email protected] wrote:

coded in octal.
777 instead of 0777 when the user wants to read the perms, then my
best workers get their tools to do the work for them. – Syndicate Wars
OK, thanks, but i don’t want to print it just have it as a return
value then you mean i can use sprintf to print to stdout and the value
will be cached by Ruby ?

this is for cosmetic purpose because my “octal” is well writen except
the first “0” (zero) missing…

On 8/14/07, unbewust [email protected] wrote:

777 instead of 0777 when the user wants to read the perms, then my
question how to return within a C est to Ruby an integer coded in
octal ???

thought, at that time i’m usinbg INT2FIX ( )

Ruby displays integers in base 10 format. I do not believe there is a
way to get it to display 511 in octal (0777) without using sprintf as
Eric H. mentioned in his reply.

However, if you just want to return from a C function and have the
number look pretty in your C source code, then this will work:

return INT2FIX( 0777 );

But I am not sure if that is what you mean?

Blessings,
TwP

that’s OK for input of octal number if for example, from ruby the user
write :

this_file.perms = 0777 (then with the first 0)

i get the right number

however, for the time being, i’m returning, for the same value :

777 instead of 0777 when the user wants to read the perms, then my
question how to return within a C est to Ruby an integer coded in
octal ???

A integer in ruby is a numeric value, it’s not octal or decimal.

thought, at that time i’m usinbg INT2FIX ( )

A string representation of a number can be octal or decimal or whatever.
The Ruby as well and the C library have means to convert between numbers
and its string representations.

num = “775”.to_i(8)
dec = num.to_s #> “509”
oct = num.to_s(8) #> “775”

mfg, simon … l

best

Yvon

BTW: The german word unbewusst is written with two s. :slight_smile:

On Aug 14, 2007, at 05:59, unbewust wrote:

that’s OK for input of octal number if for example, from ruby the
question how to return within a C est to Ruby an integer coded in
octal ???

thought, at that time i’m usinbg INT2FIX ( )

The easiest way will probably be to use sprintf. From C, use
rb_f_sprintf.

OK, thanks, but i don’t want to print it just have it as a return
value then you mean i can use sprintf to print to stdout and the value
will be cached by Ruby ?

sprintf returns a String. You can print it with puts if you want, or
use it later.

On Aug 14, 6:56 am, unbewust [email protected] wrote:

On 14 août, 11:31, Eric H. [email protected] wrote:

The easiest way will probably be to use sprintf. From C, use
rb_f_sprintf.

OK, thanks, but i don’t want to print it just have it as a return
value then you mean i can use sprintf to print to stdout and the value
will be cached by Ruby ?

Despite the name, sprintf does not actually print the value to stdout.
I prefer its more terse version, String#%:

C:>irb
irb(main):001:0> n = 509
=> 509
irb(main):002:0> s = “%04o” % n
=> “0775”

For more info, “ri sprintf” or “ri String#%”

num = “775”.to_i(8)
dec = num.to_s #> “509”
oct = num.to_s(8) #> “775”

i’ve allready a way to output my number as an octal “775” BUT i’ll
like having better “0775” because in the C language the first 0 means
octal and i ask for xhen input .

I don’t quite understand the purpose of your extension. Normally you do
interaction with the user in ruby code.

mfg, simon … l

On 14 août, 16:55, Simon K. [email protected] wrote:

777 instead of 0777 when the user wants to read the perms, then my
question how to return within a C est to Ruby an integer coded in
octal ???

A integer in ruby is a numeric value, it’s not octal or decimal.

thought, at that time i’m usinbg INT2FIX ( )

A string representation of a number can be octal or decimal or whatever.
The Ruby as well and the C library have means to convert between numbers
and its string representations.

OK thanks

the reason for me to output as an octal number it is an habit when
talking about perms…
i’ll look at the C library to output that the correct way.

num = “775”.to_i(8)
dec = num.to_s #> “509”
oct = num.to_s(8) #> “775”

i’ve allready a way to output my number as an octal “775” BUT i’ll
like having better “0775” because in the C language the first 0 means
octal and i ask for xhen input .

mfg, simon … l

best

Yvon

BTW: The german word unbewusst is written with two s. :slight_smile:

yes i know, i didn’t cacht my misspelling :wink:

vielen dank :wink:

Yvon

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs