|Subject:||Re: Question about bin2dec|
|Date:||Fri, 30 Jun 2006 11:34:32 -0700 (PDT)|
In Matlab 7.0.4 version at my school, the code:
John Eaton wrote:
On 29-Jun-2006, Muthiah Annamalai wrote:
| > I have some questions regarding the bin2dec function. My understanding
| > is that it converts a string of binary characters into a decimal
| > number. However, it looks to me that spaces within the string give
| > rise
| > to a completely different result. As an example:
| >> x = [1 1 1 1 0];
| >> x_str = int2str(x); %convert to string
| > fmt = %1d%3d%3d%3d%3d
| >> x_str
| > x_str = 1 1 1 1 0
| >> bin2dec(x_str)
| > ans = 4680
| > However, if I type
| >> bin2dec('11110')
| > I get an answer of 30, which is what I would expect. The spaces
| > inbetween the characters throw the answer off from the expected result.
| > Is this a bug, or a "feature"? It seems to me that we would not want
| > the result from the first method.
| > -Ron
| I think that is a 'feature'. Especially you can use it like this:
| which will do the job.
It is also a compatibility bug, though the behavior appears to have
been a deliberate choice. Here is the code from base2dec.m:
## Right justify the values before anything else.
d = strjust (d, "right");
## Lookup value of symbols in symbol table, with invalid symbols
## evaluating to NaN and space evaluating to 0.
table = NaN * ones (256, 1);
table (toascii (symbols (1 : base))) = 0 : base-1;
table (toascii (" ")) = 0;
d = reshape (table (toascii (d)), size (d));
Should we opt for compatility here? I don't see that converting
spaces to 0 is any better than ignoring them.
|[Prev in Thread]||Current Thread||[Next in Thread]|