高效的六角操作
我有一个由十六进制值表示的字节数组,这些是持续时间。数据可以转换为整数值并乘以常数以获得计时。数据的解码将作为一系列十六进制字符串保存到文件中。操作十六进制值的有效方法是什么?
我在处理数据格式时考虑了性能问题,因为我必须在不同阶段(计算、数据显示等)使用不止一种格式。大多数示例显示从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有表示十六进制值的字节数组。您有一个表示数字的字节数组。仅当您表示数字时,表示数字的基数才相关。
换句话说:如果您认为字节数组代表十进制整数,您认为它会有什么不同?如果我用英尺和英寸而不是米来表示我的身高会有所不同吗?
现在,如果您尝试表示 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[]
orshort[]
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...)
来自 MSDN:
From MSDN: