Skip to main content
Question

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

  • April 5, 2016
  • 5 replies
  • 74 views

fmelizard
Safer
Forum|alt.badge.img+22
How to replace characters(like A-Z) in a string with their corresponding ASCII codes(like 65-90) in bulk?
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

5 replies

takashi
Celebrity
  • April 6, 2016

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 -> ??


  • April 6, 2016

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

,

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


  • April 6, 2016

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


takashi
Celebrity
  • April 6, 2016

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 {}]
}

  • April 8, 2016

It works. Thanks a lot.