# Best way to create Bignum from C extension

**URL:** <https://www.ruby-forum.com/t/best-way-to-create-bignum-from-c-extension/115492>\
**Category:** Ruby\
**Created:** [September 25, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/best-way-to-create-bignum-from-c-extension/115492 "2007-09-25T19:33:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 25, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/best-way-to-create-bignum-from-c-extension/115492/1 "2007-09-25T19:33:23Z")

</div>

I’ve been hand-translating some Ruby code into C for an extension and  
have come across the need to create and return a Bignum from the  
extension. I see that there is a macro INT2NUM that will produce a  
Bignum from long or long long, but what is the best way to create an  
arbitrarily sized Bignum from a byte array packed as:

[a, b, c] → (a \* (256 \*\* 0)) + (b \* (256 \*\* 1)) + (c \* (256 \*\* 2))

in other words, reversing the array and flatting it would produce the  
correct binary representation of the number.

I tried creating a Fixnum with INT2FIX(0) and then using rb\_funcalls to  
incrementally construct a Bignum, but that approach 1) didn’t seem to  
work past WORD sized numbers and 2) seems like it would be awfully slow.

–  
Tom Preston-Werner

- Libraries:  
Chronic ([chronic.rubyforge.org](http://chronic.rubyforge.org))  
God ([god.rubyforge.org](http://god.rubyforge.org))  
Fuzed ([fuzed.rubyforge.org](http://fuzed.rubyforge.org))
- Site:  
[rubyisawesome.com](http://rubyisawesome.com)

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 26, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/best-way-to-create-bignum-from-c-extension/115492/2 "2007-09-26T20:41:37Z")

</div>

On Sep 25, 1:32 pm, Tom W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> Fuzed (fuzed.rubyforge.org)
> 
> ```
> 
> - Site:  
> [rubyisawesome.com](http://rubyisawesome.com)

dumb question probably, but could you use scientific notation style  
numbers; i.e., 1.076 \* 10^10? You’d return a base as a float and an  
exponent as a fixnum, and then you could calculate it on the fly…

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 26, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/best-way-to-create-bignum-from-c-extension/115492/3 "2007-09-26T20:50:26Z")

</div>

Tom M wrote:

> > in other words, reversing the array and flatting it would produce the  
> > Chronic ([chronic.rubyforge.org](http://chronic.rubyforge.org))

No, the reason I need to do this is to read in Erlang’s packed data  
representation. Interestingly enough, that _is_ how Erlang transmits  
floats!

–  
Tom Preston-Werner

- Libraries:  
Chronic ([chronic.rubyforge.org](http://chronic.rubyforge.org))  
God ([god.rubyforge.org](http://god.rubyforge.org))  
Fuzed ([fuzed.rubyforge.org](http://fuzed.rubyforge.org))
- Site:  
[rubyisawesome.com](http://rubyisawesome.com)

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 28, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/best-way-to-create-bignum-from-c-extension/115492/4 "2007-09-28T17:06:07Z")

</div>

On Wed, Sep 26, 2007 at 02:32:08AM +0900, Tom W. wrote:

> I tried creating a Fixnum with INT2FIX(0) and then using rb\_funcalls to  
> incrementally construct a Bignum, but that approach 1) didn’t seem to  
> work past WORD sized numbers and 2) seems like it would be awfully slow.

Take a look at the rb\_big\_\* functions declared in intern.h. I think you  
can work something out using them.

Paul

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [September 26, 2007, 9:47pm UTC](https://www.ruby-forum.com/t/best-way-to-create-bignum-from-c-extension/115492/5 "2007-09-26T21:47:15Z")

</div>

On Sep 26, 2:49 pm, Tom W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > - Libraries:  
> > > No, the reason I need to do this is to read in Erlang’s packed data  
> > > [rubyisawesome.com](http://rubyisawesome.com)  
> > > something like breaking up the string into substrings and then calling  
> > > unpack on each substring, then reassemling… think ‘get\_more\_digits’  
> > > since you’re already inclined towards Erlang…
