Question

How to replace characters(like A-Z) in a string with their corresponding ASCII codes(like 65-90) in bulk?


Userlevel 4
Badge +13
How to replace characters(like A-Z) in a string with their corresponding ASCII codes(like 65-90) in bulk?

5 replies

Userlevel 2
Badge +17

Hi @FMEZipster, you can use the TextEncoder (Encoding Type: HEX) to translate every character within a string to ASCII code with hexadecimal representation.

If you need to convert the result to decimal representation and/or comma-separated format, need some additional transformers or other approach. What representation do you need finally? e.g. ABC -> ??

Decimal representation(e.g. ABC ->656667)

,

Decimal representation?e.g. ABC ->656667?

Decimal representation(e.g. ABC ->656667)

Userlevel 2
Badge +17

This is a possible way - split the string into individual characters, extract their ASCII code (CharacterCodeExtractor), then concatenate them (Aggregator).

0684Q00000ArLeeQAF.png

... scripting might be easier in this case.

PythonCaller script example:

def extractAsciiCode(feature):
    codes = [str(ord(c)) for c in feature.getAttribute('_src')]
    feature.setAttribute('_codes', ''.join(codes))

TclCaller script example:

proc extractAsciiCode {} {
    set codes {}
    foreach ch [split [FME_GetAttribute "_src"] {}] {
        lappend codes [scan $ch %c]
    }
    return [join $codes {}]
}

It works. Thanks a lot.

Reply