int

Convert int (32 bits) to char (8 bits)

五迷三道 提交于 2020-01-15 04:12:08
问题 I have these definitions: int data = uartBaseAddress[UART_DATA_REGISTER / 4]; // data coming from UART RX port char message[20]; // array of 20 chars Now when I try to do this: message[0] = (char) data; printf("%x", message[0]); It prints (for example): "ffffff9c" . Of course I want only the last 8 bits ("9c") and I don't understand how to properly do the conversion from int to char . EDIT: I mean: i have to populate the array like this: data = 0xFFFFFF9c; message[0] = data & 0xFF; -- it has

Intel x86_64 Assembly: What is good way to convert 32bit integer to 64bit int?

萝らか妹 提交于 2020-01-15 03:46:10
问题 As title says. My way to do this: ; eax holds 32bit integer sal rax, 32 sar rax, 32 ;after operation int is converted (rax holds the same value on 64 bits) Is there more elegant/better/faster way to do this?? 回答1: TL;DR : movsxd , or for the eax special-case, cdqe . Like I explained in one of your previous questions, you can answer these yourself by looking at compiler output. It's really easy to write a function that sign-extends an integer, then go look up the instruction it uses in the

sqrt() - Why am I allowed to provide an argument of int and not only double and the output is also right?

久未见 提交于 2020-01-14 22:02:14
问题 I wondering why the compiler let this pass and is giving the right output, although sqrt() from its prototype normally should only get an double value as argument: In C99 the declaration of the prototype is: double sqrt (double x); #include <stdio.h> #include <math.h> int main (void) { int i = 9; printf("\t Number \t\t Square Root of Number\n\n"); printf("\t %d \t\t\t %f \n",i, sqrt(i)); } Output: Number Square Root of Number 9 3.000000 Why does the compiler not throw a warning at least and

sqrt() - Why am I allowed to provide an argument of int and not only double and the output is also right?

谁说胖子不能爱 提交于 2020-01-14 21:48:14
问题 I wondering why the compiler let this pass and is giving the right output, although sqrt() from its prototype normally should only get an double value as argument: In C99 the declaration of the prototype is: double sqrt (double x); #include <stdio.h> #include <math.h> int main (void) { int i = 9; printf("\t Number \t\t Square Root of Number\n\n"); printf("\t %d \t\t\t %f \n",i, sqrt(i)); } Output: Number Square Root of Number 9 3.000000 Why does the compiler not throw a warning at least and

How to convert int into char* in C (without sprintf)

半世苍凉 提交于 2020-01-14 06:49:38
问题 some day, i wanted to convert in C an integer into a char *, this int can be negative. I wasn't able to use sprintf ,snprintf neither, so how do i do that ? 回答1: To roll one's own itoa() -like function, first one must address how to handle memory. The easiest is to use a large-enough memory for all possible int values ( INT_MIN ). The buffer required size is mathematically ceiling(log10(-INT_MIN))+3 . This can be approximated with: #include <limits.h> #define INT_STR_SIZE (sizeof(int)*CHAR

Adding two strings mathematically?

冷暖自知 提交于 2020-01-13 11:53:02
问题 i was looking around the forums and i still couldnt find my answer to my problem. I got two strings, that are just really an array of numbers. for example(i just choose random numbers string input1="12345678909876543212"; string input2="12345"; I want to add these two string together but act them like there integers. My goal is creating a class where i can add bigger numbers than (long long int) so it can exceed the largest long long int variable. So i revese the string with no problem, so

Adding two strings mathematically?

最后都变了- 提交于 2020-01-13 11:52:43
问题 i was looking around the forums and i still couldnt find my answer to my problem. I got two strings, that are just really an array of numbers. for example(i just choose random numbers string input1="12345678909876543212"; string input2="12345"; I want to add these two string together but act them like there integers. My goal is creating a class where i can add bigger numbers than (long long int) so it can exceed the largest long long int variable. So i revese the string with no problem, so

How to left-pad an integer with spaces?

本小妞迷上赌 提交于 2020-01-13 09:16:33
问题 How to format an Integer value without leading zero? I tried using decimal format but I can't achieve exact result: DecimalFormat dFormat=new DecimalFormat("000"); mTotalNoCallsLbl.setText(""+dFormat.format(mTotalNoOfCallsCount)); mTotalNoOfSmsLbl.setText(""+dFormat.format(mTotalNoOfSmsCount)); mSelectedNoOfCallsLbl.setText(""+dFormat.format(mSelectedNoOfCallLogsCount)); mSelectedNoOfSmsLbl.setText(""+dFormat.format(mSelectedNoOfSmsCount)); I'm getting these output : 500 004 011 234 but I

How to convert AnyObject type to Int in Swift

人盡茶涼 提交于 2020-01-12 03:24:46
问题 I am searching a key in an Array of Dictionarys and I want to convert the result value into an Int value. This is what I tried. if let result = lasrIDArray.flatMap( {$0["\(self.selectedTitle)"]} ).first { print(result) if let number = result as? NSNumber { let tag = number.integerValue let currentScroll = view.viewWithTag(Int(api.selectedCatID)!) as! UIScrollView let lastImgVw = currentScroll.viewWithTag(tag) as! UIImageView print(lastImgVw.frame.origin.y) } } But if let number = result as?

Having Trouble With: Tuple and Int?

为君一笑 提交于 2020-01-11 14:08:16
问题 So I am making a camel game and I get a weird error that says "TypeError: '>' not supported between instances of 'tuple' and 'int'" and I am not sure what this means, I have posted before and I will state this again I am a beginner coder. Thank you to whoever helps here is the code and I will put a comment where the error is that you! import random done = False milesTraveled = 0 thirst = 0 camelTired = 0 nativesDis = -20 canteens = 5 print(''' Welcome to Camel! You have stolen a camel to make