0

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
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Lha
  • 25
  • 8
  • `Encoding.ASCII` is the 7-bit US-ASCI encoding which doesn't allow any value above 127/0x7F. Where did those bytes come from? A file? A socket? What encoding did the creator of the bytes use? And why convert one byte at a time instead of all of them once, with `var thatString=Encogin.ASCII.GetString(thatByteArray);`? – Panagiotis Kanavos Jun 16 '22 at 06:41
  • Assuming the bytes were generated on your own machine they're probably using the codepage of the machine's locale, so `Encoding.Default.GetString(...)` may work. Otherwise you'll have to find out what encoding was used, or try multiple ones to see which ones work. – Panagiotis Kanavos Jun 16 '22 at 06:46
  • Are you sure those are characters to begin with? `0x01` isn't printable, it's an invisible Start-Of-Header character that only made sense in 1970s terminals. `0x03` is `End-of-Text` and `0x08` is `Backspace`. You won't find such characters in any text file or message – Panagiotis Kanavos Jun 16 '22 at 06:49
  • @Panagiotis _Where did those bytes come from? A file? A socket?_ I am trying to read the byte information from **MCU using CAN bus**, but the above-mentioned byte is just a random value that I have given. I have put `0x0` in front to express the value as HEX value. – Lha Jun 16 '22 at 07:37
  • _And why convert one byte at a time instead of all of them once, with_ I did it to meet the protocol. The CAN message has the same structure of ID, DATA(here referred to as `msg` of 8 byte I receive 3 messages from 3 different IDs, then combine all those messages in one string array and at last display the content of the message in one line. – Lha Jun 16 '22 at 07:37
  • Are you sure those bytes represent *characters* instead of message-specific raw *bit* values? In the [very first Google result on CAN messages I found](https://www.csselectronics.com/pages/can-bus-simple-intro-tutorial) I see that raw DATA isn't human readable (ie not text) and the contents need special decoding. DATA can contain multiple signals and these can have variable lengths – Panagiotis Kanavos Jun 16 '22 at 07:47
  • @PanagiotisKanavos you are right all the data are in the raw bit I use a library to read the message and convert it to the human-readable. Your suggest `var thatString=Encogin.ASCII.GetString(thatByteArray);` is working great reducing much effort, now I think I will rather concat the other messages to `thatString` than declaring a new string array and accessing each index of that array. – Lha Jun 16 '22 at 08:02

0 Answers0