如果您想了解C#等效于使用pythonslice操作旋转列表和c#执行python的知识,那么本篇文章将是您的不二之选。我们将深入剖析C#等效于使用pythonslice操作旋转列表的各个方面,并为您
如果您想了解C#等效于使用python slice操作旋转列表和c# 执行python的知识,那么本篇文章将是您的不二之选。我们将深入剖析C#等效于使用python slice操作旋转列表的各个方面,并为您解答c# 执行python的疑在这篇文章中,我们将为您介绍C#等效于使用python slice操作旋转列表的相关知识,同时也会详细的解释c# 执行python的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- C#等效于使用python slice操作旋转列表(c# 执行python)
- AC#等效于C的读取文件I / O
- C#等效于AES的Java SecretKeySpec
- C#等效于Java的Arrays.fill()方法
- C#等效于SQL Server数据类型
C#等效于使用python slice操作旋转列表(c# 执行python)
在python中,我可以获取列表my_list并旋转内容:
>>> my_list = list(range(10))>>> my_list[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> new_list = my_list[1:] + my_list[:1]>>> new_list[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
在C#中创建由现有C#列表的两个部分组成的新列表的等效方法是什么?我知道如有必要,我可以用蛮力产生。
答案1
小编典典var newlist = oldlist.Skip(1).Concat(oldlist.Take(1));
AC#等效于C的读取文件I / O
谁能告诉我如何在C#.NET版本2中直接将字节数组放入结构中?像fread
在C语言中熟悉的一样,到目前为止,在读取字节流并自动填充结构方面并没有取得太大的成功。我已经看到了一些使用unsafe
关键字在托管代码中存在指针轨迹的实现。
看一下这个样本:
public unsafe struct foobarStruct{ /* fields here... */ public foobarStruct(int nFakeArgs){ /* Initialize the fields... */ } public foobarStruct(byte[] data) : this(0) { unsafe { GCHandle hByteData = GCHandle.Alloc(data, GCHandleType.Pinned); IntPtr pByteData = hByteData.AddrOfPinnedObject(); this = (foobarStruct)Marshal.PtrToStructure(pByteData, this.GetType()); hByteData.Free(); } }}
我有两个构造函数的原因 foobarStruct
- 是否不能有一个空的构造函数。
- 实例化结构时,将一块内存(作为字节数组)传入构造函数。
该实施方案是否足够好,或者有更清洁的方法来实现这一目标?
编辑: 我不想使用ISerializable接口或其实现。我正在尝试读取一个二进制映像,以计算出使用的字段并使用PE结构确定其数据。
答案1
小编典典使用P /
Invoke编组器没有任何问题,这也不是不安全的,并且您不必使用unsafe关键字。弄错它只会产生错误的数据。与显式编写反序列化代码相比,使用起来容易得多,尤其是在文件包含字符串的情况下。您不能使用BinaryReader.ReadString(),它假定字符串是由BinaryWriter编写的。但是,请确保使用struct声明声明数据的结构,this.GetType()不太可能正常工作。
这是一个通用类,可用于任何结构声明:
class StructureReader<T> where T : struct { private byte[] mBuffer; public StructureReader() { mBuffer = new byte[Marshal.SizeOf(typeof(T))]; } public T Read(System.IO.FileStream fs) { int bytes = fs.Read(mBuffer, 0, mBuffer.Length); if (bytes == 0) throw new InvalidOperationException("End-of-file reached"); if (bytes != mBuffer.Length) throw new ArgumentException("File contains bad data"); T retval; GCHandle hdl = GCHandle.Alloc(mBuffer, GCHandleType.Pinned); try { retval = (T)Marshal.PtrToStructure(hdl.AddrOfPinnedObject(), typeof(T)); } finally { hdl.Free(); } return retval; }
文件中数据结构的示例声明:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]struct Sample { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 42)] public string someString;}
您需要调整结构声明和属性,以与文件中的数据匹配。读取文件的示例代码:
var data = new List<Sample>(); var reader = new StructureReader<Sample>(); using (var stream = new FileStream(@"c:\temp\test.bin", FileMode.Open, FileAccess.Read)) { while(stream.Position < stream.Length) { data.Add(reader.Read(stream)); } }
C#等效于AES的Java SecretKeySpec
我有以下用Java编写的代码。我需要与此等效的C#。
Key key = new SecretKeySpec(keyValue, "AES");Cipher c = Cipher.getInstance("AES");c.init(1, key);byte[] encVal = c.doFinal(Data.getBytes());encryptedValue = new BASE64Encoder().encode(encVal);
答案1
小编典典这里的C#代码等效于Java。
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();AesManaged tdes = new AesManaged();tdes.Key = UTF8.GetBytes(keyValue);tdes.Mode = CipherMode.ECB;tdes.Padding = PaddingMode.PKCS7;ICryptoTransform crypt = tdes.CreateEncryptor();byte[] plain = Encoding.UTF8.GetBytes(text);byte[] cipher = crypt.TransformFinalBlock(plain, 0, plain.Length);String encryptedText = Convert.ToBase64String(cipher);
C#等效于Java的Arrays.fill()方法
我在Java中使用以下语句:
Arrays.fill(mynewArray, oldArray.Length, size, -1);
请提出等效的C#。
答案1
小编典典我不知道框架中执行此操作的任何内容,但是实现起来很容易:
// Note: start is inclusive, end is exclusive (as is conventional// in computer science)public static void Fill<T>(T[] array, int start, int end, T value){ if (array == null) { throw new ArgumentNullException("array"); } if (start < 0 || start >= end) { throw new ArgumentOutOfRangeException("fromIndex"); } if (end >= array.Length) { throw new ArgumentOutOfRangeException("toIndex"); } for (int i = start; i < end; i++) { array[i] = value; }}
或者,如果您要指定计数而不是开始/结束:
public static void Fill<T>(T[] array, int start, int count, T value){ if (array == null) { throw new ArgumentNullException("array"); } if (count < 0) { throw new ArgumentOutOfRangeException("count"); } if (start + count >= array.Length) { throw new ArgumentOutOfRangeException("count"); } for (var i = start; i < start + count; i++) { array[i] = value; }}
C#等效于SQL Server数据类型
对于以下SQL Server数据类型,C#中对应的数据类型是什么?
精确数值
bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
近似数值
float
real
日期和时间
date
datetimeoffset
datetime2
smalldatetime
datetime
time
字串
char
varchar
text
Unicode字符串
nchar
nvarchar
ntext
二进制字符串
binary
varbinary
image
其他数据类型
cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
(来源:MSDN)
我们今天的关于C#等效于使用python slice操作旋转列表和c# 执行python的分享就到这里,谢谢您的阅读,如果想了解更多关于AC#等效于C的读取文件I / O、C#等效于AES的Java SecretKeySpec、C#等效于Java的Arrays.fill()方法、C#等效于SQL Server数据类型的相关信息,可以在本站进行搜索。
本文标签: