C# Non-ASCII-Printable Bytes To String

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
User avatar
Zone 117




Pi Trickster Stylist Advisor
Snitch! Connoisseur

Posts: 799
Joined: Wed Nov 09, 2005 4:37 pm
Location: Want to be in Asia :'(

C# Non-ASCII-Printable Bytes To String

Post by Zone 117 »

(C#) I'm trying to convert bytes (from an image) that are not ASCII-printable into a string.
Meaning..
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(myBytes);
..does not work, it will show ???? characters.


Any help appreciated.
Image Thanks for the sig Xiion :P
[ AI Revision ][ Sewer ][ Boat Mod ][ Archaic ]
Remapped: Mod Archive and Forums.
User avatar
LuxuriousMeat





Posts: 824
Joined: Thu Nov 03, 2005 6:43 pm
Location: zzzzzzzzzzzzzzzz
Contact:

Re: C# Non-ASCII-Printable Bytes To String

Post by LuxuriousMeat »

Zone 117 wrote:(C#) I'm trying to convert bytes (from an image) that are not ASCII-printable into a string.
Meaning..
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(myBytes);
..does not work, it will show ???? characters.


Any help appreciated.
Uh, are you trying to like show the hexadecimal value of the bytes?
If so use this:

Code: Select all

str = BitConverter.GetString(myBytes);
That will output the hexadecimal value of each byte a delimits each with a dash.
Image
User avatar
Zone 117




Pi Trickster Stylist Advisor
Snitch! Connoisseur

Posts: 799
Joined: Wed Nov 09, 2005 4:37 pm
Location: Want to be in Asia :'(

Re: C# Non-ASCII-Printable Bytes To String

Post by Zone 117 »

I started trying to use the BitConverter a little bit ago. As far as I can see, it converts the bytes to hex correctly. However when I convert the hex back into bytes, half the file is empty.

What method would you use to convert them back into bytes?

Thanks.
Image Thanks for the sig Xiion :P
[ AI Revision ][ Sewer ][ Boat Mod ][ Archaic ]
Remapped: Mod Archive and Forums.
User avatar
LuxuriousMeat





Posts: 824
Joined: Thu Nov 03, 2005 6:43 pm
Location: zzzzzzzzzzzzzzzz
Contact:

Re: C# Non-ASCII-Printable Bytes To String

Post by LuxuriousMeat »

Zone 117 wrote:I started trying to use the BitConverter a little bit ago. As far as I can see, it converts the bytes to hex correctly. However when I convert the hex back into bytes, half the file is empty.

What method would you use to convert them back into bytes?

Thanks.
Here's one I use:

Code: Select all

        byte[] TextStringToBytes(string str)
        {
            string[] parts = str.Split(new char[] { '-' });
            if (parts.Length < 1) return new byte[0];

            byte[] bytes = new byte[parts.Length];
            for (int x = 0; x < parts.Length; x++)
                if (!byte.TryParse(parts[x], out bytes[x])) return new byte[0];

            return bytes;
        }
Example of use:

Code: Select all

string str = "00-02-03-04";
byte[] b = TextStringToBytes(str);
Image
Post Reply