如何使用getString()方法将数组的选定元素解码为c#中的字符串

发布于 2025-02-07 20:06:43 字数 1314 浏览 0 评论 0原文

我正在将一个字节数组从派生的类中解码为主类中的字符串。

  • 我在十六进制的派生类中获取原始值
  • ,将所有元素复制到主类中的其他数组中,
  • 将元素解码为字符串,并将它们分配给新数组的元素,例如
  • 我试图应用的代码中的元素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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文