当前位置: 动力学知识库 > 问答 > 编程问答 >

How to convert ASCII integers to string in Streambase?

问题描述:

I am using streambase, and have created a map to convert incoming integers (0-255) into character codes. Right now, I'm using manual if/else statements. For example:

 if (message_code == '106') then

message_code = 'J'

else if (message_code == '110') then

message_code= 'N'

However, I'd like to just use this more generally. Searching Streambase Studio help didn't really provide anything as far as I could tell. I know this can be done in Java, but would probably require calling a java external function. I'm not so competent at this, so am a bit lost.

Is there a better way to do this in StreamBase?

网友答案:

Initial investigations confirm that the StreamBase Expression Language or built-in Functions don't have an obvious way to convert ints representing ASCII character values into a one-character string corresponding to the ASCII value. If there's some non-obvious trick I discover later, I'll come back and edit this answer.

I definitely wouldn't use a 255-clause if/then/else expression!

I would probably use a Java function and here is an example I've lightly tested:

package example;

public class ASCIIIntToString {

    public static String ASCIIToString(Integer a) {
        if (a == null || a < 0 || a > 255) {
            return null;
        } else {
            return Character.toString((char) (int) a);
        }
    }
}

And the custom-function declaration to drop into your sbd.sbconf would look like this:

<custom-functions>
    <custom-function alias="ASCIIToString"
                     args="auto"
                     class="example.ASCIIIntToString"
                     language="java"
                     name="ASCIIToString"
                     type="simple"/>
</custom-functions>

But if you would rather not drop into Java and stay in EventFlow then two methods come to mind:

  1. Load a CSV file with int -> string mappings in them into a Query Table and dto a lookup with Query Read operator, perhaps using the Initial Contents tab to reference the CSV file since ASCII isn't going to change anytime soon.
  2. Create a list constant called, say, ASCII, that has all the strings you want to convert to and index the list by the int. For example, in a Map operator do Add c ASCII[i], and add some bounds checking logic (perhaps in a user function).

Disclosure/Disclaimer: I am an employee of TIBCO Software, Inc. Opinions expressed here are my own and not TIBCO's.

分享给朋友:
您可能感兴趣的文章:
随机阅读: