casting

Filter strings with regex before casting to numeric

倖福魔咒の 提交于 2019-12-23 23:08:22
问题 I have this code (was already there, isn't mine): SELECT a.id_original_contrato AS contrato, ( CASE WHEN d.value~'^\\d+$' THEN d.value::integer ELSE 0 END ) AS monto, EXTRACT(YEAR FROM b.value)::integer AS anoinicio, EXTRACT(YEAR FROM c.value)::integer AS anofin ... etc (some JOIN's and WHERE's) Let me explain: d.value comes from a table where value is character varying (200) . The code will insert later the d.value (now called 'monto') in another table as a integer . Someone coded that regex

Accessing an array as a struct vs undefined behavior

故事扮演 提交于 2019-12-23 22:54:02
问题 Let's say we have this structure with 4 float values and a float array with 4 elements. Is it then undefined behavior or not to access the array as a Foo instance and change the array elements through that instance? struct Foo { float a; float b; float c; float d; }; float values[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; int main() { auto& floats = *reinterpret_cast<Foo*>(values); floats.a = 0.0f; floats.b = 0.0f; floats.c = 0.0f; floats.d = 0.0f; } Compile and run online: http://cpp.sh/6y7m 回答1: Yes,

Jooq casting String to BigDecimal

人走茶凉 提交于 2019-12-23 22:29:17
问题 Is there a way to cast a String to a BigDecimal in a jooq-query without losing precision? When i do endResER.VALUE.cast(BigDecimal.class) where VALUE is a field with a String-value in the database it returns a BigDecimal without any fraction digits. I need to compare two amounts that are saved as Strings in the DB. 回答1: You can cast your value to a SQLDataType like this: endResER.VALUE.cast(SQLDataType.DECIMAL.precision(10, 5)) Beware though, that there is a known issue for jOOQ 3.1: #2708.

Integer to byte conversion

情到浓时终转凉″ 提交于 2019-12-23 21:45:50
问题 Say I've got an integer, 13941412, that I wish to separate into bytes (the number is actually a color in the form 0x00bbggrr). How would you do that? In c, you'd cast the number to a BYTE and then shift the bits. How do you cast to byte in Python? 回答1: Use bitwise mathematical operators, the "bytes" are already there: def int_to_rgb(n): b = (n & 0xff0000) >> 16 g = (n & 0x00ff00) >> 8 r = (n & 0x0000ff) return (r, g, b) 回答2: You can bitwise & with 0xff to get the first byte, then shift 8 bits

What does the compiler at casting integer constants?

ⅰ亾dé卋堺 提交于 2019-12-23 21:06:53
问题 Using the following macro: #define MIN_SWORD (signed int) 0x8000 In e.g. the following expression: signed long s32; if (s32 < (signed long)MIN_SWORD)... is expected to do the following check: if (s32 < -32768) One some compilers it seems to work fine. But on some other compiler the exprssion is evaluated as: if (s32 < 32768) My question: How is a ANSI-C compiler supposed to evaluate the following expression: (signed long) (signed int) 0x8000 ? It seems that on some compilers the cast to `

Connecting a STD_LOGIC to a one bit STD_LOGIC_VECTOR

夙愿已清 提交于 2019-12-23 20:44:28
问题 I'm using Xilinx ISE and generated a memory using the CORE Generator & Architecture Wizard. The problem is that it created a write enable signal ( wea ) as a STD_LOGIC_VECTOR(0 downto 0) and that results in a type mismatch: Line ###: Type error near encnt ; current type std_logic; expected type std_logic_vector How can I cast encnt , which is std_logic, to a one bit std_logic_vector? (ISE doesn't allow me to change wea from the file of memory.) 回答1: This is a pretty common scenario with these

pointer arithmetic (char*) &a[1] - (char *)&a[0] == 4

删除回忆录丶 提交于 2019-12-23 20:44:09
问题 If a is an int array, (char*) &a[1] - (char *)&a[0] is equal to 4, while &a[1] - &a[0] is equal to 1. why is that? 回答1: Pointer math operates on the size of the data structure its pointing to. This is because if I do this: int array[10] ; int * p = array ; p ++ ; I want p pointing at the second int, not some memory halfway in between two elements. So &a[1] is four bytes apart from &a[0] but asking it &a[1] - &a[0] asks how many ints apart it is. When you cast it to char you ask for the math

How to fill an array with a value using void generic pointers?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 20:40:37
问题 Given the following method void fillArray(void *arr, int const numElements, void *val, int size) How can you fill an array ( *arr ) with a value ( *val ) without knowing what type the array ? numElements is the number of elements that are in the array and size is the byte size of whatever type the array is. 回答1: You can use memcpy for that. However, in order to advance the memory location, you have to cast input pointer to a char* first. If you have void* , the pointer arithmetic operations

Why can't I pull a ushort from a System.Object and then cast it as a uint? (C#)

南笙酒味 提交于 2019-12-23 20:32:09
问题 I'm manipulating the items in a list that's a System.Management.ManagementObjectCollection . Each of these items is a System.Management.ManagementObject which contains properties indexed by string. See: foreach (ManagementObject queryObj in searcher.Get()) { string osversion = (string)queryObj["Version"]; string os = (string)queryObj["Name"]; uint spmajor = (uint)queryObj["ServicePackMajorVersion"]; uint spminor = (uint)queryObj["ServicePackMinorVersion"]; ... ... ... } Each "dictionary

cannot convert 'a' (type 'int') to type 'double&

China☆狼群 提交于 2019-12-23 20:26:16
问题 I'm following some C++ tutorials, and frustratingly, the source material states this particular function call cannot be made, but doesn't at all explain why; template<typename T> void Swap(T &a, T &b){ T temp; temp = a; a = b; b = temp; } So now I create an explicit template function instantiation and pass in a and b ; int a = 5; double b = 10.3; Swap<double>(a, b); Which then throws the following compiler error; cannot convert 'a' (type 'int') to type 'double&' My only hunch is that this is