casting

How to cast generic types that I know to be integers?

柔情痞子 提交于 2019-12-17 14:04:33
问题 I want to check return codes of C APIs in a generic way and the result must be free from C types such as libc::c_int . Are there any ways to write a function like fn check<S: PartialOrd + std::num::Zero, T> (x: S) -> Option<T> { if std::num::zero::<S>() <= x { Some(x as T) } else { None } } when I'm sure that S and T are integral types for all usages of check() ? The compiler rejects my code complaining error: non-scalar cast: `S` as `T` 回答1: Updated for Rust 1.x It is impossible to cast

How to cast generic types that I know to be integers?

烈酒焚心 提交于 2019-12-17 14:04:12
问题 I want to check return codes of C APIs in a generic way and the result must be free from C types such as libc::c_int . Are there any ways to write a function like fn check<S: PartialOrd + std::num::Zero, T> (x: S) -> Option<T> { if std::num::zero::<S>() <= x { Some(x as T) } else { None } } when I'm sure that S and T are integral types for all usages of check() ? The compiler rejects my code complaining error: non-scalar cast: `S` as `T` 回答1: Updated for Rust 1.x It is impossible to cast

Convert a big integer to a full string in PHP

微笑、不失礼 提交于 2019-12-17 13:58:08
问题 I've been searching for a while now, but what I can find is not what I search for. I need to convert an integer value, that may be very huge , to a string. Sounds easy: "$var" ? No, because this can lead to the E+ representation of the number. <?php $var = 10000000000000000000000000; echo $var."\n"; echo "'$var'\n"; echo (string) $var."\n"; echo strval($var); ?> 1.0E+25 '1.0E+25' 1.0E+25 1.0E+25 How can I make the output be 10000000000000000000000000 instead? 回答1: This is not stored as an

Add bytes with type casting, Java

无人久伴 提交于 2019-12-17 13:55:43
问题 I am trying to add two values in a byte array. This is my code: byte[] ars = {3,6,9,2,4}; ars[0] = (byte)ars[0] + (byte)ars[4]; System.out.println( ars[0] ); I get this error on compilation: Main.java:9: possible loss of precision found : int required: byte ars[0] = (byte)ars[0] + (byte)ars[4]; ^ 1 error Any help is, as always, much appreciated. 回答1: close, but a little off. ars[0] = (byte)(ars[0] + ars[4]); keep in mind ars[0] and ars[4] are already bytes, so there is no need to cast them to

How can I cast an int to a bit in MySQL 5.1?

感情迁移 提交于 2019-12-17 13:53:33
问题 I am transitioning from SQL Server to MySQL 5.1 and seem to be tripped up trying to create a table using a select statement so that the column is a bit. Ideally the following would work: CREATE TABLE myNewTable AS SELECT cast(myIntThatIsZeroOrOne as bit) AS myBit FROM myOldtable However sql is very unhappy at casting as a bit. How can I tell it to select an int column (which I know only has 0s and 1s) as a bit? 回答1: You cannot! CAST and CONVERT only work to: BINARY[(N)] CHAR[(N)] DATE

Why does null need an explicit type cast here? [duplicate]

戏子无情 提交于 2019-12-17 13:44:23
问题 This question already has answers here : Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate] (9 answers) Conditional operator cannot cast implicitly? (3 answers) Closed 6 years ago . The following code does not compile: //int a = ... int? b = (int?) (a != 0 ? a : null); In order to compile, it needs to be changed to int? b = (a != 0 ? a : (int?) null); Since both b = null and b = a are legal, this doesn't make sense to me. Why do we have to cast the null into

Java: Long result = -1: cannot convert from int to long

两盒软妹~` 提交于 2019-12-17 13:23:30
问题 I'm using eclipse java ee to perform java programming. I had the following line of code in one of my functions: Long result = -1; I got the following error: Type mismatch: cannot convert from int to Long I can't quite understand why when i add a number to the variable it provides this error. How can this issue be resolved and why did it happen in the first place? 回答1: There is no conversion between the object Long and int so you need to make it from long . Adding a L makes the integer -1 into

Rails Migrations: tried to change the type of column from string to integer

这一生的挚爱 提交于 2019-12-17 10:52:49
问题 I created a table in my rails app with rails generate migrations command. Here is that migration file: class CreateListings < ActiveRecord::Migration def change create_table :listings do |t| t.string :name t.string :telephone t.string :latitude t.string :longitude t.timestamps end end end Then I wanted to store the latitude and longitude as integers so I tried to run: rails generate migration changeColumnType and the contents of that file are: class ChangeColumnType < ActiveRecord::Migration

Any difference between type assertions and the newer `as` operator in TypeScript?

微笑、不失礼 提交于 2019-12-17 10:49:11
问题 Is there any difference between what the TypeScript spec calls a type assertion: var circle = <Circle> createShape("circle"); And the newer as operator: var circle = createShape("circle") as Circle; Both of which are typically used for compile-time casting? 回答1: The difference is that as Circle works in TSX files, but <Circle> conflicts with JSX syntax. as was introduced for this reason. For example, the following code in a .tsx file: var circle = <Circle> createShape("circle"); Will result

Opinions on type-punning in C++?

别来无恙 提交于 2019-12-17 10:40:16
问题 I'm curious about conventions for type-punning pointers/arrays in C++. Here's the use case I have at the moment: Compute a simple 32-bit checksum over a binary blob of data by treating it as an array of 32-bit integers (we know its total length is a multiple of 4), and then summing up all values and ignoring overflow. I would expect such an function to look like this: uint32_t compute_checksum(const char *data, size_t size) { const uint32_t *udata = /* ??? */; uint32_t checksum = 0; for (size