相关疑难解决方法(0)

C#little endian还是big endian?

在允许我们通过UDP/IP控制它的硬件文档中,我发现了以下片段:

在这种通信协议中,DWORD是4字节数据,WORD是2字节数据,BYTE是单字节数据.存储格式为小端,即4字节(32位)数据存储为:d7-d0,d15-d8,d23-d16,d31-d24; 双字节(16位)数据存储为:d7-d0,d15-d8.

我想知道这是如何转换为C#的?在发送之前我是否必须转换内容?例如,如果我想发送32位整数或4个字符的字符串?

c# hardware udp endianness

62
推荐指数
3
解决办法
9万
查看次数

如何获取值的二进制表示

可能重复:
c#中的十进制到二进制转换

我有3号,432号,1号等号码.我需要将这些数字转换为零和1的集合,然后将这些位存储在整数数组中,但不确定如何获得任何整数的位表示.

c#

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

为什么Bytes残留?

我最近一直在玩一些字节数组(处理灰度图像).一个字节的值可以是0-255.我正在修改字节,并遇到了我分配给字节的值超出字节范围的情况.它正在为我正在玩的图像做出意想不到的事情.

我写了一个测试,并了解到该字节仍然存在.例:

private static int SetByte(int y)
{
    return y;
}
.....
byte x = (byte) SetByte(-4);
Console.WriteLine(x);
//output is 252
Run Code Online (Sandbox Code Playgroud)

有一个结转!当我们走另一条路时,就会发生这种情况.

byte x = (byte) SetByte(259);
Console.WriteLine(x);
//output is 3
Run Code Online (Sandbox Code Playgroud)

我希望它在第一种情况下将其设置为255,在第二种情况下将其设置为0.这个结转的目的是什么?是不是因为我正在投射这个整数赋值?什么时候在现实世界中有用?

c#

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

C#将int转换为2个字节的数组

如果我的问题不够明确,我会提前道歉,我没有很多c#的经验,我遇到了一个奇怪的问题.我试图将一个int转换为两个字节的数组(例如:取2210并获取:0x08,0xA2),但我得到的只是:0x00,0xA2,我无法弄清楚为什么.非常感谢任何建议.(我已经尝试阅读有关此事的其他问题,但找不到有用的答案)

我的代码:

        profile_number = GetProfileName(); // it gets the int 
        profile_num[0] = (byte) ((profile_number & 0xFF00));
        profile_num[1] = (byte) ((profile_number & 0x00FF));
        profile_checksum = CalcProfileChecksum();
Run Code Online (Sandbox Code Playgroud)

//注意:我指的是一个2字节的数组,所以关于4字节数组的问题的答案对我没有帮助.

c# arrays int

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

解密使用 AES-128 加密的 M3U8 播放列表,无需 IV

我目前正在构建一个用于下载 M3U8 播放列表的应用程序,但我遇到了一个问题:如果播放列表是用 AES-128 加密的,例如有这样一行:

#EXT-X-KEY:METHOD=AES-128,URI="https://website.com/link.key",IV=0xblablabla

我必须在将它们写入输出文件之前解密这些段,如果存在 IV,则下面的代码对我有用,但如果 IV 属性不存在,则解密会产生错误的结果:

var iv = "parsed iv"; // empty if not present
var key_url = "parsed keyurl";

var AES = new AesManaged()
{
    Mode = CipherMode.CBC,
    Key = await Client.GetByteArrayAsync(key_url)
};

if (!string.IsNullOrEmpty(iv))
    AES.IV = Functions.HexToByte(iv.StartsWith("0x") ? iv.Remove(0, 2) : iv);
else
    AES.IV = new byte[16];

//...

using (FileStream fs = new FileStream("file.ts", FileMode.Create, FileAccess.Write, FileShare.Read))
{
    var data = DownloadSegment(...); // Downloads segment as byte array (encrypted)

    byte[] temp = new …
Run Code Online (Sandbox Code Playgroud)

c# aes http-live-streaming m3u8

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

在 C# 中转换 int->hex->binary 时出现错误“十六进制字符串的位数为奇数”

目的 :

首先将整数值转换为 hexstring,然后转换为 byte[]。

例子 :

   Need to convert  int:1024 to hexstring:400 to byte[]: 00000100 00000000
Run Code Online (Sandbox Code Playgroud)

方法:

为了从整数转换为十六进制字符串,我尝试了下面的代码

    int i=1024;
    string hexString = i.ToString("X");
Run Code Online (Sandbox Code Playgroud)

我得到的十六进制字符串值为“400”。然后我尝试使用下面的代码将十六进制字符串转换为字节 []

    byte[] value = HexStringToByteArray(hexValue);

    /* function for converting hexstring to  byte array */
    public  byte[] HexStringToByteArray(string hex)
    {

        int NumberChars = hex.Length;

        if(NumberChars %2==1)
          throw new Exception("Hex string cannot have an odd number of digits.");

        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2) …
Run Code Online (Sandbox Code Playgroud)

c# hex integer bytearray

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

标签 统计

c# ×6

aes ×1

arrays ×1

bytearray ×1

endianness ×1

hardware ×1

hex ×1

http-live-streaming ×1

int ×1

integer ×1

m3u8 ×1

udp ×1