标签: byte

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

如何在C#中将字符串转换为byte []

如何从C#中的字符串中获取字节数组?我想将一个字符串传递给这个方法.

c# string byte cryptography bytearray

8
推荐指数
1
解决办法
921
查看次数

wchar以单个空字节或其中两个结尾?

我只是不明白,也找不到关于wchar end的更多信息.

如果它以单个空字节结束,它是如何知道它不是字符串结束,如果像"009A"这样的东西代表一个unicode符号?

如果以两个空字节结束?好吧,我不确定,需要确认.

c++ string null byte wchar

8
推荐指数
3
解决办法
8361
查看次数

Java:如何"修剪"字节数组?

所以我有一些代码从文件中读取一定数量的字节并返回结果字节数组(这基本上用于分块文件以通过网络发送(最终)base64编码的ascii文本).

它工作正常,除了生成文件的最后一个块时,它不是一个完整的块.因此,生成的字节数组不满.但是,它是一个恒定的大小,这意味着文件被重新组装,有一大堆额外的数据(可能是0)附加到末尾.

我怎样才能使文件的最后一个块的byte []真正只包含它需要的数据?代码如下所示:

 private byte[] readData(File f, int startByte, int chunkSize) throws Exception {
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    raf.seek(startByte);
    byte[] data = new byte[chunkSize];
    raf.read(data);        
    raf.close();
    return data;
}
Run Code Online (Sandbox Code Playgroud)

因此,如果chunkSize大于文件中的剩余字节,则返回一个完整大小的byte [],但它只有半满的数据.

java file-io byte bytearray file

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

如何使用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
查看次数

ByteBuffer和Byte数组

问题

我需要将两个整数和一个可变长度的字符串转换为字节.

我做了什么

我将每种数据类型转换为字节数组,然后将它们添加到字节缓冲区中.之后我将该缓冲区复制到一个字节数组,如下所示.

byte[] nameByteArray = cityName.getBytes();
byte[] xByteArray = ByteBuffer.allocate(4).putInt(x).array();
byte[] yByteArray = ByteBuffer.allocate(4).putInt(y).array();
ByteBuffer byteBuffer = ByteBuffer.allocate(nameByteArray.length + xByteArray.length + yByteArray.length);
Run Code Online (Sandbox Code Playgroud)

现在这似乎有点多余.我当然可以将所有内容放入字节缓冲区并将其转换为字节数组.但是,我不知道我的字符串长度是多少.那么在这种情况下如何分配字节缓冲区呢?(要分配一个字节缓冲区,你必须指定它的容量)

java byte bytebuffer

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

当我传入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
查看次数

标签 统计

byte ×10

c# ×4

bytearray ×3

bits ×2

c++ ×2

file ×2

java ×2

string ×2

arrays ×1

bit ×1

bytebuffer ×1

cryptography ×1

ctypes ×1

extract ×1

file-io ×1

null ×1

python ×1

struct ×1

swap ×1

unions ×1

visual-c++ ×1

wchar ×1