将大量ASCII值(以字符串形式)转换为可读文本C#

时间:2018-09-01 05:50:38

标签: c# python string ascii

从本质上讲,我正在尝试与以下Python代码做相同的事情,只是在C#中。

用户将提供一长串用逗号分隔的ASCII值,例如“ 104、101、108、108、111、44、32、119、111、114、108、100”,我想将其转换可读文本并将该信息返回到文本框。感觉我使事情变得比应有的复杂得多。非常感谢您的帮助!

decodeText = userInputBox.Text;
var arr = new string[] { decodeText};

int[] myInts = Array.ConvertAll(arr, int.Parse);

for(int i = 0; i < myInts.Length; i++)
{
    decodeBox.Text = not sure??
}

Python示例:

  
    
      L = [104、101、108、108、111、44、32、119、111、114、108、100]       ''.join(map(chr,L))       “你好,世界”

    
  

How do I convert a list of ascii values to a string in python?

6 个答案:

答案 0 :(得分:1)

String finalString = String.Empty;

foreach(var item in myInts)
{
   int unicode = item;
   char character = (char) unicode;
   string text = character.ToString();
   finalString += text;
}

如果您想使用String Builder来提高性能。

StringBuilder finalString = new StringBuilder();

foreach(var item in myInts)
{
   char character = (char) item;
   builder.Append(character)
}

答案 1 :(得分:1)

您需要用,分割输入,然后将字符串号解析为实数,并将其转换为char,然后使用其char[] ctor创建字符串。

var result = new string(userInputBox.Text.Split(',').Select(c => (char)int.Parse(c)).ToArray());

答案 2 :(得分:1)

我确实相信您是第一次尝试的好方法:

var arr = userInputBox.Text.Split(‘,’);
textbox.Text = new string(Array.ConvertAll(arr, s => (char)(int.Parse(s)));

在使用convertall将字符串数组更改为int数组的地方,我向char添加了强制类型转换,以使其输出chars数组(我在下面解释了原因),并且可以通过以下方式将其转换为字符串将其传递到新字符串的构造函数中

这导致了更为紧凑的形式(老实说,我可以将它做成一个衬里),但是从学习的角度来看,紧凑性并不总是理想的

因此,如果更易于理解,这可以修复您的代码:

decodeText = userInputBox.Text;
var arr = decodeText.Split(‘,’);

int[] myInts = Array.ConvertAll(arr, int.Parse);

for(int i = 0; i < myInts.Length; i++)
{
    decodeBox.Text += (char)myInts[i];
}

您缺少的关键位(除了使用字符串拆分将字符串拆分为数字字符串数组之外)还需要将int转换为char

在c#中,有一个从int到字符A的直接映射,例如A为65,如果您将int 65转换为char,用(char)则变为A

然后我们将其附加

如果整数列表很长,请考虑使用stringbuilder来构建您的字符串

答案 3 :(得分:0)

一旦您拥有document.querySelector('table').addEventListener('click', ({ target }) => { if (target.matches('button:nth-child(1)')) changeNum(target, 1); else if (target.matches('button:nth-child(2)')) changeNum(target, -1); }); function changeNum(button, changeBy) { const [, quantityElm, priceElm, totalElm] = button.parentElement.parentElement.children; const quantity = Number(quantityElm.textContent) + changeBy; quantityElm.textContent = quantity; const price = Number(priceElm.textContent); const total = quantity * price; totalElm.textContent = total; }的数组:

<table>
  <thead>
    <tr>
      <th>
        Name
      </th>
      <th>Quantitiy</th>
      <th>Price</th>
      <th>Total</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>item</td>
      <td>0</td>
      <td>25</td>
      <td>0</td>
      <td>
        <button>+</button>
        <button>-</button>
      </td>
    </tr>
    <tr>
      <td>item</td>
      <td>0</td>
      <td>5</td>
      <td>0</td>
      <td>
        <button>+</button>
        <button>-</button>
      </td>
    </tr>
    <tr>
      <td>item</td>
      <td>0</td>
      <td>5</td>
      <td>0</td>
      <td> <button>+</button>
        <button>-</button></td>
    </tr>
  </tbody>
</table>

使用Linq轻松将它们转换为ints数组:

int[] asciis = new []{104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100};

然后是字符串表示形式(幸运的是,我们有一个字符串构造函数可以做到这一点):

chars

答案 4 :(得分:0)

直接前进:

var data = new byte[]{104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100};
var str = Encoding.ASCII.GetString(data);

.net fiddle sample

如果您需要转换文本输入中的数据,则需要转换

var input = "104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100";
var data = Array.ConvertAll( 
    input
        .Split(',')
        .Select( e => e.Trim() )
        .ToArray(),
    Byte.Parse );
var str = Encoding.ASCII.GetString(data);

.net fiddle sample

答案 5 :(得分:0)

由于您只是用C#输入的,因此您需要一个可以理解的代码。

model.toJSON({ virtuals: true })

ConvertFromUtf32(int utf32):将指定的Unicode代码点转换为UTF-16编码的字符串。

输出:

enter image description here