[C ext to Ruby] how to output a octal coded number ???

U

unbewust

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
 
E

Eric Hodel

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 ( )

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

unbewust

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 ?

this is for cosmetic purpose because my "octal" is well writen except
the first "0" (zero) missing...
 
T

Tim Pease

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 Hodel 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
 
S

Simon Krahnke

* unbewust said:
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. :)
 
U

unbewust

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


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



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

yes i know, i didn't cacht my misspelling ;-)

vielen dank ;-)

Yvon
 
P

Phrogz

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#%"
 
E

Eric Hodel

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 =20=

use it later.
 
S

Simon Krahnke

* unbewust said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top