当前位置:网站首页>Byte hexadecimal binary understanding

Byte hexadecimal binary understanding

2022-07-07 23:08:00 Scale biubiu

Hexadecimal

The following content refers to the blog : About 0x16 Base number
Concept

With 0x The starting data represents 16 Base number , The right of each person in the computer is 16(10 The weight of hexadecimal is 10), namely (16 Base number )10 = (10 Base number )1×16. remarks : there 0 It's the number. 0, It's not a letter O!
Why do I need hexadecimal

Programming , We usually use 10 Base number …… After all C/C++ It's high-level language .

such as :
int a = 100,b = 99;

however , Because of the representation of data in the computer , Eventually in binary form , So sometimes binary , It can solve the problem more intuitively . But binary numbers are too long . such as int Type take up 4 Bytes ,32 position . such as 100,
use int The binary representation of the type will be :
0000 0000 0000 0000 0000 0000 0110 0100

Think or operate on such a long number , No one will like . therefore ,C,C++ There is no way to write binary numbers directly in the code .
use 16 Base or 8 Base can solve this problem . because , The larger the base , The shorter the expression length of a number . however , Why is it that 16 or 8 Base number , And nothing else , Such as 9 or 20 How about the system ?
2、8、16, Namely 2 Of 1 Power ,3 Power ,4 Power . This allows the three bases to be converted to each other very directly .8 Base or 16 Hexadecimal shortens binary numbers , But it keeps the expression characteristic of binary number . In the following course on binary conversion , You can see this .
Conversion from hexadecimal to decimal

2 Base number , Use two Arabic numerals :0、1;
8 Base number , Use eight Arabic numerals :0、1、2、3、4、5、6、7;
10 Base number , Use ten Arabic numerals :0 To 9;

16 Base number , Use sixteen Arabic numerals …… wait , Arabs or Indians , Only invented 10 That's a number ?16 The base is the square 16 Into the 1, But we only have 0~9 These ten numbers , So we use A,B,C,D,E,F These six letters represent 10,11,12,13,14,15. Letters are not case sensitive . The... Of a hexadecimal number 0 The weight of bit is 16 Of 0 Power , The first 1 The weight of bit is 16 Of 1 Power , The first 2 The weight of bit is 16 Of 2 Power ……

therefore , In the N(N from 0 Start ) On a , If it's a number X (X Greater than or equal to 0, also X Less than or equal to 15, namely :F) The size of the representation is X * 16 Of N Power .

Suppose there is a hexadecimal number 2AF5, So how to convert it into 10 How about the system ?
In vertical form :
2AF5 The conversion 10 Base number :
The first 0 position : 5 * 16^0 = 5
The first 1 position : F * 16^1 = 240
The first 2 position : A * 16^2 = 2560
The first 3 position : 2 * 16^3 = 8192

10997
Direct calculation is :
5 * 16^0 + F * 16^1 + A * 16^2+2 * 16^3 = 10997
Hexadecimal numbers convert to each other

If you don't use special writing forms ,16 Hexadecimal numbers will also be the same as 10 Binary mixing . Any number :9876, I can't tell it's 16 Base or 10 Base number .
C,C++ Regulations ,16 Hexadecimal numbers must be in 0x start . such as 0x1 It means a 16 Hexadecimal number . and 1 Represents a decimal . Another example is :0xff,0xFF,0X102A, wait . Among them x It's also case insensitive .( Be careful :0x Medium 0 It's the number. 0, Not letters O)

Here are some usage examples :
int a = 0x100F;
int b = 0x70 + a;
thus , We've learned all the bases :10 Base number ,8 Base number ,16 The expression of hexadecimal number .

C/C++ in ,10 There are positive and negative hexadecimal numbers , such as 12 Express positive 12, and -12 Negative 12,; but 8 Into the system and 16 Base can only be used to represent unsigned positive integers , If you're in code :-078, Or write :-0xF2,C,C++ Don't take it as a negative number .
Hexadecimal numbers convert to each other

The conversion between binary and hexadecimal is more important . However, the conversion between the two does not need to be calculated , Every C,C++ Programmers can see binary numbers , Can be directly converted to hexadecimal numbers , vice versa . So do we , Just finish this section , It can be done .

First let's look at a binary number :1111, How much is it ?
You may have to calculate it like this :1 * 2 ^0+ 1 * 2^1 + 1 * 2^2+ 1 * 2^3 = 1 * 1 + 1 * 2 + 1 * 4 + 1 * 8 = 15.

However , because 1111 only 4 position , So we must directly remember the weight of each bit , And from high to low ,:8、4、2、1. namely , The highest weight is 8, And then in that order 4,2,1.
remember 8421, For any one 4 The binary number of bits , We can all quickly work out the corresponding 10 Base value .

Four bit binary numbers are listed below xxxx All possible values ( Skip the middle part )
only 4 Bit 2 Hexadecimal number Fast calculation method Decimal value Hexadecimal value
1111 = 8 + 4 + 2 + 1 = 15 -> F
1110 = 8 + 4 + 2 + 0 = 14 -> E
1101 = 8 + 4 + 0 + 1 = 13 -> D
1100 = 8 + 4 + 0 + 0 = 12 -> C
1011 = 8 + 0 + 2 + 1 = 11 -> B
1010 = 8 + 0 + 2 + 0 = 10 -> A
1001 = 8 + 0 + 0 + 1 = 9 -> 9

0001 = 0 + 0 + 0 + 1 = 1 1
0000 = 0 + 0 + 0 + 0 = 0 0
byte

Talk about your own understanding :

java in byte Is the smallest basic data type , It's just 8 position , That is to say 2 Of 8 Power . Represents the unsigned integer range 0~256. Represents the range of signed integers -127 ~ 128( Decimal means ). For the understanding of its value range, please refer to the article here for details java Medium byte type

We use it a lot byte[] Array , You can see that the range of each element is -127 ~ 128 Inside . But it can also be expressed in hexadecimal . From the above explanation of hexadecimal, we know ,16 Base can only represent unsigned integers . Then its representation range is 0 ~ 256. It's hexadecimal 0X00 ~ 0xFF(15*16+16).

So there will be such an operation : Hexadecimal byte[] Turn decimal byte[], At first, I didn't understand this statement . It's really just the byte[] Each element in is hexadecimal - Decimal conversion .

We can see in obtaining network data byte[] The data in has for example 7B 5A This kind of data , This is the hexadecimal representation . Because network communication requirements byte[] The data in needs to be represented in hexadecimal .
So if you want to transmit one in the network int value ( Every int yes 32 position ), Need 4 individual byte( Every byte yes 8 position );
If you want to transmit one in the network String character string , Then you need to convert the string to byte[] Array . Pay attention to byte[] Each element in the array is represented in decimal , We need to replace it with 16 Base number , as follows

/**
	 *  String conversion to 16 Base number ( There is no need to Unicode code )
	 * 
	 * @param str
	 * @return
	 */
	public static String str2HexStr(String str) {
		char[] chars = "0123456789ABCDEF".toCharArray();
		StringBuilder sb = new StringBuilder("");
		byte[] bs = str.getBytes();
		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(chars[bit]);
			bit = bs[i] & 0x0f;
			sb.append(chars[bit]);
			// sb.append(' ');
		}
		return sb.toString().trim();
	}
 
	/**
	 * 16 Binary directly converted to string ( There is no need to Unicode decode )
	 * 
	 * @param hexStr
	 * @return
	 */
	public static String hexStr2Str(String hexStr) {
		String str = "0123456789ABCDEF";
		char[] hexs = hexStr.toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2];
		int n;
		for (int i = 0; i < bytes.length; i++) {
			n = str.indexOf(hexs[2 * i]) * 16;
			n += str.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (n & 0xff);
		}
		return new String(bytes);
	}


————————————————
Copyright notice : This paper is about CSDN Blogger 「Androider_Zxg」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/u012545728/article/details/96436953

原网站

版权声明
本文为[Scale biubiu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130600283557.html