Jump to content

(Help) How to convert file UTF-8 to UTF-16 LE BOM


Recommended Posts

Posted (edited)

 

I have a lot of "utf-8" files and want to convert them to "utf-16 le bom" quickly. Is there a script that can do it?

 

147.png

1478.png

Edited by vonzyy
  • vonzyy changed the title to (Help) How to convert file UTF-8 to UTF-16 LE BOM
Posted
On 4/4/2025 at 4:43 PM, CaptJack said:

Try spare into 2 files more than one. 

Atleast don't sell the machine translation

No, mate. I'm not selling it.

  • Moderators
Posted

This Python script should do the trick:

 

import os


def convert_to_utf16(input_path: str, output_path: str) -> None:
    print("Starting convert...")
    with open(input_path, 'rt', encoding='utf-8') as f_in:
        content = f_in.read()
    with open(output_path, 'wb') as f_out:
        f_out.write(b'\xff\xfe')
        f_out.write(content.encode('utf-16-le'))
    print("Convert success!")


def convert_all_txt_in_folder(input_folder: str, output_folder: str) -> None:
    os.makedirs(output_folder, exist_ok=True)

    for filename in os.listdir(input_folder):
        if filename.lower().endswith('.txt'):
            input_path = os.path.join(input_folder, filename)
            base_name = os.path.splitext(filename)[0]
            output_filename = f"{base_name}_utf16le.txt"
            output_path = os.path.join(output_folder, output_filename)
            convert_to_utf16(input_path, output_path)
            print(f'Converted: {filename} -> {output_filename}')


if __name__ == '__main__':
    convert_all_txt_in_folder('C:\\Users\\User\\Desktop\\UTF_FILES\\input',
                              'C:\\Users\\User\\Desktop\\UTF_FILES\\output')

 

  • Like 1
Posted
10 hours ago, ikskoks said:

This Python script should do the trick:

 

import os


def convert_to_utf16(input_path: str, output_path: str) -> None:
    print("Starting convert...")
    with open(input_path, 'rt', encoding='utf-8') as f_in:
        content = f_in.read()
    with open(output_path, 'wb') as f_out:
        f_out.write(b'\xff\xfe')
        f_out.write(content.encode('utf-16-le'))
    print("Convert success!")


def convert_all_txt_in_folder(input_folder: str, output_folder: str) -> None:
    os.makedirs(output_folder, exist_ok=True)

    for filename in os.listdir(input_folder):
        if filename.lower().endswith('.txt'):
            input_path = os.path.join(input_folder, filename)
            base_name = os.path.splitext(filename)[0]
            output_filename = f"{base_name}_utf16le.txt"
            output_path = os.path.join(output_folder, output_filename)
            convert_to_utf16(input_path, output_path)
            print(f'Converted: {filename} -> {output_filename}')


if __name__ == '__main__':
    convert_all_txt_in_folder('C:\\Users\\User\\Desktop\\UTF_FILES\\input',
                              'C:\\Users\\User\\Desktop\\UTF_FILES\\output')

 

Sorry, how to use the script. I have tried to put the script into python but the script still doesn't work.

Posted
On 5/6/2025 at 7:24 AM, vonzyy said:

Sorry, how to use the script. I have tried to put the script into python but the script still doesn't work.

On Windows you just double click it like normal .exe file

Posted
23 hours ago, Sparagas said:

On Windows you just double click it like normal .exe file

I have done it but the script still doesn't work

  • Moderators
Posted

You should adjust your path. Just replace "User" with "MSI" and point to the directory that actually exist on your Desktop 😄 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...