casting

Casting an array of unsigned chars to an array of floats

随声附和 提交于 2019-12-22 05:17:11
问题 What is the best way of converting a unsigned char array to a float array in c++? I presently have a for loop as follows for (i=0 ;i< len; i++) float_buff[i]= (float) char_buff[i]; I also need to reverse the procedure, i.e convert from unsigned char to float (float to 8bit conversion) for (i=0 ;i< len; i++) char_buff[i]= (unsigned char) float_buff[i]; Any advice would be appreciated Thanks 回答1: I think the best way is to use a function object: template <typename T> // T models Any struct

Should I escape an expected integer value using mysql_real_escape_string or can I just use (int)$expectedinteger

余生长醉 提交于 2019-12-22 05:14:24
问题 is it safe to use cast (int) instead of escaping? class opinion { function loadbyopinionid($opinionid){ $opinionid=(int)$opinionid; mysql_query("select * from fe_opinion where opinionid=$opinionid"); //more code } } 回答1: mysql_real_scape_string is for STRINGS . it will not make an integer 'safe' for use. e.g. $safe = mysql_real_escape_string($_GET['page']); will do NOTHING where $_GET['page'] = "0 = 0"; because there's no SQL metacharacters in there. your query would end up something like

How to avoid downcast?

允我心安 提交于 2019-12-22 05:07:14
问题 I have an implementation of a State Pattern where each state handles events it gets from a event queue. Base State class therefore has a pure virtual method void handleEvent(const Event*) . Events inherit base Event class but each event contains its data that can be of a different type (e.g. int, string...or whatever). handleEvent has to determine the runtime type of the received event and then perform downcast in order to extract event data. Events are dynamically created and stored in a

Why is static_cast on an expression acting distributively?

扶醉桌前 提交于 2019-12-22 05:06:49
问题 I need to take 2 unsigned 8-bit values and subtract them, then add this value to a 32-bit accumulator. The 8-bit subtraction may underflow, and that's ok (unsigned int underflow is defined behavior, so no problems there). I would expect that static_cast<uint32_t>(foo - bar) should do what I want (where foo and bar are both uint8_t ). But it would appear that this casts them first and then performs a 32-bit subtraction, whereas I need it to underflow as an 8-bit variable. I know I could just

Beginner Java Question about Integer.parseInt() and casting

谁都会走 提交于 2019-12-22 04:55:53
问题 so when casting like in the statement below :- int randomNumber=(int) (Math.random()*5) it causes the random no. generated to get converted into an int.. Also there's this method I just came across Integer.parseInt() which does the same ! i.e return an integer Why two different ways to make a value an int ? Also I made a search and it says parseInt() takes string as an argument.. So does this mean that parseInt() is ONLY to convert String into integer ? What about this casting then (int) ??

Casting a generic dictionary containing a generic dictionary

99封情书 提交于 2019-12-22 04:34:06
问题 I have: var someConcreteInstance = new Dictionary<string, Dictionary<string, bool>>(); and I wish to cast it to an interface version, i.e.: someInterfaceInstance = (IDictionary<string, IDictionary<string, bool>>)someConcreteInstance; 'someInterfaceInstance' is a public property: IDictionary<string, IDictionary<string, bool>> someInterfaceInstance { get; set; } This compiles correctly, but throws a runtime casting error. Unable to cast object of type 'System.Collections.Generic.Dictionary`2

How to address C4191 warning around calls to GetProcAddress with FARPROC?

雨燕双飞 提交于 2019-12-22 04:26:29
问题 Recently I tried to use /Wall Visual C++ option to enable all warnings and found that the following code: typedef BOOL ( WINAPI * TIsWow64ProcessFunction )( HANDLE, BOOL* ); TIsWow64ProcessFunction isWow64ProcessFunction = reinterpret_cast<TIsWow64ProcessFunction> ( ::GetProcAddress( kernel32DllHandle, "IsWow64Process" ) ); spawned C4191: warning C4191: 'reinterpret_cast' : unsafe conversion from 'FARPROC' to 'TIsWow64ProcessFunction' Calling this function through the result pointer may cause

Session attribute access and converting to int?

六眼飞鱼酱① 提交于 2019-12-22 04:11:40
问题 I have stored user id in Session using following command in Servlet: HttpSession session = request.getSession(); session.setAttribute("user", user.getId()); Now, I want to access that user id from another Servlet: HttpSession session = request.getSession(false); int userid = (int) session.getAttribute("user"); // This is not working OR User user = new User(); user.setId(session.getAttribute("user")); This ain't possible (Object != int) Question: How can I cast to int and send the id to DAO

Strange C++ boolean casting behaviour (true!=true)

ぃ、小莉子 提交于 2019-12-22 03:50:34
问题 Just read on an internal university thread: #include <iostream> using namespace std; union zt { bool b; int i; }; int main() { zt w; bool a,b; a=1; b=2; cerr<<(bool)2<<static_cast<bool>(2)<<endl; //11 cerr<<a<<b<<(a==b)<<endl; //111 w.i=2; int q=w.b; cerr<<(bool)q<<q<<w.b<<((bool)((int)w.b))<<w.i<<(w.b==a)<<endl; //122220 cerr<<((w.b==a)?'T':'F')<<endl; //F } So a , b and w.b are all declared as bool . a is assigned 1 , b is assigned 2 , and the internal representation of w.b is changed to 2

Is “If” condition better than ?? and casting

佐手、 提交于 2019-12-22 03:49:04
问题 I have following two approaches for same functionality - one with "if” condition and one with "?? and casting". Which approach is better? Why? Code: Int16? reportID2 = null; //Other code //Approach 1 if (reportID2 == null) { command.Parameters.AddWithValue("@report_type_code", DBNull.Value); } else { command.Parameters.AddWithValue("@report_type_code", reportID2); } //Approach 2 command.Parameters.AddWithValue("@report_type_code", ((object) reportID2) ?? DBNull.Value); UPDATE Based on the