Help converting Hex UTF-8 bytes to character

Trevor DeVore lists at mangomultimedia.com
Fri Jun 1 00:18:02 EDT 2018


On Thu, May 31, 2018 at 5:20 PM, Monte Goulding via use-livecode <
use-livecode at lists.runrev.com> wrote:

>
> I’m pretty sure that the following will do what you want here:
>
> textDecode(format(<file content>),”utf-8”)
>

Yes it does! `format` is my new best friend.

Thanks for everyone’s tips. Here is what I came up with which has worked on
the four files I’ve thrown at it.

```
on mouseUp
  answer file "Select UTF-8 File";
  put url("binfile:" & it) into tData
  put 0 into tSkip

  repeat forever
    # Find next occurance of \x
    put offset("\x", tData, tSkip) into tStartOffset
    if tStartOffset > 0 then
      add tSkip to tStartOffset
      put tStartOffset + 3 into tEndOffset

      # Find all repeating \x instances
      repeat forever
        if char (tEndOffset + 1) to (tEndOffset + 2) of tData is "\x" then
          add 4 to tEndOffset
        else
          exit repeat
        end if
      end repeat

      try
        # Now format them as normal characters
        put format(char tStartOffset to tEndOffset of tData) into tNewString
        put tNewString into char tStartOffset to tEndOffset of tData
        add the number of chars of tNewString to tSkip
      catch e
        breakpoint
        # Skip over this \x instance as format couldn't handle it
        add 2 to tSkip
        next repeat
      end try
    else
      exit repeat
    end if
  end repeat

  set the clipboarddata to textDecode(tData, "utf8")
  beep
end mouseUp

```

-- 
Trevor DeVore
ScreenSteps
www.screensteps.com



More information about the Use-livecode mailing list