动态设置来自Ints字符串的byte []数组 [英] Dynamicaly set byte[] array from a String of Ints

查看:155
本文介绍了动态设置来自Ints字符串的byte []数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常设置我的byte []数组如下:

i usualy set my byte[] arrays like this:

byte[] byteArr = { 123, 234, 123, 234, 123, 123, 234 };

现在,我的问题,
i我得到的数据必须存储到数组中作为字符串。

now, my problem, i am getting the datas that have to be stored into the array as a string.

示例:

string datas = "123, 234, 123, 234, 123, 123, 234";

我想做类似的事情:

byte[] byteArr = { datas };

没有运气...

我尝试将字符串爆炸为字符串数组,然后在存储到每个数组字段之前将每个值转换为Int。没有运气:

I tried exploding the string to array of strings, then convert each value to Int before storing into each array field. with no luck:

for (var i = O; i<datasArray.length; i++) {
    byteArr[i] = Int32.Parse(datasArray);  //error, cannot convert int to byte
}

我该怎么办?

推荐答案

您可以使用简单的正则表达式从字符串中获取数字

You can use a simple Regex to get the numbers from a string

string datas = "123, 234, 123, 234, 123, 123, 234";
byte[] byteArr = Regex.Matches(datas, @"\d+").Cast<Match>()
                .Select(m => byte.Parse(m.Value))
                .ToArray();

这篇关于动态设置来自Ints字符串的byte []数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆