How to convert a string to bytes?

I got a C # code that converts a string into bytes. But I don’t know how to put him in UE5.

The following is the C # code that converts a string into bytes by clicking the button:

ontrolCode cc = new ControlCode();
                cc.Address = "AA AA AA AA";                     //Super address
                cc.CodeControl = "03";                         //Control code
                cc.CodeLength = "06";                           //Data area length
                cc.CodeContent = "90 00 00 00 00 00";            //90 means the first path, up to 9B (12 paths), the last four bytes 00 00 00 00 are the default password (modifiable), and the last byte is the control code.
                string str = cc.GetCodeString();
                byte[] byteSend = CodeHelper.String2Byte(str);
                byte[] byteRecieved = GetRecievedByte(byteSend);
                if (byteRecieved != null)
                {
                    listAdd(byteSend, byteRecieved);
                }

Complete format C# conversion code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RelayControlTCP
{
    //collect all codes and 
    class CodeHelper
    {
        public static readonly string strConnect = "55 AA AA AA AA AA 22 01 0F D9 16";
        public static string SendString(byte[] byteCode)
        {
            string[] strResult = Code2StringArray(byteCode);
            if (strResult == null)
                return null;
            string strRead = GetTimeString() + " >【Send】:";
            for (int i = 0; i < strResult.Length;i++ )
            {
                strRead += strResult[i];
                strRead+=" ";
            }
            return strRead;
        }

        public static string RecieveString(byte[] byteCode)
        {
            string[] strResult = Code2StringArray(byteCode);
            if (strResult == null)
                return null;
            string strRead = GetTimeString() + " >【receive】:";
            for (int i = 0; i < strResult.Length; i++)
            {
                strRead += strResult[i];
                strRead += " ";
            }
            return strRead;
        }

        public static byte[] GetDataByte(byte[] Recieve1)
        {
            byte[] Recieve=CheckBytes(Recieve1);
            int nLength = Convert.ToInt32(Recieve[7].ToString("X2"), 16);
            byte[] btResult =new byte[nLength-1];
            for (int i = 9; i < 9 + nLength-1;i++ )
            {
                btResult[i - 9] = Recieve[i];
            }
            return btResult;
        }
        public static byte GetSignByte(byte[] Recieve1)
        {
            byte[] Recieve = CheckBytes(Recieve1);
            byte btResult = Recieve[8];
            return btResult;
        }

        public static string DecodeString(byte[] byteCode,string strIP)
        {
            string[] strResult = Code2StringArray(byteCode);
            if (strResult == null)
                return null;
            string strRead = GetTimeString() + " >[Decoding]: Remote IP:";
            strRead += strIP;

            strRead += "  Device address:";
            for (int i = 1; i < 5; i++)
            {
                strRead += strResult[i];
            }

            strRead += ",Control code:";
            strRead += strResult[6];

            int nLength = int.Parse(strResult[7])-1;

            strRead += ",Identification code:";
            strRead += strResult[8];

            strRead += ",Data area:";
            for (int i = 9; i < nLength+9; i++)
            {
                strRead += strResult[i];
            }
            return strRead;
        }

        public static string ReportString(byte[] byteCode)
        {
            string[] strResult = Code2StringArray(byteCode);
            string strRead = GetTimeString() + ">Enter status report! Original code:";
 
            strRead += "  Device address:";
            for (int i = 1; i < 5; i++)
            {
                strRead += strResult[i];
            }

            strRead += ",Control code:";
            strRead += strResult[6];

            int nLength = int.Parse(strResult[7]);

            strRead += ",Identification code:";
            strRead += strResult[8];

            strRead += ",Data area:";
            for (int i = 9; i < nLength; i++)
            {
                strRead += strResult[i];
            }
            return strRead;
        }

        private static string GetTimeString()
        {
            DateTime time = System.DateTime.Now;
            return time.ToString("yyyy-MM-dd HH:mm");
        }

        public static byte[] String2Byte(string str)
        {
            byte[] btCode = null;
            string[] strs = str.Split(new char[] { ' ' });
            btCode = new byte[strs.Length];
            for (int m = 0; m < strs.Length; m++)
            {
                btCode[m] = Convert.ToByte(strs[m], 16);
            }
            return btCode;
        }
        private static byte[] CheckBytes(byte[] byteCode)
        {
            if (byteCode.Length < 11)
                return null;
            if (byteCode[0]!=85)
            {
                return null;
            }
            else
            {
                if (byteCode[1] != 85)
                    return byteCode;
                if(byteCode[1]==85)
                {
                    byte[] byteNew = new byte[byteCode.Length - 1];
                    for (int i = 1; i < byteCode.Length;i++ )
                    {
                        byteNew[i - 1] = byteCode[i];
                    }
                    return byteNew;
                }
            }
            return null;
        }

        public static string[] Code2StringArray(byte[] byteCode)
        {
            string[] strResult1 = new string[byteCode.Length];
            string[] strResult = null;
            for (int i = 0; i < byteCode.Length; i++)
            {
                strResult1[i] = byteCode[i].ToString("X2");
            }
            if (strResult1.Length < 11)
                return null;
            if (strResult1[1] == "55")
            {
                strResult = new string[strResult1.Length - 1];
                for (int i = 1; i < strResult1.Length; i++)
                {
                    strResult[i - 1] = strResult1[i];
                }
            }
            else
            {
                strResult = strResult1;
            }
            //检查数据是否有读错
            int nDataLength = Convert.ToInt32(strResult[7], 16);// int.Parse(strResult[7]);
            if (nDataLength + 10 != strResult.Length)
            {
                return null;
            }

            return strResult;
        }

        public static string SplitString(string str)
        {
            string strResult = null;
            if (str.Length % 2 != 0)
                return "The number is not an integral multiple of 2, there is an error!";
            for (int i = 0; i < str.Length;i=i+2)
            {
                strResult += str.Substring(i, 2);
                strResult += " ";
            }
            return strResult.Trim();
        }
    }
}

I want to change this conversion method to UE5 C++.
Thank you!

Hi,

This entry from the docs may help you.

3 Likes

Thank you