Question about encoding hex bytes in to an ascii string. ex: “ABC ABC x41x42x43”

SOLVED: Thank you tewq for your reply. Apparently it works the same in c# as c/++. I had a bug elsewhere in my code that was throwing off my results. I appreciate the help.

Hello,

Forgive me if this is a simple problem. I have so far had little luck googling for solutions. I will explain exactly what I am referring to:

Note: I am using C# with Unity(game engine); Visual Studio as the IDE.

So I have a string of ascii characters, for example:

string demo = “ABC ABC”;

I would like to add some non-printable characters to the string. Is there a way to add characters using their hex value via some kind of escape character? I know C/++ has this capability using x. For example, to add ABC to the end of the aforementioned string, in C I could do:

string demo = “ABC ABC x41x42x43”;

*note, 0x41 is hexadecimal for 65, which is the character A

So is there a similar method in C#?

I have searched for a solution and have found some ways to convert between decimal and hex:

Convert.ToInt32(myHex, 16); //char to byte

byte b = Convert.ToByte(myChar); //byte to char

This, however, is obviously not as convenient as just adding the characters in to the string during instantiation.

I would appreciate any advice or guidance on this subject, or even just advice about converting between different formats to view a byte. (A, 65, 0x41, etc)

Thanks!

submitted by /u/AlwaysSpeakTruth
[link] [comments]

Leave a Reply