如何使用getString()方法将数组的选定元素解码为c#中的字符串
我正在将一个字节数组从派生的类中解码为主类中的字符串。
- 我在十六进制的派生类中获取原始值
- ,将所有元素复制到主类中的其他数组中,
- 将元素解码为字符串,并将它们分配给新数组的元素,例如
- 我试图应用
的代码中的元素getTring(byte [] bytes,int byteindex,int bytecount);
- 是否有更简单的方法?
//Derived class byte:
byte[] msg = new byte[8] { 0x01, 0x08, 0x1A, 0x0B, 0xFF, 0x03, 0x31, 0x2C };
//Main Class
byte[] mainClassArray = new byte[8];
string[] ArrayToStr = new string[6];
Array.Copy(MyClass.msg, 0, mainClassArray, 0, 8);
ArrayToStr[0] = ASCIIEncoding.ASCII.GetString(mainClassArray , 0, 4); //decode first 4 and assign to the 1st element of ArrayToStr
ArrayToStr[1] = ASCIIEncoding.ASCII.GetString(mainClassArray , 4, 4); //decode last 4 and assign to the 2nd element of ArrayToStr
ArrayToStr[2] = ASCIIEncoding.ASCII.GetString(mainClassArray , 0, 4); //decode first 4 and assign to the 3rd element of ArrayToStr
ArrayToStr[3] = ASCIIEncoding.ASCII.GetString(mainClassArray , 4, 4); //decode last 4 and assign to the 4th element of ArrayToStr
ArrayToStr[4] = ASCIIEncoding.ASCII.GetString(mainClassArray , 0, 4); //decode first 4 and assign to the 5th element of ArrayToStr
ArrayToStr[5] = ASCIIEncoding.ASCII.GetString(mainClassArray , 4, 4); //decode last 4 and assign to the 6th element of ArrayToStr
I am decoding a byte array from a derived class to string in the main class.
- I get the original value in the derived class in hex
- Copy all the elements to a different array in the main class
- Decode the elements into strings and assign them to the elements of a new array like in the code
- I am trying to apply
GetString (byte[] bytes, int byteIndex, int byteCount);
- Is there any simpler way to do it?
//Derived class byte:
byte[] msg = new byte[8] { 0x01, 0x08, 0x1A, 0x0B, 0xFF, 0x03, 0x31, 0x2C };
//Main Class
byte[] mainClassArray = new byte[8];
string[] ArrayToStr = new string[6];
Array.Copy(MyClass.msg, 0, mainClassArray, 0, 8);
ArrayToStr[0] = ASCIIEncoding.ASCII.GetString(mainClassArray , 0, 4); //decode first 4 and assign to the 1st element of ArrayToStr
ArrayToStr[1] = ASCIIEncoding.ASCII.GetString(mainClassArray , 4, 4); //decode last 4 and assign to the 2nd element of ArrayToStr
ArrayToStr[2] = ASCIIEncoding.ASCII.GetString(mainClassArray , 0, 4); //decode first 4 and assign to the 3rd element of ArrayToStr
ArrayToStr[3] = ASCIIEncoding.ASCII.GetString(mainClassArray , 4, 4); //decode last 4 and assign to the 4th element of ArrayToStr
ArrayToStr[4] = ASCIIEncoding.ASCII.GetString(mainClassArray , 0, 4); //decode first 4 and assign to the 5th element of ArrayToStr
ArrayToStr[5] = ASCIIEncoding.ASCII.GetString(mainClassArray , 4, 4); //decode last 4 and assign to the 6th element of ArrayToStr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论