字符串中字节的十六进制表示

发布于 2024-08-03 23:19:40 字数 146 浏览 4 评论 0原文

我在某处读到字符串 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅语花开 2024-08-10 23:19:40

在十六进制数中,有 16 个不同的数字。首先使用用于十进制数字的普通 10 个符号(0 到 9)书写。然后使用拉丁字母表的前 6 个字母,即 A 到 F。

因为每个数字代表 0 到 F 范围内的值,即 1在十六种可能性中,它包含四位信息。因此,在一长串十六进制数字中,您可以将信息的总位数计算为当前位数的四倍。

您的示例字符串“0123456789ABCDEFFEDCBA987654321089ABCDEF01234567”是 48 位数字。这意味着它是一个 48 * 4 = 192 位数字,采用十六进制形式。

如果您有兴趣将这个大数字视为字节序列,只需采用数字对即可,因为每个字节都是 8 位。第一个(从左边数起)几个字节变成 0x010x230x45 等。

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.

蓝眼泪 2024-08-10 23:19:40

这只是一个很大的数字。您习惯的数字(例如“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.

怀里藏娇 2024-08-10 23:19:40

约阿希姆已经解释了理论概念。如果您想在 Java 中亲自尝试这些数字,请查看 java.math.BigInteger

例如,要将十六进制数转换为十进制或任何其他系统:

// the "radix" is 16 because the string represents a hexadecimal number
BigInteger bi = new BigInteger(
        "0123456789ABCDEFFEDCBA987654321089ABCDEF01234567", 16);

// print the number in decimal (digits 0-9)
System.out.println(bi.toString(10));

// print the number in octal (digits 0-7)
System.out.println(bi.toString(8));

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:

// the "radix" is 16 because the string represents a hexadecimal number
BigInteger bi = new BigInteger(
        "0123456789ABCDEFFEDCBA987654321089ABCDEF01234567", 16);

// print the number in decimal (digits 0-9)
System.out.println(bi.toString(10));

// print the number in octal (digits 0-7)
System.out.println(bi.toString(8));
℉服软 2024-08-10 23:19:40
0123456789ABCDEF FEDCBA9876543210 89ABCDEF01234567

3 个(十六进制)密钥,又名 3 * 8 字节或3 * 8 * 8 = 192 位。

0123456789ABCDEF FEDCBA9876543210 89ABCDEF01234567

3 (hex) keys aka 3 * 8 bytes or3 * 8 * 8 = 192 bits.

烟火散人牵绊 2024-08-10 23:19:40

十六进制的每个字符对应 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.

甜中书 2024-08-10 23:19:40

关于你的第二个问题:JVM如何区分不同基数的数字?

它不会也不可能这样做!作为一名程序员,你必须支持 JVM。如果指定小常量,则可以使用前缀来表示十进制、八进制或十六进制基数:

// A leading zero signals a constant in octal base;
// octal 46 is decimal 38
final int n1 = 046;

// A leading "0x" signals a constant in hexadecimal base;
// hex 3f is decimal 63
final int n2 = 0x3f;

// No prefix refers to a regular decimal number
final int n3 = 12;

只有八进制、十进制和十六进制基数的前缀,因为程序员最常使用这些前缀。请注意,二进制常量没有前缀!

如果您像我之前回答第一个问题时那样使用 java.math.BigInteger 类,那么您必须在构造函数中指定基数:

// input numbers in octal, decimal, and hexadecimal;
// Java needs your help to recognize the base!
final BigInteger b8 = new BigInteger("12345", 8);
final BigInteger b10 = new BigInteger("12345", 10);
final BigInteger b16 = new BigInteger("12345", 16);

// output them in decimal system
System.out.println(b8.toString()); // prints "5349"
System.out.println(b10.toString()); // prints "12345"
System.out.println(b16.toString()); // prints "74565"

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:

// A leading zero signals a constant in octal base;
// octal 46 is decimal 38
final int n1 = 046;

// A leading "0x" signals a constant in hexadecimal base;
// hex 3f is decimal 63
final int n2 = 0x3f;

// No prefix refers to a regular decimal number
final int n3 = 12;

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:

// input numbers in octal, decimal, and hexadecimal;
// Java needs your help to recognize the base!
final BigInteger b8 = new BigInteger("12345", 8);
final BigInteger b10 = new BigInteger("12345", 10);
final BigInteger b16 = new BigInteger("12345", 16);

// output them in decimal system
System.out.println(b8.toString()); // prints "5349"
System.out.println(b10.toString()); // prints "12345"
System.out.println(b16.toString()); // prints "74565"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文