int

In C, why is sizeof(char) 1, when 'a' is an int?

I tried printf("%d, %d\n", sizeof(char), sizeof('a')); and got 1, 4 as output. If size of a character is one, why does 'c' give me 4? I guess it's because it's an integer. So when I do char ch = 'c'; is there an implicit conversion happening, under the hood, from that 4 byte value to a 1 byte value when it's assigned to the char variab...

int or uint or what

Consider this int i = 2147483647; var n = i + 3; i = n; Console.WriteLine(i); // prints -2147483646 (1) Console.WriteLine(n); // prints -2147483646 (2) Console.WriteLine(n.GetType()); // prints System.Int32 (3) I am confused with following (1) how coul...

Python, int to hex string

I want to take an integer (that will be <= 255), to a hex string representation eg: I want to pass in 65 and get out '\x65', or 255 and get '\xff' I've tried doing this with the struct.pack('c', 65), but that chokes on anything above 9 since it wants to take in a single character string. Thanks, ...

I am getting a System.NullReferenceException : Object reference not set to an instance of an object when trying to add a value to an array at runtime

I have a piece of code: EDIT: The _penParams are initialized as the added line below. ProjectionParameters _penParams = new ProjectionParameters(); [Given(@"Rate Rule List $raterule")] public void Rate_Rule_List(Int32 raterule) { _penParams.RateRuleIds.Initialize(); _penParams.RateRuleIds.Add(raterule...

Random int 1-255 to character in C

I have a function that returns an integer, between 1 and 255. Is there a way to turn this int into a character I can strcmp() to an existing string. Basically, I need to create a string of letters (all ASCII values) from a PRNG. I've got everything working minus the int to char part. There's no Chr() function like in PHP. ...

C++: Compiler warning for large unsigned int

I have following array, that I need to operate by hand on bitmaps. const unsigned int BITS[32] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, ...

C# string[] to int[]

Is there a way to convert string arrays to int arrays as easy as parsing an string to an int in C#. int a = int.Parse(”123”); int[] a = int.Parse(”123,456”.Split(’,’)); // this don't work. I have tried using extension methods for the int class to add this functionality myself but they don't become static. Any idea on how to do this f...

Python Type Conversion

Whats the best way to convert int's, long's, double's to strings and vice versa in python. I am looping through a list and passing longs to a dict that should be turned into a unicode string. I do for n in l:     {'my_key':n[0],'my_other_key':n[1]} Why are some of the most obvious things so complicated? ...

Java: for loop, incompatible types

I'm trying to run this for loop; for (int col= 0; grid[0].length; col++) However every time I try to compile I get an error stating 'incompatible types - found int but expected boolean' I can't work out what I'm doing wrong! ...

Objective C Convert int to NSString (iPhone)

I have the following code that is meant to convert milliseconds into hours, mins and seconds: int hours = floor(rawtime / 3600000); int mins = floor((rawtime % 3600000) / (1000 * 60)); int secs = floor(((rawtime % 3600000) % (1000 * 60)) / 1000); NSLog(@"%d:%d:%d", hours, mins, secs); NSString *hoursStr = [NSString stringWithFor...

Using CompareTo() on different .NET types (e.g. int vs. double)

Hi, I've got a static method that accepts two object type variables and runs the CompareTo() method: public static int Compare(Object objA, Object objB) { return (((IComparable)objA).CompareTo(objB)); } Problem is that CompareTo() throws an exception when trying to compare between different types (e.g. int and double). Does any one...

Converting an int to a binary string representation in Java?

What would be the best way (ideally, simplest) to convert an int to a binary string representation in Java? For example, say the int is 156. The binary string representation of this would be "10011100". ...

C - Rounding integer division up (instead of truncating)

I was curious to know how I can round a number to the nearest tenth whole number. For instance, if I had: int a = 59 / 4; which would be 14.75 calculated in floating point; how can I store the number as 15 in "a"? ...

What's a great way to convert a string to a number in C#?

I'm familiar with: Convert.ToInt32(texthere); But is there another cleaner way to do it? I like having readable code for coworkers and I'm always on the lookout for anything that'll make my work seem more obvious. ...

Turning Floats into Their Closest (UTF-8 Character) Fraction.

I want to take any real number, and return the closest number, with the closest fraction as available in the UTF-8 character set, appropriate. 0/4 = 0.00 = # < .125 1/4 = 0.25 = ¼ # > .125 & < .375 2/4 = 0.50 = ½ # > .375 & < .625 3/4 = 0.75 = ¾ # > .625 & < .875 4/4 = 1.00 = # > .875 I made this function to do that task: functio...

Java HashSet<Integer> to int array

I've got a HashSet with a bunch of (you guessed it) integers in it. I want to turn it into an array, but calling hashset.toArray(); returns an array of Object type. This is fine, but is there a better way to cast it to an array of int, other than iterating through every element manually? A method I want to pass it to void doSomething...

How are integers casted to bytes in Java?

I know Java doesn't allow unsigned types, so I was wondering how it casts an integer to a byte. Say I have an integer a with a value of 255 and I cast the integer to a byte. Is the value represented in the byte 11111111? In other words, is the value treated more as a signed 8 bit integer, or does it just directly copy the last 8 bits of ...

How do i convert String to Integer/Float in Haskell

data GroceryItem = CartItem ItemName Price Quantity | StockItem ItemName Price Quantity makeGroceryItem :: String -> Float -> Int -> GroceryItem makeGroceryItem name price quantity = CartItem name price quantity I want to create a GroceryItem when using a String or [String] createGroceryItem :: [String] -> GroceryItem createGrocery...

C++ Stringstream int to string but returns null

Hi below is my function: string Employee::get_print(void) { string out_string; stringstream ss; ss << e_id << " " << type << endl; out_string = ss.str(); return out_string; } e_id and type are int and they contain values from the class Employee. But when I pass them into the stringstream they just clear the string wh...

How to force c# binary int division to return a double?

How to force double x = 3 / 2; to return 1.5 in x without the D suffix or casting? Is there any kind of operator overload that can be done? Or some compiler option? Amazingly, it's not so simple to add the casting or suffix for the following reason: Business users need to write and debug their own formulas. Presently C# is getting us...