casting

PHP cast to integer returns wrong value

不羁的心 提交于 2019-12-13 08:39:19
问题 Have a look at this my friend and tell me I'm not crazy... echo (int) (9.45 * 100); // gives 944 echo (int) 945; // gives 945 I don't understand why the first instruction would return 944!???? Is this a known php issue? help is appreciated as always! 回答1: According to PHP documentation (http://php.net/manual/en/language.types.integer.php) When converting from float to integer, the number will be rounded towards zero. That's why you are getting 944. 回答2: Floating point numbers have limited

How to convert date to mm/dd/yyyy format

佐手、 提交于 2019-12-13 08:21:16
问题 I want to convert dateformat to mm/dd/yyyy. Whatever dateformat is coming in textbox, I want to convert it into mm/dd/yyyy. 回答1: First you need to get it into a datetime object. The most common standards work via: DateTime x = DateTime.Parse(txtDate.Text); If you expect a freaky format, you still have to know what format it is: DateTime x; DateTime.TryParseExact(txtDate.Text, "YYddd", out x); Then simply output the data: string date = x.ToString("MM/dd/yyyy"); But you really need to enforce

Casting QByteArray to `long` outputs different result for same input

≡放荡痞女 提交于 2019-12-13 07:36:56
问题 EDIT: Added full MCV example project. I have a strange problem where the same code and same input produce different output values. The purpose of the code is to test a function that takes a value packed into 4 bytes, and unpack it into a single 32bit value. The expected value of value1 , value2 and value3 in test_unpack() is 2018915346 (i.e. 0x78563412 because of little-endian unpacking). I got this method of unpacking from another answer. Below is an MCV example that you can easily build and

Need help getting height of image (Possible Inheritance Problem)

旧城冷巷雨未停 提交于 2019-12-13 06:59:28
问题 Ok so this is the KernelHandler Class public class KernelHandler extends ImageHandler I'm getting a cannot find method getWidth() I know it has something to do with inheritance, but I need help, please //Heres the contructor public KernelHandler(String nameOfFile) { super(nameOfFile); } // here's the super constructor public ImageHandler(String nameOfFile){ URL imageURL = getClass().getClassLoader().getResource(nameOfFile); try { myImage = ImageIO.read(imageURL); } catch ( IOException e ) {

How to cast from int to byte, then use a bitshift operator

烈酒焚心 提交于 2019-12-13 06:24:35
问题 Why does the following not work? I cast an int to a byte, then shift the bits over by 7. I don't see any problems there. However, I get the error message "possible loss of precision... required: byte; found: int" pixels is an array of bytes, c is a Color object, iter is an integer. pixels[iter++] = ((byte) c.getRed()) << 7; pixels[iter++] = ((byte) c.getGreen()) << 7; pixels[iter++] = ((byte) c.getBlue()) << 7; 回答1: In Java, the shift operators return an int value, even if the quantity being

How does the `--defsym` linker flag work to pass values to source code?

故事扮演 提交于 2019-12-13 05:17:07
问题 In an LJ post, the --defsym flag was used for passing the build date into the source code: #include <stdio.h> extern char __BUILD_DATE; void main(void) { printf("Build date: %u\n", (unsigned long) &__BUILD_DATE); } by linking with the following flags: gcc example.c -Xlinker --defsym -Xlinker __BUILD_DATE=$(date +%Y%m%d) According to the ld manual, --defsym symbol=expression Create a global symbol in the output file, containing the absolute address given by expression. I am trying to

Iterating over a QVariant that is a QList<int>?

徘徊边缘 提交于 2019-12-13 05:07:11
问题 I'm using QObject's dynamic property to store information to be used in a Slot that can access said property. The sender is a QState with: myQState->setProperty("key", QList<int>(0, 1, 2)); I would like to convert the stored QVariant back to a QList so it can be iterated. The following code does not work (error C2440: QVariant can't be converted to QList with {[T=int]): QVariant vprop = obj->property("key"); QList<int> list = vprop; // < - - - - - - - - ERROR foreach (int e, list ) { qDebug()

How to remove the first 3 bytes and last one byte from s64 value?

試著忘記壹切 提交于 2019-12-13 05:04:29
问题 code : s64 end_time; struct timespec ts; getrawmonotonic(&ts); end_time = timespec_to_ns(&ts); How to remove the first three bytes from end_time and last one byte from it?? I want to store it in a uint32. could someone tell me how to do that?? uint32 latency; fscanf(fp, "%lu\n", latency); //fp is reading the end_time and storing in latency. latency = (uint32) (latency >> 8) & 0xFFFFFFFF; 回答1: How about: u32 end_time32 = (u32) (end_time >> 24) & 0xFFFFFFFF; Depending on your definition of

Casting multiple columns from one factor variable

£可爱£侵袭症+ 提交于 2019-12-13 04:59:07
问题 I have picked up an awful public data set that needs a lot of work to make it useful. Here is a simplification: Molten<-data.frame(ID=round(runif(100, 0, 50),0), Element=c(rep("Au", 20), rep("Fe", 10), rep("Al", 30),rep("Cu", 20),rep("Au", 20)), Measure=rnorm(100), Units=c(rep("ppm",10), rep("pct",10), rep("ppb", 80))) Molten$UnitElement<-paste(Molten$Element, Molten$Units, sep="_") Molten<-Molten[!duplicated(Molten[,c("ID", "Element")]),] I have arrived at a data frame with the IDs and a

Write an oveloaded comparison (==) between two primative types, in C++

纵然是瞬间 提交于 2019-12-13 04:58:21
问题 As pointed out by this article, it is impossible to overload the comparison operator (==) such that both sides could take primitive types. "No, the C++ language requires that your operator overloads take at least one operand of a "class type" or enumeration type. The C++ language will not let you define an operator all of whose operands / parameters are of primitive types." (parashift) I was wondering: **If I really-really needed to compare two primitives in a non-standard way using the ==,