I have a float, say 14.55e9 and I want to stringify it to "14550000000" the most nimic, clean and performant way as possible.
For now I only could find something based on slicing:
$myFloat[0..$myFloat.find('.')]
The formatFloat method from strutils outputs "14550000000.0" if you ask for precision=0
you can use
import json, math
var intvalue = int(math.floor( 14.55));
echo %*(intvalue);
EDIT:
Sorry i posted some javascript code. Math is lowercase in Nim
and JSON.stringify was from javascript
Related
I have a python string that looks like this:
byte_string = "01100110"
I want to turn this into a byte like so:
byte_byte = some_function(byte_string) # byte_byte = 0b01100110
How can I achieve this?
It appears you want to turn that string into a single number. For that you just convert to int with a base of 2.
byte_byte = int(byte_string, 2)
After reflecting on the question some more, I think I originally mis-understood your intent, which is reflected in my below "Prior Answer".
I'm thinking this prior SO question may address your question as well, as it sounds like you want to convert a string of 8 bits into a single byte.
And, as #Mark Ransom has indicated, it would look like this as a decimal code:
int(byte_string, 2)
output: 102
Or like this as the corresponding ascii character:
chr(int(byte_string, 2))
output: 'f'
----Prior Answer Based on Assumption of Encoding a String into an array of bytes------
You can encode a string to bytes like this, optionally passing the specific encoding if you want:
byte_string = "01100110"
byte_string.encode("utf-8")
bytes(byte_string, 'utf-8')
Result:
b'01100110'
Link to python docs for encoding / decoding strings.
Another option would be to create a bytearray:
bytearray(byte_string, 'utf-8')
output: bytearray(b'01100110')
And here is another StackOverflow thread that might be helpful as well, describing the difference between bytes and bytearray objects in relation to their use in converting to and from strings.
what is the simplest way to print the result as follows using pyhton3
I have a Hex string s="FFFC"
In python if using this command line: print(int(s,16))
The result I'm expecting is -4 (which is in signed format). But this is not the case, It displays the Unsigned format which the result is 65,532.
How can I convert this the easiest way?
Thank you in advance.
There are several ways, but you could just do the math explicitly (assuming s has no more than 4 characters, otherwise use s[-4:]):
i = int(s, 16)
if i >= 0x8000:
i -= 0x10000
You can use the bytes.fromhex and int.from_bytes class methods.
s = bytes.fromhex('FFFC')
i = int.from_bytes(s, 'big', signed=True)
print(i)
Pretty self-explanatory, the only thing that might need clarification is the 'big' argument, but that just means that the byte array s has the most significant byte first.
I'm new to Groovy. When I want convert some integer number to hex string, I have tried codes like this:
theNumber.toString(16)
as what I did in JavaScript. (Groovy is just like yet another script language looks similar to Java, right?)
But the code above not work as my expected. When the number is very large, the result is correct; but most of the time, it just return 16.
println(256.toString(16)) // 16
println(36893488147419103232.toString(16)) // 20000000000000000
I'm confused why Groovy behavior such strange. Could anyone help me to explain this? And, what is the best way to convert integer number to hex string?
Thanks.
Java is not JavaScript.
Groovy is a language built for the Java platform.
Java code also works directly with Groovy. So you can use .toHexString()
Integer.toHexString(256)
Long.toHexString(28562)
For numbers larger than the maximum value of long (9223372036854775807) the BigInteger datatype can be used.
String bigInt = new BigInteger("36893488147419103232").toString(16);
What you are calling is the static toString(int) from e.g. Integer. docs:
public static String toString(int i)
Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.
E.g.:
groovy:000> Integer.toString(16)
===> 16
So what you want is:
groovy:000> Integer.toString(256,16)
===> 100
That may sound like a weird struggle and actually easy to do, but I cannot find a working way to convert an hexidecimal in a string format into a float.
My exemple is for instance: 406ea716
If I convert it using one of the following website, I get 3.728948.
http://www.h-schmidt.net/FloatConverter/IEEE754.html
http://gregstoll.dyndns.org/~gregstoll/floattohex/
I tried every single piece of code I found on the internet, but it won't return the same result.
Does it exist a module in NodeJS to perform the same conversion? If not, what can I do?
Thank you for your help.
I had the same issue. try this.
Buffer('406ea716','hex').readFloatBE(0)
3.7289481163024902
No need for a module:
var hex = '406ea716';
// transform the hexadecimal representation in a proper js hexadecimal representation by prepending `0x` to the string
// parseInt() - because your example was an integer.
var num = parseInt( '0x' + '406ea716');
console.log( num );
Have you tried parseInt?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
$ node
> parseInt('406ea716', 16)
1080993558
var trnlist = from tr in db.DebtorTransactions
join wr in db.Warranties
on tr.ProductID equals Convert.ToInt32(wr.PkfWarranty) into wrtd
from wr1 in db.Warranties
join sr in db.SalesReps
on wr1.fldSrId equals sr.pkfSrID into wrsr
from wr2 in db.Warranties
join ag in db.Agentsenter code here
on wr2.fldAgentID equals ag.pkfAgentID into wrag
select wrtd;
tr.ProductID is an int and wr.PKfWarranty is string.var rustul= convert.toint32(tr.ProductID) doesn't suitable for me.
Is there any built-in function of Linq to entity to do this?
Here you say:
tr.ProductID is a int
And then you try:
convert.toint32(tr.ProductID)
So... you're trying to convert an int to an int? In a comment you say:
the best overload method match for int.parse(string) has some invalid arguments
Well, if you're trying to call int.Parse() and passing it an int then you'd probably get that exact error. I imagine there's no overload for int.Parse() which accepts an int since, well, the value is already an int.
Let's look back at your problem description:
tr.ProductID is a int and wr.PKfWarranty is String
And you want to compare these two values? Then you'll either need to convert tr.ProductID to a string:
tr.ProductID.ToString()
or convert wr.PKfWarranty to an int:
int.Parse(wr.PKfWarranty)
A few things to note:
Converting from an int to a string is pretty safe, I doubt you'll ever have problems with that. However, converting from a string to an int assumes that the string can be converted to an int. This won't be the case if the string has anything in it that's not an int, or has a number too large to fit into the int data type. int.TryParse() exists for this purpose, but can be tricky to use in an in-line LINQ statement, especially when that statement is an expression tree which needs to produce SQL code.
If you convert the int to a string, there are different ways to compare strings. Depending on whether this is happening in resulting SQL code or in C# code makes a difference. If the latter, string.Equals() is the preferred method.