Home > Software engineering >  [for] binary calculation
[for] binary calculation

Time:10-06

A hexadecimal number: D2 A3

1. To convert binary

2. The binary invert

3. After the not add 1 at the end of.

4. Determine whether it is negative, such as the D2 (decimal 210) converted to decimal, then determine the if (210 & gt; 127) than is positive, whereas negative

Is a binary number in the char data, then a comparison of a, is this kind of treatment?

CodePudding user response:

Turns into a binary number to a decimal number, are going to have to read every binary number

CodePudding user response:

Fyi:
 # include & lt; Stdio. H> 
#include
#include
#include
Int main () {
Int I, v;
Char bs [33].
Char b [33];
Char hs [9].
Char h [9];
Char s [4];
Char * e;

//decimal integer binary string;
I=1024;
Ltoa (I, b, 2);
Sprintf (bs, "% 032 s", b);
Printf (" I=% d, bs=% s \ n ", I, bs);
//decimal integer hexadecimal string;
I=1024;
Ltoa (I, h, 16);
Sprintf (hs, "% s" 08, h);
Printf (" I=% d, hs=% s \ n ", I, hs);
//hex string converted to a decimal number
Strcpy (hs, "00000400");
Sscanf (hs, "% x", & amp; I);
Printf (" the hs=% s, I=% d \ n ", the hs, I);
//binary string into hexadecimal string;
Strcpy (bs, "00000000000000000000010000000000");
I=strtol (bs, & amp; E, 2);
Ltoa (I, h, 16);
Sprintf (hs, "% s" 08, h);
Printf (" bs=% s, hs=% s \ n ", bs, hs);
//binary string converted to a decimal number;
Strcpy (bs, "00000000000000000000010000000000");
I=strtol (bs, & amp; E, 2);
Printf (" bs=% s, I=% d \ n ", bs, I);
//hex string into a binary string
Strcpy (hs, "00000400");
Sscanf (hs, "% x", & amp; I);
Ltoa (I, b, 2);
Sprintf (bs, "% 032 s", b);
Printf (" the hs=% s, bs=% s \ n ", hs, bs);
//ASC \ GBK string hexadecimal string
Strcpy (s, "a han");
I=0;
While (1) {
If (0==s [I]) break;
Sprintf (hs + I * 2, 02 x %, (unsigned char) s [I]);
i++;
}
The setlocale (LC_ALL, "CHS");
Printf (" s=% s, hs=% s \ n ", s, hs);
//hex string into (GBK Chinese characters) and characters (ASC)
Strcpy (hs, "61 baba");
I=0;
While (1) {
If (1!=sscanf (hs + I * 2, "% 2 x", & amp; V)) break;
S [I]=(char) v.
i++;
}
S [I]=0;
Printf (" the hs=% s, s=% s \ n ", hs, s);

return 0;

}
//I=1024, bs=00000000000000000000010000000000
//I=1024, hs=00000400
//hs=00000400, I=1024
//bs=00000000000000000000010000000000, hs=00000400
//bs=00000000000000000000010000000000, I=1024
//hs=00000400, bs=00000000000000000000010000000000
//s=a han, hs=61 baba
//hs=61 baba, s=a han

CodePudding user response:

Whatever the hexadecimal, number or the number of methods are those
D2 if access is in the form of a string to
It is directly related operations can be converted to int

CodePudding user response:

reference xianglitian reply: 3/f
into the system, no matter what the number or the number of methods are those
D2 if access is in the form of a string to
That is converted to int directly for related operation can


Is the character in the form of cstrings, hex go directly into the system, 2 then invert, then calculated the not add 1

CodePudding user response:

1. The number in the computer use is complement, negative transfer the complement of the original code will only take 1, a positive complement is equal to the original code

2.16 base can be recreated the advantage of binary, each hexadecimal a one-to-one correspondence with four bits

3. To give you a hexadecimal number, are you to judge a positive or negative, you have to know the count the number of bits used to be, the key to the:

D2A3 corresponding binary is a 32-bit:
? 1101001010100011? 

If use 32-bit int to store it, so 1 is the highest level, says he is negative, if use 64 - bit longlong to store it, so he is positive, because the highest is 0, so you want to according to turn to a string of hexadecimal Numbers, you have to know what is this number, for example, you want to turn int 32 then you can do this:

 # include "stdafx. H" 
# include "string. H"

Int HexToInt32 (const char * s)
{
Int nRet=0;
Int nLen=strlen (s);
For (int x=0; x {
Int p=s [x];
The switch (p)
{
Case '0' : p=0 x0; break;
Case '1' : p=0 x1; break;
Case '2' : p=0 x2; break;
Case '3' : p=0 x3; break;
Case '4' : p=0 x4; break;
Case '5' : p=0 x 5; break;
Case '6' : p=0 x6; break;
Case '7' : p=0 x7; break;
Case '8' : p=0 by 8; break;
Case '9' : p=0 x9; break;
Case 'A' : p=0 xa; break;
Case 'B' : p=0 xb; break;
Case 'C' : p=0 xc; break;
Case 'D' : p=0 xd; break;
Case 'E' : p=0 xe; break;
Case 'F' : p=0 xf. break;
}
Int nShift=(nLen - 1 - x) * 4;
NRet |=p & lt; }
Return nRet;
}


Int _tmain (int arg c, _TCHAR * argv [])
{
Const char * strHex="D2";
Int n=HexToInt32 (strHex);
Hex: printf (" % s - & gt; Dec: % d ", strHex, n);
getchar();
return 0;
}


Useless C in order to display the whole transformation process is the direct conversion function, HexToInt32 () this function requires hexadecimal string must be capitalized, you need to lower case judgment could switch inside add


CodePudding user response:

The above have a slip of the pen, deposit deposit is 16 bits and 32 bits
  • Related