1

My string contains the bytes (e.g 0x27), basically what I need to do is convert that string array which contains the byte data to a byte data type, so then I can encode it in UTF8, so it displays meaningful info.

1 string array contains:

0x37, 0x32, 0x2d, 0x38, 0x33, 0x39, 0x37, 0x32,0x2d, 0x30, 0x31

I need that converted to a byte array, is that possible?

My code is:

        string strData;
        string strRaw;

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.InnerXml = Data;
        XmlElement xmlDocElement = xmlDoc.DocumentElement;

        strData = xmlDocElement.GetAttribute("datalabel").ToString();
        strRaw = xmlDocElement.GetAttribute("rawdata").ToString();

        string[] arrData = strData.Split(' ');
        string[] arrRaw = strRaw.Split(' ');

Thanks for any help.

2
  • 1
    Do you mean the string contains text like '0x23 ' or the chars in the string have the value of 23 hex etc? Commented Feb 16, 2012 at 19:14
  • Text is 0x23, so like for example: string str = "0x23" Commented Feb 16, 2012 at 19:29

4 Answers 4

7

To say the 'string contains the bytes' could be interpreted in a few ways. You can extract a string into bytes in a number of ways. Converting a string directly into bytes based on UTF8 encoding:

var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);

There are of course similar methods for other encodings.

Ignore the above

Your comment changes the way the question reads quite a bit! If your strings are just hex (i.e. the bytes are not encoded into the string) just convert from hex to integers. Something like....

var b = Convert.ToUInt32(str.Substring(2), 16)

// For an array
var bytes = new byte[arrData.Length];
for(var i = 0; i < arrData.Length; i++) {
   bytes[i] = (byte)Convert.ToUInt32(arrData[i].Substring(2), 16);
}
Sign up to request clarification or add additional context in comments.

7 Comments

The problem I have is, the bytes are stored in an array, and GetBytes does not accept a string array.
Sorry I misunderstood the question - your comment - str = "0x23" changes this entirely. All you want to do is parse these strings as hex. var b = Convert.ToUInt32(str.Substring(2), 16)
Ok so how would I be able to do this with a string array?
You would loop over the array and do this for every element. You could get fancy and then write that as LINQ or something - but at the end of the day it's a loop.
Sorry I forgot to point out, that I need it as a byte array. so from a string array to a byte array, so then I can encode it in UTF8.
|
1

If you have each byte in a char and just want to convert it to a byte array without using an encoding, use;

string blip = "\x4A\x62";
byte[] blop = (from ch in blip select (byte)ch).ToArray();

If you want to convert it using UTF8 encoding right away, use

string blip = "\x4A\x62";
var blop = System.Text.Encoding.UTF8.GetBytes(blip);

Comments

1

given your string is "0x37, 0x32, 0x2d, 0x38, 0x33, 0x39, 0x37, 0x32,0x2d, 0x30, 0x31" or similar you can get the byte values like this;

string input = "0x37, 0x32, 0x2d, 0x38, 0x33, 0x39, 0x37, 0x32, 0x2d, 0x30, 0x31";

string[] bytes = input.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

byte[] values = new byte[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
{
    values[i] = byte.Parse(bytes[i].Substring(2,2), System.Globalization.NumberStyles.AllowHexSpecifier);
    Console.WriteLine(string.Format("{0}", values[i]));
}

once you have them you need to feed them into an apropriate Encoder/Decoder to get the string.

Comments

0

You should be able to do the following:

System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(str);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.