Is there a way to convert Int64 to binary and back to Int64?
var bytes = BitConverter.GetBytes(long);
var value = BitConverter.ToInt64(bytes, 0);
if string formatting is what you need:
Convert.ToString(someInt64, 2);
Convert.ToUInt64("101010", 2);
Similar to HuBeZa's answer, this converts type long to string:
Convert.ToString(Convert.ToInt64(myLongInt), 2);
Related
I would like to convert the string to float in pandas
df['BusinessTravel'] = df['BusinessTravel'].astype(float)
but it gives me this error:
could not convert string to float: 'Travel_Rarely'
Does anybody know how to fix it?
You can convert something like this pi_string = "3.1415926" to float with float(pi_string) but to convert a string such as BusinessTravel is not possible.
What you can do is map your strings in dict object.
map_dict = {"Travel_Rarely" : 1.0,
"Travel_Frequently" : 2.0,
"Travel_Occasionaly" : 3.0 }
and then use it to map all existing values of BusinessTravel column.
(Hint: Use apply on the column)
How to convert a string value which contains float representation to integer in kotlin?
I tried to convert string to float with .toFloat() and then converted it to an integer using toInt() and it works flawlessly.
But how to convert such string to integer directly?
val strDemo = "42.22"
val intDemo = strDemo.toInt()
snippet above throws NumberFormatException because it is not correct number representaion of Integer.
But, when I try
val strDemo = "42.22"
val intDemo = strDemo.toFloat().toInt()
it converts the data with no exception because string gets converted to float first. And there is a correct number representation for a Float value.
Now how to bypass the toFloat() method and convert strDemo to Integer directly?
There's no magic function that will convert a decimal/float string numbers to integer directly. It has to be done this way. Even if you found one, I'm sure that the process toFloat().toInt() still happen on that function.
So the solution that you can do is to create an extension of String like this:
StringExt.kt
fun String.floatToInt(): Int {
return this.toFloat().toInt()
}
You can use it like this:
val strDemo = "42.22"
val intDemo = strDemo.floatToInt()
I have this value:
263e5df7a93ec5f5ea6ac215ed957c30
When I fill this in on: https://8gwifi.org/base64Hex.jsp (Hex to Base64)
It gives me back:
Jj5d96k+xfXqasIV7ZV8MA==
This is the expected value. However, when I try this in Kotlin,
val encodedHexB64 = Base64.encodeToString("263e5df7a93ec5f5ea6ac215ed957c30".toByteArray(UTF_8), Base64.NO_WRAP)
It gives me back:
MjYzZTVkZjdhOTNlYzVmNWVhNmFjMjE1ZWQ5NTdjMzA=
How to get the correct value in Kotlin?
To complete the previous:
val input = "263e5df7a93ec5f5ea6ac215ed957c30"
val bytes = input.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
val encodeBase64 = Base64.encodeToString(bytes, Base64.DEFAULT)
Now you have: Jj5d96k+xfXqasIV7ZV8MA==
It looks like the input string represents 16 bytes, where each byte is coded with two hex digit chars of that string.
On the contrary toByteArray(UTF_8) encodes the string in UTF-8 encoding turning each char into one or more bytes. When you convert these bytes to base64, first you get the longer result and second — these are completely different bytes.
I suppose the correct way to convert the input hex string into byte array would be:
val input = "263e5df7a93ec5f5ea6ac215ed957c30"
val bytes = input.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
Then you encode these bytes to base64 as usual.
From what I have understood, A Python 2 string (type str) is nothing but a sequence of bytes. How do I convert such a string to a Java byte array, explicit?
A naive attempt that doesn't work:
from jarray import array
myStr = 'some str object'
myJavaArr = array(myStr, 'b') # TypeError: Type not compatible with array type
My reason to this is that when Jython implicitly converts a Python String to Java code, it converts it to a Java String, incorrectly because it doesn't know the encoding of the str.
I found an utility method that does the trick:
from org.python.core.util import StringUtil
myJavaArr = StringUtil.toBytes(myStr)
Your original approach of passing a str object to jarray.array() worked in Jython 2.5 and earlier.
Now you must convert the str to a sequence of numbers first (eg list, tuple, or bytearray):
myStr = 'some str object'
myJavaArr = array( bytearray(myStr), 'b')
or
myStr = 'some str object'
myList = [ord(ch) for ch in myStr]
myJavaArr = array( myList, 'b')
(see also https://sourceforge.net/p/jython/mailman/message/35003179/)
Note, too, that Java's byte type is signed. The conversion will fail if any of your integers have a value outside the range -128..127. If you are starting with unsigned byte values in Python, you'll need to convert them. (eg apply this to each value: if 0x80 <= i <= 0xFF: i = i - 256)
In java, I have
String snumber = null;
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number); //'number' is of type 'long' passed to a function
//which has this code in it
I am not aware of the DecimalFormat operations in java and so finding it hard to write an equivalent Obj C code.
How can I achieve this? Any help would be appreciated.
For that particular case you can use some C-style magic inside Objective-C:
long number = 123;
int desiredLength = 10;
NSString *format = [NSString stringWithFormat:#"%%0%dd", desiredLength];
NSString *snumber = [NSString stringWithFormat:format, number];
Result is 0000000123.
Format here will be %010d.
10d means that you'll have 10 spots for number aligned to right.0 at the beginning causes that all "empty" spots will be filled with 0.
If number is shorter than desiredLength, it is formatted just as it is (without leading zeros).
Of course, above code is valid only when you want to have numbers with specified length with gaps filled by zeros.
For other scenarios you could e.g. write own custom class which would use appropriate printf/NSLog formats to produce strings formatted as you wish.
In Objective-C, instead of using DecimalFormat "masks", you have to live with string formats.