casting Questions

4

Solved

I have a function that takes a pointer to a floating point array. Based on other conditions, I know that pointer is actually pointing to a 2x2 OR 3x3 matrix. (in fact the memory was initially alloc...
Hinterland asked 8/8, 2012 at 16:33

5

Solved

I am trying to convert a date to datetime but am getting errors. The datatype I'm converting from is (float,null) and I'd like to convert it to DATETIME. The first line of this code works fine, but...
Precept asked 4/10, 2010 at 13:52

9

Solved

Similar to Cast int to enum in C# but my enum is a Generic Type parameter. What is the best way to handle this? Example: private T ConvertEnum<T>(int i) where T : struct, IConvertible { re...
Aurist asked 30/4, 2012 at 16:26

4

Solved

How can I discard/round the millisecond part, better if the second part is also removed from a timestamp w/o timezone ?
Acheron asked 18/4, 2012 at 15:52

8

Solved

I have the following simple code : int speed1 = (int)(6.2f * 10); float tmp = 6.2f * 10; int speed2 = (int)tmp; speed1 and speed2 should have the same value, but in fact, I have : speed1 = 61 ...
Jetport asked 18/1, 2012 at 14:4

9

Solved

I'm trying to make a function that will accept a float variable and convert it into a byte array. I found a snippet of code that works, but would like to reuse it in a function if possible. I'm a...
Geese asked 25/6, 2014 at 23:30

3

Solved

I have a buffer that I use for UART, which is declared this way: union Eusart_Buff { uint8_t b8[16]; uint16_t b9[16]; }; struct Eusart_Msg { uint8_t msg_posn; uint8_t msg_len; union Eusart_B...
Bucentaur asked 2/3, 2019 at 23:38

6

How do I cast an int to an enum in C++? For example: enum Test { A, B }; int a = 1; How do I convert a to type Test::A?
Jesse asked 12/7, 2012 at 13:30

4

Solved

Say you have a function like this : SmartPtr<A> doSomething(SmartPtr<A> a); And classes like this : class A { } class B : public A { } And now I do this : SmartPtr<A> fo...
Suzisuzie asked 12/4, 2011 at 10:20

5

Solved

Say I have this: $username = (string) $inputs['username'] ?? null; If $inputs['username'] is set then I want it to cast to string. If it's not set then $username should be null. However, at the...
Jacobean asked 14/5, 2018 at 20:26

5

This statement fails. How could I cast from one enum into another (which are identical) enum Enum1 { Key1 = 'key' } enum Enum2 { Key1 = 'key' } const key = Enum1.Key1 const key2 = key as Enum2...
Catechist asked 8/3, 2018 at 8:14

16

Solved

How do I convert a byte array into a string? I have found these functions that do the reverse: function string2Bin(s) { var b = new Array(); var last = s.length; for (var i = 0; i < last; i...
Polluted asked 7/7, 2010 at 14:46

2

Solved

This is a followup to a question about printing a common member from different structs I thought that unions permit to examine the common initial sequence of two of their elements. So I ended with ...
Cinemascope asked 11/3, 2021 at 10:16

12

Solved

Upcasting is allowed in Java, however downcasting gives a compile error. The compile error can be removed by adding a cast but would anyway break at the runtime. In this case why Java allows do...
Nightie asked 19/12, 2008 at 12:11

1

Solved

How can I convert a f64 into the closest f32? There is neither a From nor a TryFrom implementation, presumably because such implementations are only provided for lossless conversions. I also search...
Resonator asked 15/5, 2022 at 11:14

3

Solved

I am bit confused with typecasting in Swift. Have a small doubt. What is the difference between as?,as! and only as. And can we say "as" is similar to is. Thanks in advance.
Lebar asked 3/3, 2016 at 2:28

3

Solved

Note: I mistakenly asked about static_cast originally; this is why the top answer mentions static_cast at first. I have some binary files with little endian float values. I want to read them in a ...
Visa asked 20/12, 2012 at 23:53

9

Solved

In C# the following code returns 2: double d = 2.9; int i = (int)d; Debug.WriteLine(i); In Javascript, however, the only way of converting a "double" to an "int" that I'm aware of is by using Ma...
Nichrome asked 5/12, 2011 at 16:27

10

Solved

I have a List<SubClass> that I want to treat as a List<BaseClass>. It seems like it shouldn't be a problem since casting a SubClass to a BaseClass is a snap, but my compiler complains t...
Graduate asked 22/2, 2011 at 18:8

5

Solved

In our application, we have a very large byte-array and we have to convert these bytes into different types. Currently, we use BitConverter.ToXXXX() for this purpose. Our heavy hitters are, ToInt16...
Concurrence asked 7/2, 2011 at 16:41

4

Solved

I can't seem to find this question anywhere on Google (or StackOverflow), which really surprised me, so I'm putting it on here to help others in the same situation. I have a SQL query which runs f...
Chromolithography asked 29/5, 2014 at 14:24

6

Solved

I have the following scenario where I want to pass in string and a generic type: public class Worker { public void DoSomeWork<T>(string value) where T : struct, IComparable<T>, IEqu...
Echevarria asked 9/7, 2009 at 23:33

5

I am getting error error: aggregate value used where an integer was expected on compiling this code: #include <stdio.h> typedef unsigned long U32; typedef struct hello_s { U32 a:8; U3...
Cornellcornelle asked 18/9, 2012 at 19:57

5

Solved

What are the differences between int a; // a gets some value double pi = static_cast<double>(a)/3; and int a; // a gets some value double pi = double(a)/3; Have you ever seen the latte...
Access asked 3/5, 2012 at 19:40

4

Solved

I am reading raw data from a file and I want to convert it to an integer: fn main() { let buf: &[u8] = &[0, 0, 0, 1]; let num = slice_to_i8(buf); println!("1 == {}", num); } pub fn sli...
Syndrome asked 27/3, 2015 at 18:11

© 2022 - 2024 — McMap. All rights reserved.