Home > Back-end >  C need questions about numerical convert hex
C need questions about numerical convert hex

Time:09-28

Gave it to a data a=352; 4 types: int, byte length:
Want to send this data to equipment, fixed by agreement, the agreement for the hex format,
(to 352 were divided into 4 bytes, lack of high fill 0, (with two Numbers such as the current as a single byte to 3 52), convert hex 0 x00 0 x00 0 x03 0 x34) stored in buf,
With what method to realize conversion and storage, please

CodePudding user response:

Bytes don't mean that
You describe too disorderly cannot understand
352
The content of the four bytes is 0 x00 0 x00 0 x01 0 x60
Not a byte 03 52 bytes

CodePudding user response:

reference 1st floor lin5161678 response:
bytes don't mean that
You describe too disorderly cannot understand
352
The content of the four bytes is 0 x00 0 x00 0 x01 0 x60
Not a byte 03 52 bytes

I mean you could change into 3 and 52, 352 and then in this converted to hexadecimal!!!!! Is the original data of two split apart, again after the splitting of the converted to hexadecimal

CodePudding user response:

Because it's this kind of situation, this data may be this combination form 352 (XXXXXXXX), receiving equipment directive hex to 00 00 03 34, and then the data is converted to decimal 0 0 3 52, according to the above combinations is 00352, remove the previous 0, 352 (that is the 352 May be a combination of value), so now is must, in turn, will be 352 to 00 00 03 34, how to implement, bosses give directions

CodePudding user response:

reference does not explain, 3/f response:
because it's this kind of situation, this data may be this combination form 352 (XXXXXXXX), receiving equipment directive hex to 00 00 03 34, and then the data is converted to decimal 0 0 3 52, according to the above combinations is 00352, remove the previous 0, 352 (that is the 352 May be a combination of value), so now is, in turn, will be 352 to 00 00 03 34, how to implement, bosses give directions

Don't bother, turn the network byte order directly, regardless of their equipment, large end small end, the receiver back again,

CodePudding user response:

Can use strong, such as
Int a=0 x352;
Unsigned char buf [4].
Memcpy (buf, & amp; A, 4);
This is on the buf,
Actually don't need to use buf this variable, can also
((unsigned char *) & amp; A) [0], ((unsigned char *) & amp; A) [1], ((unsigned char *) & amp; A) [2], ((unsigned char *) & amp; A) [3].

CodePudding user response:

reference 5 building self-confidence boy reply:
can use strong, such as
Int a=0 x352;
Unsigned char buf [4].
Memcpy (buf, & amp; A, 4);
This is on the buf,
Actually don't need to use buf this variable, can also
((unsigned char *) & amp; A) [0], ((unsigned char *) & amp; A) [1], ((unsigned char *) & amp; A) [2], ((unsigned char *) & amp; A) [3],

Need to pay attention to the size of the situation, in a small program to try first, and then understand the big end and small end is to consider the past

CodePudding user response:

Directly to assignment line array, in memory is saved in the form of hexadecimal,
 unsigned char buff [4]={0}; 
Buff [2]=3;
Buff [3]=52;

  • Related