passing hex command to c function [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 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

Related

I need a proper English translating for this [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
accept user input via scanf() in the form of 4 hexadecimal digits. Your code will interpret this input as a 16-bit unsigned short. For each input item, your code should extract bits 6 through 9 and print out the unsigned numerical value of those bits
I had a sample that says: input is: abcd the result is: 15
i've be working on it but i was converting to decimal from hex and that was a different thing
i just want an explanation about what extract bits 6 to 9 is
0xabcd = 0b1010 1011 1100 1101
bits --------98 7654 3210
bits 6 to 9 11 11
and
0b1111 == 0xf == 15

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.

convert uint16_t hexadecimal number to decimal 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 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

Set proper bits on n-bit number with given decimal value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Suppose I have the number 0b000 and I need to set the correct bits, so that they will be equal for ex. 5. (0b101).
How do I do that, using bitwise operations algorithm?
Okay, more details then. I'm developing morse code decoder, and to describe a input, I'm using 8 bits: 000 00000, where first three bits are the number of dot/dashes given, and the rest of bits are reserved for the input, where dot is 0, and dash is 1.
For example, the letter A (.-) would be: 010 01000.
The question is, how can I modify the first three bits so that they will show how many dot/dashes were given during the input?
You switch bits on using |. Let's stick with your non-standard notation for binary literals (note that C++14 onwards supports it):
0b000 | 0b100 is 0b100.
0b100 | 0b001 is 0b101.
Note that you can toggle bits using ^ (work through some examples as an exercise).
Finally, you can switch off bits using '&~`.
Solved, if you want to set the first 3 bits, then shift 5 bits to the left (where value is the number you want to set on 3 first bits):
value = value << 5;
And then OR it with the rest of bits:
morseBits = morseBits | value;

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