convert uint16_t hexadecimal number to decimal in C [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have browsed so many questions regarding converting hexadecimal number to a decimal number but I couldn't find a way to convert a uint_16t hexadecimal number to decimal number.Can you help me in this case?Thanks for any advice.

I assume that for hexadecimal you intend it's value representation, for instance:
uint16_t a = 0xFF;
In this case, you are just telling compiler that the variable a has type unsigned int, and it's value is 0xFF (255). There is no difference between writing
uint16_t a = 0xFF;
And
uint16_t a = 255;
It's value will be the same in both cases. You don't need any conversion. Pay attention to the fact that you are using an unsigned integer of length 16 bits, so the maximum value you can give to the variable before hitting an overflow is 2^16 = 65536

Related

How to convert from Hex to Decimal using only Integers in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm receiving the following data over a serial port: <0x1b><0x2e><0x15>...
Each value enclosed in '<>' is a single byte.
I require the third byte from the data so i do this:
int Length;
char Data[..];
Length = Data[2];
But the value of Length is 21 and not 15 because the value written in memory is hex.
How do i convert the decimal representation of 15 to decimal 15?
I've tried converting it to various types and so on..
But none of that works for me as i'm writing a driver and performance matters a lot.
I've looked over stackoverflow and other sites but all the given examples are with strings, none are with plain integers.
When i send it to the rest of the algorithm i run into issues as the algorithm expects 15.
Given int x that contains 8 bits that represent a number using natural packed binary-coded decimal, they can be converted to the number with:
int y = x/16*10 + x%16;
x/16 produces the high four bits, and then multiplying by ten scales them to the tens position. x%16 produces the low four bits. They are kept in the ones position and added to the tens.

Explain commented lines in the body [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
typedef union
{
float f;
struct
{
//unsigned int mantissa : 23;
//unsigned int exponent : 8;
//unsigned int sign : 1;
} field;
} myfloat;
I came across these lines in this code. What to they mean?
The commented lines are members using bitfields. The number after the colon determines the number of bits that the member would use.
Since the struct they are contained in forms a union with a float, they are likely an attempt by somebody to inspect the components of the member f, as a single precision IEEE-754 floating point number, which uses 23 bits of mantissa, 8 bits for the exponent and 1 bit for the sign.
Those commented out lines are the names and lengths of the different sections of bits in a float. What is this code from/what is it supposed to be doing?

passing hex command to c function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In the function declaration below for uart write on a mcu, can I pass a hex command?
uart_write(const uart_t uart, const uint8_t data);
uart_write(uart_1, 0x56);
yes, you can. However, instead of saying pass a hex command it would be more correct to say pass the value using hex representation.
Anyway
uart_write(uart_1, 0x56);
is the same as
uart_write(uart_1, 86); // 86 == 5 * 16 + 6
An integer can be given in many formats/representations - the compiler just converts the value into a representation suitable for the compiler. Perhaps this could be interesting for you: http://www.cplusplus.com/doc/hex/
If you mean a hexadecimal number, yes you can. To write a hexadecimal number, be sure to precede it with 0x. You can also write octal (base 8) numbers by preceding it with a 0
0x56 Hexadecimal 56
011 Octal 11

How can we code in fixed-point representation for signed floating point input? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The microcontroller I have to implement my digital filter does not support floating point operations.
Given an analog input signal (which can take on values from -1.65 V to 1.65 V) sampled at a given rate of 100 Hz, I can only perform fixed-point operations. So I'm guessing I have to convert my input to fixed point first. It is also stated that the output of the ADC is quantized into unsigned 10-bit values.
My problem is.
I know that there is a Qm.n format for fixed-points which includes a sign bit. And none of the references online include conversion from signed input floating point to unsigned fixed-point
AND I FOUND THIS CODE:
int fixedValue = (int)Math.Round(floatValue*Scale);
double floatValue = (double)fixedValue/Scale;
Questions:
1. How can I choose my scaling factor?
2. Is it dependent on the range of my input values and the number of bits used for the fixed-point representation?
3. The Qm.n format uses a signed bit. Can fixed point representations be unsigned?
It all boils down to choosing the scaling factor and mapping from signed input to unsigned 10 bit fixed point (which will be used for further calculations in solving a difference equation then converting it back to double at the output)
Thanks in advance.
Use a simple 2-point interpolation.
#define Value_MAX 1.65
#define Value_MIN (-1.65)
#define value10bit_MAX 1023
#define value10bit_MIN 0
#define slope ((value10bit_MAX - value10bit_MIN)/(Value_MAX - Value_MIN))
int value10bit = (int)Math.Round((floatValue - Value_MIN)*slope + value10bit_MIN);
OP reports "microcontroller that only support fixed-point operations." yet appears to be using (or wants to use) int fixedValue = (int)Math.Round(floatValue*Scale);. So maybe this works for OP

Get 2 MSB digits from a decimal number in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wonder is there anyway to get the first 2 most significant bits of a binary number version of a decimal number without converting it to actually binary number. For example, I want to get "00" from 5 which is "00101" in binary. Does c supports this binary conversion in an easy way? Does c support shifting and bit masking etc?
Thanks
Yes it does!
So to get the first 2 bits from an 8-bit number we need to mask your current number.
We can do this:
some_number = some_number & 0xC0;
some_number &= 0xC0; // This does the same thing with a different syntax
So what we're doing here is performing a bitwise AND with the value 0xC0, converting 0xC0 to binary gives us 1100 0000. So when we AND this value with some_number we get ONLY the top 2 bits as every other value in the number is 0 and so produces a 0 whenever we AND it with anything else.
For numbers larger than 8-bit all we have to do is increase the length of our mask.
Here's another example say we want the top 2 bits of a 32 bit integer
some_number = some_number & 0xC00000

Resources