Questions tagged [casting]

Casting is a process where an object type is explicitly converted into another type if the conversion is allowed. This process might lead to a change in value.

Some algorithms have significant performance differences for different data-types, for example, calculating the N-dimensional median is much is much faster for integers than float type data. Therefore, the use of casting can be important for code performance.

18049 questions
8
votes
7 answers

Typecasting generic parameters

Using the following code: Function GetSetting(Of T)(ByVal SettingName As String, ByRef DefaultVal As T) As T Return If(Configuration.ContainsKey(SettingName), CType(Configuration(SettingName), T), DefaultVal) End Function Yields the following…
Clément
  • 10,212
  • 13
  • 62
  • 104
8
votes
1 answer

numpy array casting ruled not 'safe'

Indexing one numpy array with another - both are defined as dtype='uint32'. Using numpy.take to index and get an unsafe casting error. Not come across this before. Any idea what is going on? Python 2.7.8 |Anaconda 2.1.0 (32-bit)| (default, Jul 2…
Henry Thornton
  • 3,787
  • 8
  • 27
  • 35
8
votes
1 answer

floor() and ceil() functions vs casting to integer in C

I am writing some C code to repeatedly round floats up and down to integer values. The standard C math library includes the functions floor() and ceil(). I noticed that a much quicker implementation of the functions is to cast directly to…
samuelschaefer
  • 574
  • 1
  • 8
  • 26
8
votes
2 answers

Is conversion and promotion the same thing?

I am not sure if promotion just means converting a data type to a larger data type (for example short to int). Or does promotion means converting a data type to another "compatible" data type, for example converting a short to an int, which will…
user4420637
8
votes
1 answer

OutOfMemoryError: Java heap space when casting a numeric primitive to char

I have been studying Decorator pattern and developed simple class ToUpperCaseInputStream. I overrode read() method so it could convert all chars from InputStream to uppercase. Code of the method is shown below (throws…
Zarial
  • 273
  • 2
  • 9
8
votes
1 answer

How can I convert to size_t from int safely?

Assigning an int to a size_t (or using it in malloc) in GCC produces the following warning: warning: conversion to 'size_t' from 'int' may change the sign of the result [-Wsign-conversion] To solve this, I would like to wrap the conversion in a…
Martin
  • 5,667
  • 7
  • 44
  • 73
8
votes
1 answer

Converting between 2 different libraries using the same COM interface in C#

I have a pair of libraries that both use the same COM interface. In one library I have a class that implements that interface. The other library requires an object that implements the interface. However both libraries have their own definition of…
Goz
  • 58,120
  • 22
  • 114
  • 192
8
votes
1 answer

Casting and dynamic vs static type in Java

I'm learning about static vs dynamic types, and I am to the point of understanding it for the most part, but this case still eludes me. If class B extends A, and I have: A x = new B(); Is the following allowed?: B y = x; Or is explicit casting…
XpdX
  • 83
  • 1
  • 1
  • 3
8
votes
3 answers

How Can I Check an Object to See its Type and Return A Casted Object

I have method to which I pass an object. In this method I check it's type and depending on the type I do something with it and return a Long. I have tried every which way I can think of to do this and I always get several compiler errors telling…
Russ Bradberry
  • 10,197
  • 17
  • 66
  • 83
8
votes
7 answers

Casting between classes that share the same interface

I have two interfaces IHeaderRow, and IDetailRow I then have an object that implements both RawRow:IHeaderRow, IDetailRow I then need to cast it to HeaderRow which implements IHeaderRow. But when I try, it ends up being null or giving an…
CaffGeek
  • 20,889
  • 16
  • 95
  • 176
8
votes
4 answers

Go conversion between struct and byte array

I am writing a client - server application in Go. I want to perform C-like type casting in Go. E.g. in Go type packet struct { opcode uint16 data [1024]byte } var pkt1 packet ... n, raddr, err := conn.ReadFromUDP(pkt1) // error here Also…
Shriganesh Shintre
  • 2,014
  • 3
  • 14
  • 16
8
votes
1 answer

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

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 (x: S) -> Option { if…
nodakai
  • 6,891
  • 3
  • 24
  • 51