Ask Your Question
0

How do I deal with large, hex numbers in Sage?

asked 2015-03-07 17:47:47 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

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)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2015-03-07 23:13:16 +0100

tmonteil gravatar image

updated 2015-03-07 23:15:09 +0100

You can deal with .csv files by importing the csv module.

Hexadecimals are represented by numbers starting with 0x, see this page :

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.

edit flag offensive delete link more

Comments

Here'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])])
Geremia gravatar imageGeremia ( 2015-03-09 21:12:18 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-03-07 17:47:47 +0100

Seen: 6,538 times

Last updated: Mar 07 '15