1

this sound a bit confusing to me, but i was wondering how to convert a string type to a bytes the string format is same as bytes example :

String Buffer = "31,139,8,0,0,0,0,0,4,0,236,189,9,156,212"; // its bytes on string Format 

i want to convert that string to bytes (**Not Converting the Value of the string but the actual type)

Edit : example of what i want exactly :

`string = "10,156,0,0,4,0,236,156";`

to be

byte[] buffer = {10,156,0,0,4,0,236,156};

5
  • I'm afraid this sounds confusing not only to you... Commented Oct 12, 2016 at 13:35
  • I thought so :( Commented Oct 12, 2016 at 13:37
  • 1
    So split on comma and parse the result as bytes? Commented Oct 12, 2016 at 13:37
  • please give a more detailed explanation of what you're after if you want useful answers Commented Oct 12, 2016 at 13:37
  • See duplicate, replace int with byte. Commented Oct 12, 2016 at 13:40

2 Answers 2

2

Try this for size:

var bytes = Buffer.Split(',').Select(Byte.Parse).ToArray();

This splits your string into an array of strings (separated by ,), then parses them to bytes and converts to an array.

Sign up to request clarification or add additional context in comments.

Comments

0

Like that?

 var bytes = Encoding.Default.GetBytes("your string");

Ok, value

"31,139,8,0,0,0,0,0,4,0,236,189,9,156,212"
.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries)
.Select(x=>Byte.Parse(x)).ToArray()

Code above splits your string (by ',') into several stirngs ("31", "139", ... , "212"), then remove empty entries (if any), and that trying to parse each string to byte. At the result you will get array of bytes.

1 Comment

@jeroenh I answered on question in that way, as I understand it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.