Get letter corresponding to number in e (IEEE 1646)

105 views Asked by At

I want to convert from integer values to string characters as follows:

0 to "a"

1 to "b"

and so forth up to

26 to "z"

Is there a way to do this in e without a big case statement?

Note: e is strongly typed and it isn't possible to do any type of arithmetic on string values. There also isn't any char-like type.

Another node: To all you C/C++ hotshots who keep down-voting my question, this isn't as easy a problem as you might think.

2

There are 2 answers

2
Yuri Tsoglin On BEST ANSWER

You can do something like this:

{0c"a"+my_num}.as_a(string)

0c"a" denotes the ASCII value of the letter 'a'. And an as_a() conversion of a list of numbers (actually, bytes) into a string creates a string where each character has the ASCII value of the corresponding list element.

1
Kalev On

You can define a new enum type to correspond to the alphabet, and use the fact that enum values are backed by int values to transform a list of ints to a list of enums, or to a string.

Consider the following example:

<'
type chars : [a, b, c, d, e, f, g];

extend sys {
    run() is also {
        var l : list of int[0..6];
        var s: string = "";

        gen l keeping {it.size() == 5};
        print l;
        for each in l { print it.as_a(chars); };

        for each in l { s = append(s, it.as_a(chars)); };
        print s;
    };
};        
'>

The output of this example will be:

  l = 
0.      4
1.      0
2.      6
3.      4
4.      5
  it.as_a(chars) = e
  it.as_a(chars) = a
  it.as_a(chars) = g
  it.as_a(chars) = e
  it.as_a(chars) = f
  s = "eagef"

Note that you can assign custom values to elements in the enum. In that way, you can assign standard ASCII values to enum elements.

 type chars : [a=10, b=11, c=12, d=13, e=14, f=15, g=16];