高效的六角操作

发布于 2024-09-16 18:59:39 字数 238 浏览 5 评论 0原文

我有一个由十六进制值表示的字节数组,这些是持续时间。数据可以转换为整数值并乘以常数以获得计时。数据的解码将作为一系列十六进制字符串保存到文件中。操作十六进制值的有效方法是什么?


我在处理数据格式时考虑了性能问题,因为我必须在不同阶段(计算、数据显示等)使用不止一种格式。大多数示例显示从 byte[] 到十六进制字符串(“1A 3C D4”)的转换,反之亦然,但我一直在寻找替代方案,即转换为 Int16 并使用 char[] 数组。

I have a byte array represented by hex values, these are time durations. The data could be converted to integer values and multiplied by a constant to get the timings. The decoding of the data will be saved to a file as a series of hex strings. What would be an efficient way of manipulating hex values?


I was looking at performance issues when dealing with data formats, as I have to work with more than one format at different stages (calculations, data display, etc.). Most examples show the conversion from byte[] to hex string ("1A 3C D4"), and viceversa, but I was looking for an alternative, which is to convert to Int16 and use char[] array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

魄砕の薆 2024-09-23 18:59:39

您没有表示十六进制值的字节数组。您有一个表示数字的字节数组。仅当您表示数字时,表示数字的基数才相关

换句话说:如果您认为字节数组代表十进制整数,您认为它会有什么不同?如果我用英尺和英寸而不是米来表示我的身高会有所不同吗?

现在,如果您尝试表示 16 位数字,我建议使用字节数组不是一个好主意。请改用 ushort[]short[],因为这些 16 位值。如果您在将数据放入这样的数组时遇到问题,请提供详细信息...同样,如果您在操作方面遇到任何其他问题。请注意,直到您将数据写为文本,就计算机而言,实际上不存在数据位于哪个基数的概念。

(请注意,这对于浮点值来说是不同的,例如,十进制和双精度之间的数据实际上是不同的......在那里,表示的基础是数据格式的一部分。它不适用于整数。或者,您可以将所有整数视为二进制,直到您决定将它们格式化为文本......)

You don't have a byte array representing hex values. You have a byte array representing numbers. The base you represent a number in is only relevant when you're representing it.

To put it a different way: if you thought of your byte array as representing decimal integers instead, how do you imagine it would be different? Is my height different if I represent it in feet and inches instead of metres?

Now, if you're trying to represent 16-bit numbers, I'd suggest that using a byte array is a bad idea. Use a ushort[] or short[] instead, as those are 16-bit values. If you're having trouble getting the data into an array like that, please give details... likewise if you have any other problems with the manipulation. Just be aware that until you're writing the data out as text, there's really no such concept as which base it's in, as far as the computer is concerned.

(Note that this is different for floating point values, where the data really would be different between a decimal and a double, for example... there, the base of representation is part of the data format. It's not for integers. Alternatively, you can think of all integers as just being binary until you decide to format them as text...)

伪装你 2024-09-23 18:59:39

来自 MSDN

十六进制(“X”)格式说明符
将数字转换为字符串
十六进制数字。案例如下
格式说明符指示是否
使用大写或小写字符
对于十六进制数字
大于 9。例如,使用“X”
生成“ABCDEF”,并生成“x”
产生“abcdef”。这种格式是
仅支持整数类型。

精度说明符表示
所需的最少位数
结果字符串。如果需要的话,
数字用零填充
left 产生位数
由精度说明符给出。

byte x = 60;
string hex = String.Format("0x{0:X4}", x);
Console.WriteLine(hex); // prints "0x003C"

From MSDN:

The hexadecimal ("X") format specifier
converts a number to a string of
hexadecimal digits. The case of the
format specifier indicates whether to
use uppercase or lowercase characters
for hexadecimal digits that are
greater than 9. For example, use "X"
to produce "ABCDEF", and "x" to
produce "abcdef". This format is
supported only for integral types.

The precision specifier indicates the
minimum number of digits desired in
the resulting string. If required, the
number is padded with zeros to its
left to produce the number of digits
given by the precision specifier.

byte x = 60;
string hex = String.Format("0x{0:X4}", x);
Console.WriteLine(hex); // prints "0x003C"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文