Alex
Alex

Reputation: 13

Convert a string with hex UTF codes to characters using busybox

I have a string with a file path, where non-ASCII characters are HEX-encoded.

For instance:

/media/\x444\x430\x439\x43b.txt

and I need to convert it to /media/файл.txt using busybox.

I can do this task using ruby:

n = '/media/\x444\x430\x439\x43b.txt'
n.gsub(/\\x\h{3}/){|h| [h[2..-1].to_i(16)].pack('U') }

How to perform this on busybox (version 1.19.4)?

Upvotes: 1

Views: 62

Answers (1)

KamilCuk
KamilCuk

Reputation: 140880

How to perform this on busybox (version 1.19.4)?

Redirect the input to awk. Set field separator to nothing.

In awk write a loop to iterate from 1 to NF, for every char, extract the i-th character from input.

If the i-th character is not a /, printf it.

Manage state of the parser. If the i-th character is a /, remember it in a variable. The next 3 characters are decimals, which have to be converted from string into a number and accumulated in another variable.

After converting the 3 characters to decimals, you'll get a UTF-16 characters. Now you have to write a function to convert UTF-16 to UTF-8. After conversion, output the utf-8 characters.

Upvotes: 0

Related Questions