c# 填充前导八位字节

发布于 2024-10-31 13:12:07 字数 125 浏览 1 评论 0原文

我的指数是 3 个字节长。现在我需要它是 4 个字节。我找到了可以填充前导八位字节的地方..但我不知道该怎么做..所以有人可以帮助我吗?

示例输入:我现在拥有的指数是 65537,然后是 int 字节 01 00 01。

i have exponent whats 3 bytes long. Now i need it to be 4 bytes. I found somewhere that i could pad at the leading octets.. but i have no idea to do that.. So can anybody help me out?

Example Input: exponent what i have right now is 65537, int bytes its then 01 00 01.

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

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

发布评论

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

评论(2

掩耳倾听 2024-11-07 13:12:07

假设您只想用零填充它,创建一个新的四字节数组并将现有的复制到其中:(

byte[] newArray = new byte[4];
// Copy bytes 0, 1, 2 of oldArray into 1, 2, 3 of newArray.
Array.Copy(oldArray, 0, newArray, 1, 3);

您也可以通过三个赋值手动执行此操作;对于这种情况,这可能更简单,但扩展性不好(

如果您发现需要在末尾而不是开头进行填充,请将“1”更改为“0”...或在其中使用 Array.Resize 案件。

Assuming you just want to pad it with zeroes, create a new four byte array and copy the existing one into it:

byte[] newArray = new byte[4];
// Copy bytes 0, 1, 2 of oldArray into 1, 2, 3 of newArray.
Array.Copy(oldArray, 0, newArray, 1, 3);

(You can do this manually with three assignments as well; that's potentially simpler for this situation, but doesn't scale well (in terms of code) to larger sizes.)

Change the "1" to "0" if you find you need the padding at the end instead of the start... or use Array.Resize in that case.

冷默言语 2024-11-07 13:12:07

完全确定其含义,但听起来您只是想要:

byte[] first = /* 3 bytes */
byte[] second = new byte[4];
// since 3 bytes, we'll do this manually; note second[0] is 0 already
second[1] = first[0];
second[2] = first[1];
second[3] = first[2];

当然,如果您实际上正在处理int,那么它已经左侧填充 0,至 4 个字节。

Not entirely sure of the meaning, but it sounds like you just want:

byte[] first = /* 3 bytes */
byte[] second = new byte[4];
// since 3 bytes, we'll do this manually; note second[0] is 0 already
second[1] = first[0];
second[2] = first[1];
second[3] = first[2];

Of course, if you are actually dealing with an int it is already padded on the left with 0, to 4 bytes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文