ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 09 Mar 2015 15:12:18 -0500How do I deal with large, hex numbers in Sage?http://ask.sagemath.org/question/26061/how-do-i-deal-with-large-hex-numbers-in-sage/ How do I deal with large, hex numbers in Sage (say ones with 256 or 512 bytes)? How would I import them from a CSV file?
(cf. [this related answer](http://ask.sagemath.org/question/24708/how-to-enter-very-large-numbers/?answer=24710#post-id-24710))Sat, 07 Mar 2015 10:47:47 -0600http://ask.sagemath.org/question/26061/how-do-i-deal-with-large-hex-numbers-in-sage/Answer by tmonteil for <p>How do I deal with large, hex numbers in Sage (say ones with 256 or 512 bytes)? How would I import them from a CSV file?</p>
<p>(cf. <a href="http://ask.sagemath.org/question/24708/how-to-enter-very-large-numbers/?answer=24710#post-id-24710">this related answer</a>)</p>
http://ask.sagemath.org/question/26061/how-do-i-deal-with-large-hex-numbers-in-sage/?answer=26068#post-id-26068You can deal with `.csv ` files by importing the [csv module](https://docs.python.org/2/library/csv.html).
Hexadecimals are represented by numbers starting with `0x`, see [this page](http://www.sagemath.org/doc/reference/rings_standard/sage/rings/integer.html) :
sage: 0x100
256
sage: 0x123
291
To transform an hexadecimal string representation `s` of a number to a Sage integer (that can be very long), you can do:
sage: s = '123'
sage: ZZ('0x'+s)
291
If you need more informations, you should provide a sample of your csv file and the result you would expect.
Sat, 07 Mar 2015 16:13:16 -0600http://ask.sagemath.org/question/26061/how-do-i-deal-with-large-hex-numbers-in-sage/?answer=26068#post-id-26068Comment by Geremia for <p>You can deal with <code>.csv</code> files by importing the <a href="https://docs.python.org/2/library/csv.html">csv module</a>.</p>
<p>Hexadecimals are represented by numbers starting with <code>0x</code>, see <a href="http://www.sagemath.org/doc/reference/rings_standard/sage/rings/integer.html">this page</a> :</p>
<pre><code>sage: 0x100
256
sage: 0x123
291
</code></pre>
<p>To transform an hexadecimal string representation <code>s</code> of a number to a Sage integer (that can be very long), you can do:</p>
<pre><code>sage: s = '123'
sage: ZZ('0x'+s)
291
</code></pre>
<p>If you need more informations, you should provide a sample of your csv file and the result you would expect.</p>
http://ask.sagemath.org/question/26061/how-do-i-deal-with-large-hex-numbers-in-sage/?comment=26111#post-id-26111Here's what I did, and it works very well:
import csv mylist=[] with open('~/Downloads/datasmall.txt', 'rb') as csvfile: data = csv.reader(csvfile) for row in data: mylist.append([int(row[1]),float.fromhex(row[2])])Mon, 09 Mar 2015 15:12:18 -0500http://ask.sagemath.org/question/26061/how-do-i-deal-with-large-hex-numbers-in-sage/?comment=26111#post-id-26111