当前位置:网站首页>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
边栏推荐
- 30讲 线性代数 第五讲 特征值与特征向量
- Line test - graphic reasoning -7- different graphic classes
- Line test - graphic reasoning - 3 - symmetric graphic class
- DTC社群运营怎么做?
- [network] Introduction to C language
- 行测-图形推理-2-黑白格类
- 7-51 combination of two ordered linked list sequences
- Sword finger offer 63 Maximum profit of stock
- Leetcode94. Middle order traversal of binary trees
- Force deduction - question 561 - array splitting I - step by step parsing
猜你喜欢
Leetcode1984. Minimum difference in student scores
行测-图形推理-7-相异图形类
行测-图形推理-6-相似图形类
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
肠道里的微生物和皮肤上的一样吗?
Leetcode206. Reverse linked list
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2
LeetCode203. Remove linked list elements
Brush question 3
随机推荐
Sword finger offer 28 Symmetric binary tree
Sword finger offer 55 - I. depth of binary tree
JS triangle
肠道里的微生物和皮肤上的一样吗?
Some parameters of Haikang IPC
Debezium series: binlogreader for source code reading
定位到最底部[通俗易懂]
ASEMI整流桥KBPC1510的型号数字代表什么
面试百问:如何测试App性能?
微生物健康網,如何恢複微生物群落
行测-图形推理-1-汉字类
What does the model number of asemi rectifier bridge kbpc1510 represent
Debezium series: introducing support for the final operator
LeetCode144. Preorder traversal of binary tree
Brush question 6
行测-图形推理-3-对称图形类
GBU1510-ASEMI电源专用15A整流桥GBU1510
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
[untitled] reprint melting ice - track icedid server with a few simple steps
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2