Out of interest, I was trying to find a way to cast an integer to a String in Fortran77.
I came across CHAR(I), but this converts the ASCII index I into the character in that postion.
Is there a way to just simply cast an integer to a String in Fortran77?
How about vice versa?
The Fortran way is to write the value of the integer into a string variable; this operation is known as an internal write. I'm heading out the door now so won't check this, and I have an ethical objection to writing FORTRAN77 or helping anyone else write it, so make no guarantee that the following doesn't contain bits of more modern Fortran.
First declare a character variable to receive the integer
character(len=12) :: int_as_string
then write the integer into it as you would normally write an integer to any other channel such as stdout
write(int_as_string,'(i12.12)') my_int
I expect you'll want to set the format for writing the integer to something that suits you better
Related
In Java architects of the language used prefixes like L/l for long numbers to make parsing easier and to differentiate Int vs Long constants. I am making a deserialisation library for TOML and received a request from the user not only to support easy-parsing to Long, but also to Int numbers depending on the string input.
So during the parsing I need to understand if the string in the input is Byte/Short/Int or Long and select a proper type automatically for an input.
This leads me to a question: is there any library in Kotlin (my library is MPP) that can help me to check if ? Like Math does it in Java. I am pretty sure that there should be some obvious library or algorithm for it, so I do not want to implement my yet another one.
If you will propose not a library, but a good algorithm for determine a type of an integer number by the string input - that will also be fine for me. But better it would be some known-algorithm.
We should not also forget about UNSIGNED int that are there in Kotlin but missing in Java
You can parse the positive part of an integer. If your integer is larger, than Int.MAX_VALUE it becomes negative. So when it becomes negative, you know there is an overflow.
Int.MAX_VALUE + 1 // produces a negative value
You can also try this library for parsing. https://github.com/tiksem/KotlinSpirit
val errorCode = int.compile().parseWithResult("3453534435543543345345").errorCode
errorCode will be ParseCode.INT_OUT_OF_BOUNDS
Is there any way to code a long string to a unique number (integer) and then decode this number to original string? (I mean to reduce size of long string)
The simple answer is no.
The complex answer is maybe.
What you are looking for is compression, compression can reduce the size of the String but there is no guarantee as to how small it can make it. In particular you can never guarantee being able to fit it into a certain sized integer.
There are concepts like "hashing" which may help you do what you want depending on exactly what you are trying to do with this number.
Alternatively if you use the same string in a lot of different places then you can store it once and pass references/pointers to that single instance of the String around.
First you need to hash it to string eg md5. Then you convert the characters of the hash string into numbers according to the alphabetical number
I want to implement a check function that given two strings s1 and s2 will check if s2 is the caesar cipher of s1 or not. the inter face needs to be looked like string->string->bool.
the problem is that I am not allowed to use any string functions other than String.length, so how can I solve it? i am not permitted any list array, iterations. Only recursions and pattern matching.
Please help me. And also can you tell me how I can write a substring function in ocaml other than the module function with the above restrictions?
My guess is that you are probably allowed to use s.[i] to get the ith character of string s. This is the same as String.get, but the instructor may not think of it in those terms. Without some form of getting the individual characters for the string, I believe that this is impossible. You should probably double check with your instructor to be sure, but I would be surprised if he had meant for you to be unable to separate a string into characters (which is something that you cannot do with pattern-matching alone in Ocaml).
Once you can get individual characters, the way to do it should be pretty clear (you do not need substring to traverse each string recursively).
If you still want to write substring, creating it would be complex since you don't have access to String.create or other similar functions. But you can write your own version of String.create using recursion, one character string literals (like "x"), the ability to set a character in a string to another (like s.[0] <- c), and string concatenation (s1 ^ s2). Again, of course, all of this is assuming that those operators are allowed to be used.
i got one little doubt with my MIPS homework, i have to, given a string, return an int equal to the string (the string is made of ints). I have no idea how to do this, can anyone give me a boost?
I suppose that what you have to do is to convert a string to an integer in the way that atoi() does. You could have a look at an implementation of that function for hints.
We have an alpha numeric string (up to 32 characters) and we want to transform it to an integer (bigint). Now we're looking for an algorithm to do that. Collision isn't bad (therefor we use an bigint to prevent this a little bit), important thing is, that the calculated integers are constantly distributed over bigint range and the calculated integer is always the same for a given string.
This page has a few. You'll need to port to 64bit, but that should be trivial. A C# port of SBDM hash is here. Another page of hash functions here
Most programming languages come with a built-in construct or a standard library call to do this. Without knowing the language, I don't think anyone can help you.
Yes, a "hash" should be the right description for my problem. I know, that there is CRC32, but it only provides an 32-bit int (in PHP) and this 32-bit integers are at least 10 characters long, so a huge range of integer number is unused!?
Mostly, we have a short string like "PX38IEK" or an 36 character UUID like "24868d36-a150-11df-8882-d8d385ffc39c", so the strings are arbitrary, yes.
It doesn't has to be reversible (so collisions aren't bad). It also doesn't matter what int a string is converted to, my only wish is, that the full bigint range is used as best as possible.