标签: byte

little-endian系统将数据转换为网络顺序的转换是什么?

将little-endian系统中的数据转换为网络字节顺序所需的基础转换是什么?对于2字节和4字节数据,有众所周知的函数(如htons,ntohl等)来封装更改,1字节数据字符串(如果有的话)会发生什么?

此外,维基百科暗示little-endian是big-endian的镜像,但如果这是真的,为什么我们需要对2字节和4字节数据进行特定处理?

文章"关于圣战和和平辩护"似乎意味着有许多不同口味的小端 - 这是一篇古老的文章 - 它仍然适用吗?是否仍然需要像Java类文件开头那样的字节顺序标记?

最后,网络字节顺序需要4字节对齐吗?

language-agnostic byte alignment endianness

7
推荐指数
1
解决办法
599
查看次数

如何使用unix从文件中提取特定字节

如何从文件中的某些位置的二进制文件中提取12字节块.

如果我想提取前12个字节,我可以做类似的事情

head -c12 file.bin>output
Run Code Online (Sandbox Code Playgroud)

如果我想从byte61中提取12个字节,我可以做类似的事情

head -c72 file.bin|tail -c12 >output
Run Code Online (Sandbox Code Playgroud)

如果我需要提取20个12字节的块,有更简单的方法吗?

谢谢

byte file extract

7
推荐指数
1
解决办法
1万
查看次数

计算字节数组中位总和的最快方法

我有两个长度相同的字节数组.我需要在每个字节之间执行XOR运算,然后计算位数之和.

例如:

11110000^01010101 = 10100101 -> so 1+1+1+1 = 4
Run Code Online (Sandbox Code Playgroud)

我需要对字节数组中的每个元素执行相同的操作.

c# arrays byte bit

7
推荐指数
3
解决办法
2万
查看次数

以字节交换每对位

这是NVIDIA代表在招聘会上提出的一个问题:

编写小而有效的代码来交换一个字节内的每对位; 例如,10 11 01 10应该成为01 11 10 01.

有没有比for通过每个其他索引循环更"有效"的方法呢?我的代码很小,但我想不出这可能比循环更有效"...我猜测可能有办法使用XOR来避免循环,但我不能想办法.

谢谢!

byte swap bits

7
推荐指数
2
解决办法
4206
查看次数

当我传入64个字节时,为什么C#Convert.ToBase64String()给我88作为长度?

我想了解以下内容:

如果我声明64字节作为数组长度(缓冲区).当我转换为基数为64的字符串时,它表示长度为88.长度不应该只有64,因为我传递的是64字节?我完全可能误解了这个实际的工作方式.如果是的话,请你解释一下.

//Generate a cryptographic random number
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

// Create byte array
byte[] buffer = new byte[64];

// Get random bytes
rng.GetBytes(buffer);

// This line gives me 88 as a result. 
// Shouldn't it give me 64 as declared above?
throw new Exception(Convert.ToBase64String(buffer).Length.ToString());

// Return a Base64 string representation of the random number
return Convert.ToBase64String(buffer);
Run Code Online (Sandbox Code Playgroud)

c# byte bits

7
推荐指数
2
解决办法
1931
查看次数

如何在Python中正确声明ctype结构+联合?

我正在搞乱制作二进制数据解析器,虽然我可以回到C,但我想看看我是否可以使用Python来完成任务.

我对如何实现这一点有所了解,我当前的实现看起来像这样:

from ctypes import *

class sHeader(Structure):
    _fields_ = [("CC", c_uint8, 4),
            ("AFC", c_uint8, 2),
            ("TSC", c_uint8, 2),
            ("PID", c_uint16, 13),
            ("TP", c_uint16, 1),
            ("PSI", c_uint16, 1),
            ("TEI", c_uint16, 1),
            ("SyncByte", c_uint8)]

class Header(Union):
    _fields_ = [("sData", sTsHeader),
            ("ulData", c_uint32)]

head = Header()
head.ulData = 0xffffffff
print(head.ulData)
print(head.sData.SyncByte)

print(sHeader.SyncByte)
print(sHeader.TEI)
print(sHeader.PSI)
print(sHeader.TP)
print(sHeader.PID)
print(sHeader.TSC)
print(sHeader.AFC)
print(sHeader.CC)


print(sizeof(sHeader))
print(sizeof(c_uint8))
print(sizeof(c_uint16))
print(sizeof(c_uint32))
Run Code Online (Sandbox Code Playgroud)

哪个产生这个输出:

V:\>C:\Python27\python.exe WidiUnpacker.py
0xffffffffL
0x0
<Field type=c_ubyte, ofs=4, size=1>
<Field type=c_ushort, ofs=2:15, bits=1>
<Field type=c_ushort, ofs=2:14, bits=1>
<Field type=c_ushort, …
Run Code Online (Sandbox Code Playgroud)

python byte struct ctypes unions

7
推荐指数
2
解决办法
4864
查看次数

Python 0xff 字节

我收到这个错误:

\n\n
UnicodeDecodeError: \'utf-8\' codec can\'t decode byte 0xff in position: 0, invalid start byte\n
Run Code Online (Sandbox Code Playgroud)\n\n

我找到了这个解决方案:

\n\n
>>> b"abcde".decode("utf-8")\n
Run Code Online (Sandbox Code Playgroud)\n\n

从这里开始: \n将字节转换为 Python 字符串

\n\n

但是,如果 a)您不\xe2\x80\x99 不知道 0xff 在哪里和/或 b)您需要解码文件对象,您如何使用它?正确的语法/格式是什么?

\n\n

我正在解析一个目录,因此我尝试一次浏览一个文件。(注意:当项目变大时,这将不起作用!!!)

\n\n
>>> i = "b\'0xff\'"\n>>> with open(\'firstfile\') as f:\n...     g=f.readlines()\n... \n>>> i in g\nFalse\n>>> 0xff in g\nFalse\n>>> \'0xff\' in g\nFalse\n>>> b\'0xff\' in g\nFalse\n\n>>> with open(\'secondfile\') as f:\n<snip - same process>\n\n>>> with open(\'thirdfile\') as f:\n...     g = f.readlines()\n... \nTraceback (most recent call last):\nFile "<stdin>", line 2, …
Run Code Online (Sandbox Code Playgroud)

python unicode byte file utf-8

7
推荐指数
2
解决办法
2万
查看次数

声纳问题:删除 String(byte[]) 的使用

我遇到了一个反复出现的声纳问题“删除构造函数“String(byte[])”的使用。示例之一是以下代码:

 byte[] d = c.doFinal(e);
 return new String(d);
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会出现这种情况。欢迎任何帮助。谢谢。

arrays string byte sonarqube

7
推荐指数
1
解决办法
6850
查看次数

Python - 将字节/unicode制表符分隔数据转换为csv文件

我正在从 API 中提取以下数据行。数据以b前缀开头,根据Python 3.3 文档,该前缀表明我们正在处理带有转义序列的“字节文字” \t,并\n分别表示 ASCII 水平制表符 (TAB) 和 ASCII 换行符 (LF)。

b'settlement-id\tsettlement-start-date\tsettlement-end-date\tdeposit-date\ttotal-amount\tcurrency\ttransaction-type\torder-id\tmerchant-order-id\tadjustment-id\tshipment-id\tmarketplace-name\tamount-type\tamount-description\tamount\tfulfillment-id\tposted-date\tposted-date-time\torder-item-code\tmerchant-order-item-id\tmerchant-adjustment-item-id\tsku\tquantity-purchased\n7293436482\t03.05.2018 09:10:07 UTC\t04.05.2018 20:30:23 UTC\t06.05.2018 20:30:23 UTC\t53,44\tEUR\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n7293436482\t\t\t\t\t\tOrder\t303-3746292-6119509\t\t\tDRGC8lFbB\tAmazon.de\tItemPrice\tPrincipal\t179,99\tMFN\t03.05.2018\t03.05.2018 17:12:22 UTC\t30407746733299\t\t\t3700546702556-180412-chp-18c10347-1\t1\n7293436482\t\t\t\t\t\tOrder\t303-3746292-6119509\t\t\tDRGC8lFbB\tAmazon.de\tItemFees\tCommission\t-32,40\tMFN\t03.05.2018\t03.05.2018 17:12:22 UTC\t30407746733299\t\t\t3700546702556-180412-chp-18c10347-1\t1\n7293436482\t\t\t\t\t\tRefund\t305-1251749-5602732\t305-1251749-5602732\tamzn1:crow:YZkTuxs4RhO8FpZez3cGCg\t\tAmazon.de\tItemPrice\tPrincipal\t-109,99\tAFN\t04.05.2018\t04.05.2018 18:24:39 UTC\t38048998219979\t\t142721169810\t3700546702082-180124-jpn-131N28-6\t\n7293436482\t\t\t\t\t\tRefund\t305-1251749-5602732\t305-1251749-5602732\tamzn1:crow:YZkTuxs4RhO8FpZez3cGCg\t\tAmazon.de\tItemFees\tCommission\t19,80\tAFN\t04.05.2018\t04.05.2018 18:24:39 UTC\t38048998219979\t\t142721169810\t3700546702082-180124-jpn-131N28-6\t\n7293436482\t\t\t\t\t\tRefund\t305-1251749-5602732\t305-1251749-5602732\tamzn1:crow:YZkTuxs4RhO8FpZez3cGCg\t\tAmazon.de\tItemFees\tRefundCommission\t-3,96\tAFN\t04.05.2018\t04.05.2018 18:24:39 UTC\t38048998219979\t\t142721169810\t3700546702082-180124-jpn-131N28-6\t\n'
Run Code Online (Sandbox Code Playgroud)

当我使用此数据转换为字符串时,.decode("utf-8")我得到相应的制表符分隔数据:

settlement-id   settlement-start-date   settlement-end-date deposit-date    total-amount    currency    transaction-type    order-id    merchant-order-id   adjustment-id   shipment-id marketplace-name    amount-type amount-description  amount  fulfillment-id  posted-date posted-date-time    order-item-code merchant-order-item-id  merchant-adjustment-item-id sku quantity-purchased
7293436482  03.05.2018 09:10:07 UTC 04.05.2018 20:30:23 UTC 06.05.2018 20:30:23 UTC 53,44   EUR                                                                 
7293436482                      Order   303-3746292-6119509         DRGC8lFbB   Amazon.de …
Run Code Online (Sandbox Code Playgroud)

python csv byte ascii python-3.x

7
推荐指数
1
解决办法
2万
查看次数

GNU 时代的千字节还是千字节?

我们正在进行一些性能测量,包括一些内存占用测量。我们一直在用 GNU 时间来做这件事。但是,我无法判断它们是以千字节(1000 字节)还是千字节(1024 字节)为单位进行测量的。

我的系统的手册页提到了%M格式键(我们用它来测量峰值内存使用量):“进程在其生命周期内的最大驻留集大小,以千字节为单位。”

我假设这里的 K 表示 SI“Kilo”前缀,即千字节。但在通过各种工具查看了其他一些对各种事物的记忆测量后,我相信这个假设,就像我相信一只饥饿的狮子在为期一周的假期期间看管我的狗一样。

我需要知道,因为对于我们的测试,1000 KB 与 1024 KB 的差异加起来接近 8 GB,而且我想我可以将测量中的潜在误差减少几十亿。

memory time byte units-of-measurement measure

7
推荐指数
1
解决办法
512
查看次数