0

我需要将 a 拆分uintlist of bits( list of chars,其中每个 char 是"0"or "1",也可以)。我尝试这样做的方法是首先连接uintstring,使用数字类型的二进制表示 - bin(),然后使用分割它str_split_all()

var num : uint(bits:4) = 0xF; // Can be any number  
print str_split_all(bin(num), "/w");

("/w"是字符串匹配模式,表示任何字符)。我期望的输出:

"0"
"b"
"1"
"1"
"1"
"1"

实际输出是:

0.      "0b1111"

为什么它不起作用?谢谢您的帮助。

4

1 回答 1

3

If you want to split an integer into a list of bits, you can use the %{...} operator:

var num_bits : list of bit = %{num};

You can find a working example on EDAPlayground.

As an extra clarification to your question, "/w" doesn't mean match any character. The string "/\w/" means match any single character in AWK Syntax. If you put that into your match expression, you'll get (almost) the output you want, but with some extra blanks interleaved (the separators).

Regardless, if you want to split a string into its constituting characters, str_split_all(...) isn't the way to go. It's easier to convert the string into ASCII characters and then convert those back to string again:

extend sys {
  run() is also {
    var num : uint(bits:4) = 0xF; // Can be any number  
    var num_bin : string = bin(num);
    var num_bin_chars := num_bin.as_a(list of byte);

    for each (char) in num_bin_chars {
      var char_as_string : string;
      unpack(packing.low, %{8'b0, char}, char_as_string);
      print char_as_string;
    };
  };
};

The unpack(...) syntax is directly from the e Reference Manual, Section 2.8.3 Type Conversion Between Strings and Scalars or Lists of Scalars

于 2014-12-22T15:45:56.463 回答