How to convert Int to Hex String in Kotlin?

You can still use the Java conversion by calling the static function on java.lang.Integer: val hexString = java.lang.Integer.toHexString(i) And, starting with Kotlin 1.1, there is a function in the Kotlin standard library that does the conversion, too: fun Int.toString(radix: Int): String Returns a string representation of this Int value in the specified radix. Note, however, … Read more

How do I specify an integer literal of type unsigned char in C++?

C++11 introduced user defined literals. It can be used like this: inline constexpr unsigned char operator “” _uchar( unsigned long long arg ) noexcept { return static_cast< unsigned char >( arg ); } unsigned char answer() { return 42; } int main() { std::cout << std::min( 42, answer() ); // Compile time error! std::cout << … Read more

Formatting long numbers as strings

This version does not suffer from the bug in the previous answers where 999,999 gives you 1000.0K. It also only allows 3 significant figures and eliminates trailing 0’s. def human_format(num): num = float(‘{:.3g}’.format(num)) magnitude = 0 while abs(num) >= 1000: magnitude += 1 num /= 1000.0 return ‘{}{}’.format(‘{:f}’.format(num).rstrip(‘0’).rstrip(‘.’), [”, ‘K’, ‘M’, ‘B’, ‘T’][magnitude]) The output … Read more

Convert an integer to an array of digits

The immediate problem is due to you using <= temp.length() instead of < temp.length(). However, you can achieve this a lot more simply. Even if you use the string approach, you can use: String temp = Integer.toString(guess); int[] newGuess = new int[temp.length()]; for (int i = 0; i < temp.length(); i++) { newGuess[i] = temp.charAt(i) … Read more

In Django models.py, what’s the difference between default, null, and blank?

Direct from Django model field reference: Field.null If True, Django will store empty values as NULL in the database. Default is False. Note that empty string values will always get stored as empty strings, not as NULL. Only use null=True for non-string fields such as integers, booleans and dates. For both types of fields, you … Read more