- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个字节数组如下 -
byte[] arrByt = new byte[] { 0xF, 0xF, 0x11, 0x4 };
所以二进制 arrByt = 00001111 00001111 00010001 000000100
现在我想通过从 arrByt 中删除每个字节的前导 0 来创建一个新的字节数组
arrNewByt = 11111111 10001100 = { 0xFF, 0x8C };
我知道这可以通过将字节值转换为二进制字符串值、删除前导 0、附加值并将字节值转换回新数组来完成。然而,对于大型阵列来说,这是一个缓慢的过程。
有没有更快的方法来实现这一点(比如逻辑运算、位运算或其他高效的方法)?
谢谢。
最佳答案
这应该会很快完成工作。至少只有标准循环和运算符。试一试,也适用于更长的源阵列。
// source array of bytes
var arrByt = new byte[] {0xF, 0xF, 0x11, 0x4 };
// target array - first with the size of the source array
var targetArray = new byte[arrByt.Length];
// bit index in target array
// from left = byte 0, bit 7 = index 31; to the right = byte 4, bit 0 = index 0
var targetIdx = targetArray.Length * 8 - 1;
// go through all bytes of the source array from left to right
for (var i = 0; i < arrByt.Length; i++)
{
var startFound = false;
// go through all bits of the current byte from the highest to the lowest
for (var x = 7; x >= 0; x--)
{
// copy the bit if it is 1 or if there was already a 1 before in this byte
if (startFound || ((arrByt[i] >> x) & 1) == 1)
{
startFound = true;
// copy the bit from its position in the source array to its new position in the target array
targetArray[targetArray.Length - ((targetIdx / 8) + 1)] |= (byte) (((arrByt[i] >> x) & 1) << (targetIdx % 8));
// advance the bit + byte position in the target array one to the right
targetIdx--;
}
}
}
// resize the target array to only the bytes that were used above
Array.Resize(ref targetArray, (int)Math.Ceiling((targetArray.Length * 8 - (targetIdx + 1)) / 8d));
// write target array content to console
for (var i = 0; i < targetArray.Length; i++)
{
Console.Write($"{targetArray[i]:X} ");
}
// OUTPUT: FF 8C
关于c# - 删除字节数组中的前导 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61693958/
我目前有以下选择语句: SELECT CONCAT(TRUNCATE(HOUR("215:00:36")/24,0),":",HOUR("215:00:36")%24,":",MINUTE("215:
我正在尝试将字符串编码为 base36。 static char *decode(unsigned long long value) { char base36[37] = "01234567
如果我有一个输入字符串,input 有三个或更多小数位,我希望该字符串由单独的 if-loop 处理。 我为此目的创建了以下控制台程序,如果(第一个约束)有小数点 .,则输入 if-loop字符串和(
我有一小段代码,它有一个整数数组,其中包含一些前导 0 的元素。 int arr[]={012,234,071}; cout<
如何从强制范围中删除前导,以便 Some field Any 1 2 3 « 最佳答案 删除 vertical-align: middle 后我觉得不错。 .mandato
我有这个 psd 并试图将它转换为 html css。 但是我无法从psd中计算出css中的行高。 如何根据前导+字体大小计算行高? 谢谢 最佳答案 如果行距不是自动的:LineHeight = fo
有谁知道如何更改文本字段上的行高(行距),最好是在 Interface Builder 中? 我已经找了大约一个小时了,但没有找到。 最佳答案 解决方法如下: Resizing NSTextField
是否可以在 C# 中的单元格元素(行)之间添加空格?我正在 Visual Studio 2012 中创建 pdf,并希望在行之间设置一些空间。我有这样的东西: PdfPTable cellTable
是否可以在 C# 中的单元格(行)元素之间添加空格?我正在 visual studio 2012 中创建一个 pdf,并希望在行之间设置一些空间。我有这样的东西: PdfPTable cellTabl
type ApplyFunc func(commitIndex uint64, cmd []byte) []byte 对于这个声明。我的理解是,这是一个函数指针。它的名字是 ApplyFunc。并且此
我很好奇多列索引的前导列的概念。 我正在使用这个示例 dvdrental分贝。 这是查询: SELECT title, length, rating, replacement_cost
我想将一个 double 格式格式化为最大长度为7的字符串,其中包含一个点“.”以及其后一位数字。 例如: 123.4 becomes "00123.4" 12345 becomes "12345
我需要正则表达式的帮助来从字符串中去除不需要的字符(在 Java 中)。我用 4 个正则表达式解决了这个问题。替换将被多次调用 [peeks: 50+ times/sec] 它并降低性能。但我认为它肯
只是将两个值连接到 mysql 表中的一列。 mysql 表中的第一个值和 PHP 常量中的第二个值保存为 define('ACCOUNTCODESUFFIX','016');。 MySql 查询有点
我如何按此顺序返回这些: 1: aaaa 2: bbbb 3: the cccc 4: dddd 所以忽略开头的“the” 目前我正在使用。 select * from houses order by
我通常使用以下代码将 PHP 变量导入 JavaScript: echo("var zipCode = " . $zipCode . ";"); 这工作正常,但对于以 0 开头的邮政编码,它会被 ch
有什么方法可以用前导 0 和小数来格式化 NSNumber 吗?例如,我需要有能力写 4.5 和 000。目前我有它允许小数,但不允许前导 0。 NSNumberFormatter *f = [[NS
你好我正在以编程方式创建标签栏,在我的功能中需要选择填充自定义颜色的标签,到目前为止我已经使用下面的代码实现了这一点,下面的输出见截图。 tabBarController.tabBar.selecti
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Integer with leading zeroes 有人能告诉我这是怎么回事吗?当我用前导零初始化一个
在 Xcode 6 中,我可以使用编辑器菜单来固定: 引领空间至超视 到 Superview 的尾随空间 顶部空间到Superview Superview的底部空间 我找不到在 Xcode 7 bet
我是一名优秀的程序员,十分优秀!