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.

19752 questions
9
votes
6 answers

Cast int to char array in C

Is is possible to convert int to "string" in C just using casting? Without any functions like atoi() or sprintf()? What I want would be like this: int main(int argc, char *argv[]) { int i = 500; char c[4]; c = (char)i; i = 0; i…
Bodosko
  • 139
  • 1
  • 1
  • 5
9
votes
3 answers

SQL Select All and change the format of one column

I was wondering. If I have a table with 20(or more) column names in and I want to select All of them, but one column of those 20 columns is a date column and you would like to change the format of that column, how would you do that? This obviously…
Ruan
  • 3,969
  • 10
  • 60
  • 87
9
votes
4 answers

Does this pointer casting break strict aliasing rule?

This is the fast inverse square root implementation from Quake III Arena: float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * (…
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
9
votes
6 answers

C++ casting static two-dimensional double array to double**

I have such matrix in my program: double m[3][4] = { {2, 4, 5, 7}, {4, 5, 1, 12}, {9, 12, 13, -4} }; And I'd like to cast it to double** type. I've already tried simple double** a = (double**)m;, but it doesn't work…
Greg Witczak
  • 1,634
  • 4
  • 27
  • 56
9
votes
2 answers

void* is literally float, how to cast?

So I'm using this C library in my C++ app, and one of the functions returns a void*. Now I'm not the sharpest with pure C, but have heard that a void* can be cast to pretty much any other *-type. I also know that I expect a float at the end…
xNidhogg
  • 331
  • 1
  • 4
  • 12
9
votes
3 answers

Where does the C++ standard describe the casting of pointers to primitives?

In the excellent blog post What Every Programmer Should Know About Undefined Behavior, the section "Violating Type Rules" says: It is undefined behavior to cast an int* to a float* and dereference it (accessing the "int" as if it were a "float"). C…
Martin C. Martin
  • 3,565
  • 3
  • 29
  • 36
9
votes
3 answers

How to dynamically cast an object of type string to an object of type T

I have this XML document False in my code and I'm trying to build an array of arguments containing the node. object test = (object)…
LolaRun
  • 5,526
  • 6
  • 33
  • 45
9
votes
5 answers

const_cast vs reinterpret_cast

Referring the SO C++ FAQ When should static_cast, dynamic_cast and reinterpret_cast be used?. const_cast is used to remove or add const to a variable and its the only reliable, defined and legal way to remove the constness. reinterpret_cast is used…
Abhijit
  • 62,056
  • 18
  • 131
  • 204
9
votes
3 answers

Most efficient way to convert java.lang.Long to primitive int

I have a weird scenario where I need to convert several million java.lang.Longs into primitive int types. I need to do this several times a day, every single day. Normally, I wouldn't worry about this kind of simple casting, but since it's happening…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
9
votes
2 answers

Is reinterpret_cast bad when dealing with low-level byte manipulation?

I'm writing a websocket server and I have to deal with masked data that I need to unmask. The mask is unsigned char[4], and the data is a unsigned char* buffer as well. I don't want to XOR byte by byte, I'd much rather XOR 4-bytes at a…
matthewaveryusa
  • 632
  • 5
  • 24
9
votes
3 answers

primitive-boolean To String concatenation/conversion

how does this work? I can't seem to find an answer. boolean bool=true; System.out.println("the value of bool is : " + true); //or System.out.println("the value of bool is : " + bool); What are the things that are going on behind the scene? how…
Vinay W
  • 9,912
  • 8
  • 41
  • 47
9
votes
3 answers

Casting stream to filestream

Possible Duplicate: Convert a Stream to a FileStream in C# My question is regarding the cast of stream to FileStream ... Basically I need to do that to get the name of the file, because if I have just an object Stream it doesn't have the Name…
Alnedru
  • 2,573
  • 9
  • 50
  • 88
9
votes
3 answers

How do I cast an int value to a generic type parameter that is equivalent such as char?

I'm new to generics and I'm having some trouble implementing a small self practice code. I'm creating a linked list. I want it to store char or int values. So I decided to make the implementation generic: public class Node where T : struct,…
9
votes
6 answers

Why is this cast from interface to class failing?

private Vector2 ResolveCollision(ICollidable moving, ICollidable stationary) { if (moving.Bounds.Intersects(stationary.Bounds)) { if (moving is Player) { (Player)moving.Color = Color.Red; } } //…
ssb
  • 1,352
  • 3
  • 18
  • 40
9
votes
6 answers

How to cast Generic Lists dynamically in C#?

I'm trying to cast List to List dynamically. I've tried several ways, but I can't find a solution. This is a small sample that shows the problem: List listObject = new List(); listObject.Add("ITEM…
user161037
1 2 3
99
100