Integer to hex string in C++

Use <iomanip>‘s std::hex. If you print, just send it to std::cout, if not, then use std::stringstream You can prepend the first << with << “0x” or whatever you like if you wish. Other manips of interest are std::oct (octal) and std::dec (back to decimal). One problem you may encounter is the fact that this produces … Read more

%i or %d to print integer in C using printf()?

They are completely equivalent when used with printf(). Personally, I prefer %d, it’s used more often (should I say “it’s the idiomatic conversion specifier for int“?). (One difference between %i and %d is that when used with scanf(), then %d always expects a decimal integer, whereas %i recognizes the 0 and 0x prefixes as octal and hexadecimal, but no sane programmer uses scanf() anyway so this should not be a concern.)

Get int value from enum in C#

Just cast the enum, e.g. The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int. However, as cecilphillip points out, enums can have different underlying types. If an enum is declared as a uint, long, or ulong, it should be … Read more

Convert char array to a int number in C

I personally don’t like atoi function. I would suggest sscanf: It’s very standard, it’s in the stdio.h library 🙂 And in my opinion, it allows you much more freedom than atoi, arbitrary formatting of your number-string, and probably also allows for non-number characters at the end. EDIT I just found this wonderful question here on the site that explains and compares 3 different ways … Read more

c++ parse int from string [duplicate]

In C++11, use std::stoi as: std::string s = “10”; int i = std::stoi(s); Note that std::stoi will throw exception of type std::invalid_argument if the conversion cannot be performed, or std::out_of_range if the conversion results in overflow(i.e when the string value is too big for int type). You can use std::stol or std:stoll though in case int seems too small for the input string. In C++03/98, any of the following can be … Read more

What is the difference between Integer and int in Java?

int is a primitive type. Variables of type int store the actual binary value for the integer you want to represent. int.parseInt(“1”) doesn’t make sense because int is not a class and therefore doesn’t have any methods. Integer is a class, no different from any other in the Java language. Variables of type Integer store references to Integer objects, just as with any other reference (object) type. Integer.parseInt(“1”) is a call to the … Read more

Convert bytes to int?

Assuming you’re on at least 3.2, there’s a built in for this: int.from_bytes( bytes, byteorder, *, signed=False ) … The argument bytes must either be a bytes-like object or an iterable producing bytes. The byteorder argument determines the byte order used to represent the integer. If byteorder is “big”, the most significant byte is at the beginning of the byte array. If byteorder is “little”, the most significant byte is … Read more