how to reach the bytes of a number? and why? 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 7 years ago.
Improve this question
I know that in order to reach the bytes of a number we should do:
unsigned int number=value;
char* bytes = (char*)&number;
...but I don't really understand why.
Why do we need to use char *?
Why are we casting here?
Thank you :)

Not entirely sure what your problem is here.
Why do we need to use char *?
A char is a byte (read: 8 binary numbers 0 or 1) that can represent a decimal value from 0-255 or -128 - +127 in signed form. It is by default signed.
an int is bigger then a byte, hence the need to cast it to get a byte.
Not sure without the context why you'd want to, but you can use that to determine endianness. Related SO Question

If you want to get to the bytes of an int, you need a pointer that points at something the size of a byte. A char is defined as the size of a byte, so a pointer to a char lets you get the individual bytes of an int.
int a[10]
int *p_int= &a[0];
p_int++;
The p_int++ increments the variable by the size of an int and so will point to a[1]. It increments with 4 bytes.
char *p_char= (char *)&a[0];
p_char++;
The p_char++ increments the variable by the size of a char and so will point to the second byte of the integer a[0].

Related

How can I merge two ASCII characters? [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 2 years ago.
Improve this question
I want to merge two characters and print them via a single variable by using ASCII (refer to the image below):
[1]: https://i.stack.imgur.com/TWodP.jpg
try this if your machine is little endian
unsigned int C = (b << 8) | a;
printf("%s",&C);
otherwise if your machine is big endian try
unsigned int C = (a << 24) | (b<<16);
printf("%s",&C);
Based on my comment, but improved by Jonathan's input, I propose to do this:
int C;
C= (((unsigned char)a)<<8)|((unsigned char)b);
You have already tried the commented version to be helpful, this one is basically the same, just being robust against potentially negative values of a and b (which I considered out of scope, but Jonathan is right in being as safe as possible).
As for the explanation:
The << 8 part, a so-called bitshift left, moves a value by 8 bit towards the MSBs.
I.e. a 00000000010000001 becomes a 01000000100000000.
To be safe from negative value (see below why that is important), the value is first type-casted to unsigned char. That is the part ((unsigned char)a). Note that I tend to be generous when it comes to using (), some people do not like that. This is done for both values.
With values 'A' and 'B' we end up with
0100000100000000 and
0000000001000010.
The next part uses a bitwise OR (|), in contrast to a logical OR (||).
The result is
0100000101000010, which is what I understand to be your goal.
The importance of protecting against negative input is this. Any negative 8bit value has the MSB set and when cast to a wider data type will end up with all 8 new high bits set. This is because of the representation of negative values in integers in 2-compliment.
The final conversion to the desired wider data type is as Jonathan explains:
If you have (unsigned char)A << 8 as a term, then the unsigned char value is extended to int before the shift occurs, and the result is an int.

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

Convert char hex to char dec 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 5 years ago.
Improve this question
I want to convert a value stored in a variable type unsigned char as a decimal in this way:
unsigned char hexValue = 0x0C;
And I want to convert hexValue into a new unsigned char in "dec" format like this:
unsigned char decValue = 0x12; //Ox0C
I tried sprintf() and strtol() but without good results.
Sometimes it can get confusing understanding the difference between value and representation. So, consider this code:
#include <stdio.h>
int main(void) {
unsigned char hexVal = 0x0C;
unsigned char dec = hexVal;
printf("\nValue expressed in decimal: %d and in hexadecimal: %x", dec,dec);
return 0;
}
See live code
It expresses the notion of ten as a hexadecimal in the first assignment statement. When hexVal is assigned to variable dec what is assigned is the value ten which is stored in a binary format. Since dec is already a variable, no need for sprintf() here. You may then use the format specifier to express the value of ten as a decimal or in another base. In this case. The value is a expressed as a decimal.

Unsigned int overflow [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am facing an unsigned integer over flow issue
i.e unsigned int x= < max value * max value >
when I print x it is giving me the -ve value even though it is of unsigned integer
I am eager to understands how the compiler is making that as a negative vale
how do I over come this problem ??
thank you in advance
The compiler itself is not treating it as a signed value, that's almost certainly because you're using the %d format string for outputting it.
Use the %u one for unsigned decimal values and you see it have the "right" value (right in terms of signedness, not right in terms of magnitude, which will be wrong because you've performed an operation leading to overflow).
How are you printing it? Probably using printf. printf prints your unsigned ints as if they were signed (at least if you use %d). But this doesn't change the fact that the number is unsigned and hence positive.
Here's how you can check it: compare it to 0 and see what happens. So add this right after your printf:
if (x>=0) printf("positive\n");
else printf("negative\n");
and see what happens.

Resources