在 rust 中将字符串转换为 ascii 代码时出现意外的“10”

问题描述 投票:0回答:1

我正在尝试将一个字符串转换为 rust 中的 ascii 代码,以将字符串转换为 BrainFuck 代码:

(变量“input”是字符串“hello”,此片段从第 12 行开始)

for x in input.chars(){
    ascii = x as u8;
    println!("{}", ascii);
    if ascii > 86 {
      target = ascii - 86;
      output = output.clone() + "+[--------->++++++<]>";
      for x in 0..target{
        output = output + "+";
      output = output + ".>";
      }
    
    }else if ascii <= 86 {
      println!("{}", ascii);
      target = ascii - 86;
      output = output.clone() + "+[--------->++++++<]>";
      for x in 0..target{
        output = output + "-";
      output = output + ".>"
      }
    }
  }
  println!("{}", output)

但是,程序在 25:16 处失败,并出现“尝试进行溢出溢出”。这是不寻常的,所以我打印出了所有的 ASCII 值并得到:

104
101
108
108
111
10
10

我不确定为什么会有两个额外的 10,但它们似乎导致了减法溢出。为什么这些 10 被放入 ascii 变量中?难道我在技术上将字符串转换为以 10 为基数,然后 rust 默认将它们放在那里?

rust integer-overflow brainfuck
1个回答
0
投票

10 是换行符 https://symbl.cc/en/000A/ Rust 可能会在下一批 .bf

中添加它们
© www.soinside.com 2019 - 2024. All rights reserved.