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

**URL:** <https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883>\
**Category:** ruby-talk\
**Created:** [14 August 2007 08:49 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883 "2007-08-14T08:49:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unbewusst](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@unbewusst](https://rubytalk.org/u/unbewusst)\
**Post date:** [14 August 2007 08:49 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/1 "2007-08-14T08:49:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [14 August 2007 09:31 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/2 "2007-08-14T09:31:50Z")

</div>

The easiest way will probably be to use sprintf. From C, use rb\_f\_sprintf.

> **···**
>
> On Aug 14, 2007, at 01:49, unbewust wrote:
> 
> > 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 ( )
> 
> --  
> Poor workers blame their tools. Good workers build better tools. The  
> best workers get their tools to do the work for them. -- Syndicate Wars

---

<div class="post-metadata">

**Author:** ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)\
**Post date:** [14 August 2007 14:07 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/3 "2007-08-14T14:07:23Z")

</div>

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

> **···**
>
> On 8/14/07, unbewust \<yvon.thoraval@gmail.com\> 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 ( )

---

<div class="post-metadata">

**Author:** ![Simon\_Krahnke](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Simon\_Krahnke](https://rubytalk.org/u/Simon_Krahnke)\
**Post date:** [14 August 2007 15:04 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/4 "2007-08-14T15:04:59Z")

</div>

\* unbewust \<yvon.thoraval@gmail.com\> (10:46) schrieb:

> 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. 🙂

---

<div class="post-metadata">

**Author:** ![unbewusst](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@unbewusst](https://rubytalk.org/u/unbewusst)\
**Post date:** [14 August 2007 12:59 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/5 "2007-08-14T12:59:59Z")

</div>

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 14 août, 11:31, Eric Hodel \<drbr...@segment7.net\> wrote:
> 
> > On Aug 14, 2007, at 01:49, unbewust wrote:
> > 
> > \> 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.
> > 
> > --  
> > Poor workers blame their tools. Good workers build better tools. The  
> > best workers get their tools to do the work for them. -- Syndicate Wars

---

<div class="post-metadata">

**Author:** ![unbewusst](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@unbewusst](https://rubytalk.org/u/unbewusst)\
**Post date:** [14 August 2007 15:35 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/6 "2007-08-14T15:35:04Z")

</div>

> \* unbewust \<yvon.thora...@gmail.com\> (10:46) schrieb:
> 
> \> 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.

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. 🙂

yes i know, i didn't cacht my misspelling 😉

vielen dank 😉

Yvon

> **···**
>
> On 14 août, 16:55, Simon Krahnke \<overl...@gmx.li\> wrote:

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [14 August 2007 16:45 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/7 "2007-08-14T16:45:00Z")

</div>

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

> **···**
>
> On Aug 14, 6:56 am, unbewust \<yvon.thora...@gmail.com\> wrote:
> 
> > On 14 août, 11:31, Eric Hodel \<drbr...@segment7.net\> 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 ?

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [14 August 2007 18:34 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/8 "2007-08-14T18:34:29Z")

</div>

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

> **···**
>
> On Aug 14, 2007, at 05:59, unbewust wrote:
> 
> > On 14 août, 11:31, Eric Hodel \<drbr...@segment7.net\> wrote:
> > 
> > > On Aug 14, 2007, at 01:49, unbewust wrote:
> > > 
> > > > 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.
> > 
> > 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 ?
> 
> --  
> Poor workers blame their tools. Good workers build better tools. The  
> best workers get their tools to do the work for them. -- Syndicate Wars

---

<div class="post-metadata">

**Author:** ![Simon\_Krahnke](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Simon\_Krahnke](https://rubytalk.org/u/Simon_Krahnke)\
**Post date:** [15 August 2007 11:04 UTC](https://rubytalk.org/t/c-ext-to-ruby-how-to-output-a-octal-coded-number/39883/9 "2007-08-15T11:04:58Z")

</div>

\* unbewust \<yvon.thoraval@gmail.com\> (17:31) schrieb:

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