Base Conversion

1,258 views
Skip to first unread message

Eviatar

unread,
Nov 11, 2010, 1:25:58 AM11/11/10
to sage-devel
I coded a simple function to convert any base to another, and I want
to submit it as a patch. Where in the source code could this fit?

Thanks in advance.

David Roe

unread,
Nov 11, 2010, 2:45:33 AM11/11/10
to sage-...@googlegroups.com
This can currently be done by

sage: a = ZZ("sage",base=32); a
928270
sage: a.str(base=32)
'sage'
sage: a.str(base=36)
'jw9a'

For bases outside of 2..36, you can work with lists:

sage: b = ZZ([4,1,7],base=100)
sage: b
70104
sage: b.digits(base=71)
[27, 64, 13]
sage: 27 + 64*71 + 13*71^2
70104

For a function that makes this functionality easier to access, I would suggest sage/rings/arith.py.  If you want to modify this behavior in some ways, then you should take a look at sage/rings/integer.pyx.
David


--
To post to this group, send an email to sage-...@googlegroups.com
To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Eviatar

unread,
Nov 11, 2010, 12:13:17 PM11/11/10
to sage-devel
Thank you,

I wasn't aware of that functionality. However, I think my function is
more convenient since you can directly convert bases, instead of using
base 10 as an intermediary step.

Nick Alexander

unread,
Nov 11, 2010, 12:23:53 PM11/11/10
to sage-devel
sage: ZZ(15).digits(2)
[1, 1, 1, 1]
sage: ZZ(15).digits(3)
[0, 2, 1]

Nick

Eviatar

unread,
Nov 11, 2010, 1:39:12 PM11/11/10
to sage-devel
Yes, but can you go directly from base 2 to base 4, for example? I
guess this would work, ZZ(23,base=2).str(base=4), but it's not very
convenient.

Nick Alexander

unread,
Nov 11, 2010, 1:56:46 PM11/11/10
to sage-devel
sage: ZZ([1, 2], base=3)
7
sage: ZZ([1, 2], base=3).digits(2)
[1, 1, 1]

My opinion is that this is short and already well supported, and much
more general than the special function OP suggests.

Nick
Reply all
Reply to author
Forward
Message has been deleted
0 new messages