How do you write BigInt to file in Julia? - julia

I'm trying to write very big numbers to a file but I get the error that write() doesn't accept BigInt as an argument.
open("twosPow.txt","w") do f
for i in 1:10
write(f,BigInt(2)^10^i);
end
end
ERROR: MethodError: no method matching write(::IOStream, ::BigInt)
I also can't find a way to convert BigInt to string.

You don't have to convert to a string if you print the number to the file directly instead of write-ing it.
open("twosPow.txt","w") do f
for i in 1:9
print(f,big(2)^10^i)
end
end
Also note that the exponent 10_000_000_000 is too large for a BigInt, 2^10^10 will overflow.

write(f,string(BigInt(2)^10^i)) will do what you want.

Related

Get the correct Hexadecimal for strange symbol

I have this strange symbol on my pl/sql developer client (check image it's the symbol between P and B )
In the past, and for a different symbol, i was able to update my DB and remove them making this:
update table set ent_name = replace(ent_name, UTL_RAW.CAST_TO_VARCHAR2(HEXTORAW('C29B')), ' ');
The problem is that i dont remember how I translated the symbol (i had at that time) to the C29B.
Can you help me to understand how can i translate the currenct symbol to the HEX format, to i can use the command to remove it from my database?
Thanks
As long as it's in your table, you can use the DUMP function to find it.
Use DUMP to get the byte representation of the data in code of you wish to inspect for weirdness.
A good overview: Oracle / PLSQL: DUMP Function
Here's some text with plain ASCII:
select dump('Dashes-and "smart quotes"') from dual;
Typ=96 Len=25:
68,97,115,104,101,115,45,97,110,100,32,34,115,109,97,114,116,32,113,117,111,116,101,115,34
Now introduce funny characters:
select dump('Dashes—and “smart quotes”') from dual;
Typ=96 Len=31:
68,97,115,104,101,115,226,128,148,97,110,100,32,226,128,156,115,109,97,114,116,32,113,117,111,116,101,115,226,128,157
In this case, the number of bytes increased because my DB is using UTF8. Numbers outside of the valid range for ASCII stand out and can be inspected further.
The ASCIISTR function provides an even more convenient way to see the special characters:
select asciistr('Dashes—and “smart quotes”') from dual;
Dashes\2014and \201Csmart quotes\201D
This one converts non-ASCII characters into backslashed Unicode hex.
The DUMP function takes an additional argument that can be used to format the output in a nice way:
select DUMP('Thumbs 👍', 1017) from dual;
Typ=96 Len=11 CharacterSet=AL32UTF8: T,h,u,m,b,s, ,f0,9f,91,8d
select DUMP('Smiley 😊 Face', 17) from dual;
Typ=96 Len=16: S,m,i,l,e,y, ,f0,9f,98,8a, ,F,a,c,e

Fill column in Julia

I am trying to fill a column in Julia with values from another matrix. In R, it would look like this:
for(id in 1:y){
countries[id,1] <- x[id, countries1[id]]
}
However, when I try to convert the left side of the equal sign to Julia like so:
countries[:1]
I get an error which says:
"ERROR: MethodError: Cannot `convert` an object of type Int64 to an
object of type Array{Int64,2}
This may have arisen from a call to the constructor Array{Int64,2} .
(...), since type constructors fall back to convert methods."
I don't think my Julia conversion in correct to start with since I am leaving off id. How can I convert the r code to Julia effectively?
It is not clear how the OP intends to convert the stated R code to Julia code. However, given that it involves countries[:1], we can make an educated guess about the error:
countries[:1] should be countries[:, 1].
countries[:, 1] returns the first column in the matrix countries.
countries[:1] resolves to countries[1] and returns the integer countries[1,1]. This is because a leading colon :<name> tells Julia to treat <name> as a symbol. At some point that symbol is parsed, returning 1.
The latter point explains the error message:
ERROR: MethodError: Cannot convert an object of type Int64 to an
object of type Array{Int64,2}
The OP expected countries[:1] to return an array (Array{Int64,2}) , when in fact, it returns an integer (Int64).

How to convert any type into String in Julia

Using Julia, I'd like to reliably convert any type into type String. There seems to be two ways to do the conversion in v0.5, either the string function or String constructor. The problem is that you need to choose the right one depending upon the input type.
For example, typeof(string(1)) evaluates to String, but String(1) throws an error. On the other hand, typeof(string(SubString{String}("a"))) evaluates to Substring{String}, which is not a subtype of String. We instead need to do String(SubString{String}("a")).
So it seems the only reliable way to convert any input x to type String is via the construct:
String(string(x))
which feels a bit cumbersome.
Am I missing something here?
You should rarely need to explicitly convert to String. Note that even if your type definitions have String fields, or if your arrays have concrete element type String, you can still rely on implicit conversion.
For instance, here are examples of implicit conversion:
type TestType
field::String
end
obj = TestType(split("x y")[1]) # construct TestType with a SubString
obj.field # the String "x"
obj.field = SubString("Hello", 1, 3) # assign a SubString
obj.field # the String "Hel"

Pointer to C++ function in Cycript

In cycript, it is possible to get a reference to a c function pointer, but I've been unable to use that syntax to retrieve a pointer to c++ functions using either their proper or mangled function names from the symbol table.
Is there a way to get there from here?
Update:
Update from Saurik's Input:
I hadn't tried function pointers from the c style symbols, but you are absolutely right that the leading underscore needs to be stripped. _DES_encrypt3 needs to be accessed with:
cy# dlsym(RTLD_DEFAULT, "DES_encrypt3")
0x14dc19
This gives me a valid pointer address.
When I look at the mangled symbol for xmpp::CapsManager::~CapsManager(), which is __ZN4xmpp11CapsManagerD2Ev_1bf718, I try
cy# dlsym(RTLD_DEFAULT, "__ZN4xmpp11CapsManagerD2Ev_1bf718")
null
cy# dlsym(RTLD_DEFAULT, "_ZN4xmpp11CapsManagerD2Ev_1bf718")
null
cy# dlsym(RTLD_DEFAULT, "ZN4xmpp11CapsManagerD2Ev_1bf718")
null
None of these variations yield a pointer.
My immediate guess is that you are trying to take the raw mangled symbol name (as you describe, getting it from the symbol table), passing it to dlsym... but dlsym requires a C-level symbol name, which means your approach would not work even for a simple C symbol: you will have an extra _ at the beginning (if you check the symbol table, you will see that C functions are also mangled, to begin with _). If you strip the leading _ you should be able to use dlsym to look up your mangled C++ symbol.

Convert hex to text using SQLite

I am trying to convert from a string representing hex data to the textual encoding (ASCII, UTF-8, etc.) of the hex data using purely SQLite language. Essentially I want the functionality of the X'[hex]' syntax, but applied to a programmatically derived hex string.
I want, for example, select X(hex_data_string) from ..., which is not legal SQLite syntax.
Obviously in the above snippet, I would not necessarily be able to output the data if it was not in a valid textual encoding. That is, if hex_data_string contains control chars, etc., the X() should fail in some way. If this is possible, there would have to be a default character encoding or the desired character encoding would have to be specified somehow.
I am not asking about how to retrieve the hex data string value from the SQLite database and then use C or some other facility to convert it. I am trying to perform this conversion in pure SQLite because I have queries that I check which return a text representation of hex characters representing binary data. Most of the binary data is ASCII, so I want to be able to quickly view the content of the binary data in my query output when applicable.
Intuitively, I figured this could be accomplished by casting the hex data string to a blob and using hex() but that still returns the hex data string.
Any ideas?
Possible duplicates:
SQLite X'...' notation with column data
sqlite char, ascii function
This is quite old but I was looking for the same thing so I thought I'd post:
It seems you can just cast hex data as a varchar to convert it to ascii.
ie:
select cast(data as varchar) from some_table will return a string representation of a binary field (data).
This is not possible in pure SQLite.
As an embedded database, SQLite is designed to provide only pure database functions, and leave the program logic to the the application.
You are supposed to retrieve the hex data string value from the SQLite database and then use C or some other facility to convert it.
If you control the program you're running the queries in, you could install a user-defined function that does this conversion.
This will not be fully useful since it can't really be used inline, but I have done this in the past when I just wanted to convert one row of data and not create/find another utility.
WITH RECURSIVE test(c,cur) as (
select '','686F77647921'
UNION ALL
select c || char((case substr(cur,1,1) when 'A' then 10 when 'B' then 11 when 'C' then 12 when 'D' then 13 when 'E' then 14 when 'F' then 15 else substr(cur,1,1) end)*16
+ (case substr(cur,2,1) when 'A' then 10 when 'B' then 11 when 'C' then 12 when 'D' then 13 when 'E' then 14 when 'F' then 15 else substr(cur,2,1) end)),
substr(cur,3)
from test where length(cur)>0
)
select * from test

Resources