Convert dec to hex number in C [duplicate] - c

This question already has answers here:
How can I convert an integer to a hexadecimal string in C?
(7 answers)
Closed 8 years ago.
I tried to convert dec to hex. For example convert 255 to hex.
opa = 255.ToString("X");
gives me error:
error: invalid suffix "ToString" on floating constant
I spent lots of time to convert, but couldn't find right way.

You're asking about the wrong language. C does not support the dot operator on integers. To do this in C, you need to print it to a string like so.
char numstr[10];
sprintf(numstr, "%X", 255)

Related

Can i print int in binary format? [duplicate]

This question already has answers here:
Is there a printf converter to print in binary format?
(57 answers)
Closed 2 years ago.
Can I print an integer in binary without additional cycles, conditions, and so on? I know that you can print a number in HEX printf("%x", a) in decimal printf("%d", a) in octal printf("%o", a), but i can't find args for binary. Can you help me?
12 -> 00001100
First of all, no, printf doesn't provide a formater to print a integer in binary.
Second, do not get fooled. Using printf adds additional cycles. Converting an integer to a string (displaying it as decimal, hexadecimal, binary or other) takes some operations. In the case of binary, it's just that you have to do the conversion for printf and provide it the string containing the binary representation.

incorrect macro conversion to int when printing [duplicate]

This question already has answers here:
In C storing values that start with zero get mutated, why?
(4 answers)
Closed 4 years ago.
So I have the following code:
#define x 010000
My intention is to define x as '10000' in decimal but when I print out the above code it gets printed as 4096.
I dont understand how is that being translated to 4096.
Could anyone please explain?
Numbers starting with a 0 are treated as octal in C. 010000 is an integer literal written in the octal notation. Its value is 8**4=4096. Remove the first 0.

Data type to store a 33 digit wide number in C? [duplicate]

This question already has answers here:
Arbitrary-precision arithmetic Explanation
(8 answers)
Closed 8 years ago.
What is the data type that i should use to store a 33 digit wide number.
I am using unsigned long long but overflow occurs.How to deal with it?
__int128 on GCC allows you to store numbers up to 170141183460469231731687303715884105727, with 39 digits.

How to convert char to double in C? [duplicate]

This question already has answers here:
Floating point does not print accurately [duplicate]
(4 answers)
Closed 8 years ago.
I want to convert a char like "12345678901234567890.123 " to double,
so I use sscanf(str, "%lf", &d),
and I print it use printf("%20.3lf", d);
But I get the result is "12345678901234567178.000",
How can I fix it?
You probably can't, since you're trying to store a number with more digits of precision than will fit in your machine's double data type.
You need to use a big-number library, that "manually" computes with arbitrary number of digits and thus can go beyond the limits of your machine's basic data types.
Oh, and "12345678901234567890.123" is not "a char", it's called "a string".

How can integer variable be initialized with the binary number in C? [duplicate]

This question already has answers here:
Can I use a binary literal in C or C++?
(24 answers)
Closed 9 years ago.
We initialize octal by putting 0 as prefix and hexadecimal as 0x. How can we initialize an int variable in binary number? Is there any access specifier in C for binary number?
e.g %o for octal and %x for hexadecimal number.
Recent versions of GCC provide an extension to the C standard. Use 0b or 0B to prefix a bit series like:
int i = 0b0101010;
int a = 0b0001010; for GCC extension

Resources