Home > Back-end >  Structure convert hexadecimal
Structure convert hexadecimal

Time:10-13

# include
The # pragma pack (1)
using namespace std;

Struct icd {
Float b;
Int d;
};
Icd temp test;
Int main () {
Temp. B=0;
Temp. D=0;
Char buff [50].
Memcpy (buff, & amp; Temp, sizeof (temp));
//buff (icd *);
cout cout <(icd *) buff & lt; return 0;
}

How to put the icd this structure into hexadecimal
If the temp. B=0;
Temp. D=0;
The converted=0000000000000000
If the temp. B=0;
Temp. D=1;
The converted=0000000001000000

CodePudding user response:

Is the temp. As high b 4 bytes, temp. D as low 4 bytes?
Temp. B=0;
Temp. D=0;
Memset (buff, 0, sizeof (buff));
Unsigned char * p=(unsigned char *) & amp; Temp. B;
for (int i=0; i<4. I++) {
Sprintf (buff + I * 2, "% 02 x", * (p + I));
}
P=(unsigned char *) & amp; Temp. D;
for (int i=0; i<4. I++) {
Sprintf (buff + 8 + I * 2, "% 02 x", * (p + I));
}
Printf (" % s \ n ", buff);

CodePudding user response:

No, it's according to different data types into hexadecimal, is just like a communication protocol sample

CodePudding user response:

Communication protocol, then you can in turn back into a good structure ah
Char buff [50].
Memcpy (buff, & amp; Temp, sizeof (temp));//from structure into bytes

Icd target;
Memcpy (& amp; Target, buff, sizeof (target));//to recover from a byte into structure
cout cout CodePudding user response:

You can use pointer strong turn also
cout<((icd *) buff) - & gt; Bcout<((icd *) buff) - & gt; D CodePudding user response:

From the structure into the buff output byte is empty,
coutI want to get the string value
The converted=0000000001000000

CodePudding user response:

You don't understand the relationship between character and memory information, which is the memory of 0000000001000000 and 0000000001000000 is not the same string,
The characters' 0 'memory information is not 0, namely char c=0, in the form of a character is invisible to the print c, char c=' 0 'to see the printed character, so the memory of 0000000001000000 is impossible to print out the string "0000000001000000", if not print, can only use the method of 1/f, say, the memory information into a string,

CodePudding user response:

So to speak, char c='0' memory information is 00110000 (binary zeros corresponding ASCII characters), in the form of characters printed c, it is impossible to print out, 00110000, in the same way, the memory information is 00110000 (i.e., int c=48), can print out the characters' 0 ', and your temp. B=0, memory information is 00000000, 00000000, 00000000, 00000000 (binary), how it can't print out the "00000000"
  • Related