且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数字格式转换

更新时间:2023-11-26 08:13:58

这不是解码".一切都是二进制的,包括十六进制.术语十六进制"或十进制"只能与字符串相关联,作为人类可读的数字表示形式.作为数字类型的数字始终为二进制.字符串也是二进制的,但是例如可以在文本编辑器中读取.

没有代表数字数据的单一方法.例如,可以将64位数据以64个字符"0"或"1"的形式表示为一个字符串,作为8个单独的字节,每个字节以"128"或"128"的形式表示为十进制或整数值. "A2",或者有符号或无符号长整数,或者浮点双精度值.由于没有对二进制数据的单一解释,因此该问题没有一定意义.

—SA
It''s not "decode". Everything is binary, including hex. The term "hexadecimal" or "decimal" can only be related to string as a human-readable representation of numbers. The numbers as numeric types are always binary. Strings are binary, too, but they can be readable, say, in a text editor.

There are no single way to represent numeric data. For example, data of 64 bits can be represented in a string as 64 characters ''0'' or ''1'', as 8 separate bytes each represented by a decimal or integer value in the form of ''128'' or ''A2'', or a signed or unsigned long integer, or a floating-point double-precision value. As there is not single interpretation of the binary data, the question does not have a certain sense.

—SA


阅读文章@ http://www.rwc.uc.edu/koehler/comath/11.html [ ^ ]


十六进制是常用的,因为它的四个二进制位恰好是一个十六进制字符(因为以16为基数和4位为您提供的值0-15).

因此,要从二进制转换为十六进制(随机二进制序列):
1001 1101 1111 0010
--- 9 ---- D ---- F ---- 2(用短划线表示空格)

现在,如果要以编程方式执行此操作,则十六进制解释仅仅是一种人类可读的形式.所有数字都在内部以二进制形式存储,因此转换为十六进制实际上除了提供可读性之外并没有提供其他重要功能.
Hexadecimal is commonly used because of the fact that it four binary bits happen to be one hexadecimal character (since its base 16 and four bits give you values 0-15).

Therefore, to go from binary to hex (random binary sequence):
1001 1101 1111 0010
---9----D----F----2 (put dashes for spacing)

Now, if you want to do this programmatically, the hex interpretation is merely a human readable form. All numbers are stored as binary internally, so conversion to hex really doesn''t provide anything significant besides readability.
int value = 11; //<- decimal 11, 
//internally stored as 00000000 00000000 00000000 00001011 (arbitrary byte order)
CString hex_val;
hex_val.Format("%X",value); //<- hex B