字符串中字节的十六进制表示
我在某处读到字符串 0123456789ABCDEFFEDCBA987654321089ABCDEF01234567 是 192 位(24)。它写道它是“字节的十六进制表示”
我需要关于这个概念的帮助。
PS:这是TripleDES算法的密钥。
I read somewhere that string 0123456789ABCDEFFEDCBA987654321089ABCDEF01234567 is 192 bit (24). Its written that it is a "hex representation of bytes"
I need help on this concept.
PS: This is secret key of TripleDES algorithm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在十六进制数中,有 16 个不同的数字。首先使用用于十进制数字的普通 10 个符号(0 到 9)书写。然后使用拉丁字母表的前 6 个字母,即 A 到 F。
因为每个数字代表 0 到 F 范围内的值,即 1在十六种可能性中,它包含四位信息。因此,在一长串十六进制数字中,您可以将信息的总位数计算为当前位数的四倍。
您的示例字符串“0123456789ABCDEFFEDCBA987654321089ABCDEF01234567”是 48 位数字。这意味着它是一个 48 * 4 = 192 位数字,采用十六进制形式。
如果您有兴趣将这个大数字视为字节序列,只需采用数字对即可,因为每个字节都是 8 位。第一个(从左边数起)几个字节变成
0x01
、0x23
、0x45
等。In hexadecimal numbers, you have 16 different digits. These are written using first the ordinary 10 symbols used for decimal digits, 0 through 9. Then the first six letters of the Latin alphabet are used, i.e. A through F.
Since each digit represents a value in the range 0 through F, i.e. one of sixteen possibilities, it holds four bits of information. Thus, in a long string of hex digits, you can compute the total number of bits of information as just four times the number of digits present.
Your example string, "0123456789ABCDEFFEDCBA987654321089ABCDEF01234567", is 48 digits. This means it is a 48 * 4 = 192 bit number, in hexadecimal form.
If you're interested in viewing this large number as a sequence of bytes, just take pairs of digits, since each byte is 8 bits. The first (counting from the left) few bytes then become
0x01
,0x23
,0x45
, and so on.这只是一个很大的数字。您习惯的数字(例如“192”)之间的唯一区别是它是使用 编写的十六进制数字系统而不是十进制数字系统。十六进制数字系统使用 16 位数字(0-9 和 AF),而不是您习惯的 10 位数字(0-9)。
该特定数字相当于十进制表示的 27898229935051914480226618602452055732231960245295072615。
It's just a big number. The only difference between the numbers you are used to (such as "192") is that it's written in using the hexadecimal number system instead of the decimal number system. The hexadecimal number system uses 16 digits (0-9 and A-F) instead of the 10 you are used to (0-9).
That particular number is equivalent to 27898229935051914480226618602452055732231960245295072615 in decimal notation.
约阿希姆已经解释了理论概念。如果您想在 Java 中亲自尝试这些数字,请查看
java.math.BigInteger
。例如,要将十六进制数转换为十进制或任何其他系统:
Joachim already explained the theoretical concept. If you want to play around with such numbers yourself in Java, then take a look at
java.math.BigInteger
.E.g., to convert your hexadecimal number to the decimal or any other system:
3 个(十六进制)密钥,又名
3 * 8
字节或3 * 8 * 8 = 192
位。3 (hex) keys aka
3 * 8
bytes or3 * 8 * 8 = 192
bits.十六进制的每个字符对应 4 位。因此,对于您的示例,有 48 个字符和 48 * 4 = 192 位。
Each character in hexadecimal corresponds to 4 bits. So for your example, there are 48 characters and 48 * 4 = 192 bits.
关于你的第二个问题:JVM如何区分不同基数的数字?
它不会也不可能这样做!作为一名程序员,你必须支持 JVM。如果指定小常量,则可以使用前缀来表示十进制、八进制或十六进制基数:
只有八进制、十进制和十六进制基数的前缀,因为程序员最常使用这些前缀。请注意,二进制常量没有前缀!
如果您像我之前回答第一个问题时那样使用 java.math.BigInteger 类,那么您必须在构造函数中指定基数:
With regard to your second question: How does the JVM distinguish between numbers in different bases?
It does not and can not do so! You as a programmer have to support the JVM. If you specify small constants, then you use a prefix to signal either decimal, octal, or hexadecimal base:
There are only prefixes for octal, decimal, and hexadecimal base because these are most often used by programmers. Note, that there is no prefix for binary constants!
If you use the
java.math.BigInteger
class like I did in my previous reply to your first question, then you have to specify the base in the constructor: