I need to split a uint
to a list of bits
(list of chars
, where every char is "0"
or "1"
, is also Ok). The way I try to do it is to concatenate the uint
into string
first, using binary representation for numeric types - bin()
, and then to split it using str_split_all()
:
var num : uint(bits:4) = 0xF; // Can be any number
print str_split_all(bin(num), "/w");
("/w"
is string match pattern that means any char).
The output I expect:
"0"
"b"
"1"
"1"
"1"
"1"
But the actual output is:
0. "0b1111"
Why doesn't it work? Thank you for your help.
If you want to split an integer into a list of bits, you can use the
%{...}
operator: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:The
unpack(...)
syntax is directly from the e Reference Manual, Section 2.8.3 Type Conversion Between Strings and Scalars or Lists of Scalars