找回密码
 register

QQ登录

只需一步,快速开始

查看: 88|回复: 0

[游戏教程] LUA程序编码批量转换程序utf-8转gbk

[复制链接]

[游戏教程] LUA程序编码批量转换程序utf-8转gbk

[复制链接]
  • 打卡等级:热心大叔
  • 打卡总天数:94
  • 打卡月天数:17
  • 打卡总奖励:94
  • 最近打卡:2025-01-18 01:28:20
Waylee

主题

0

回帖

1万

积分

仙帝

积分
12364
Waylee 2023-6-3 11:47 | 显示全部楼层 |阅读模式

马上注册,查看网站隐藏内容!!

您需要 登录 才可以下载或查看,没有账号?register

×
文章编辑人员:雪舞
内容测试时间:2023-6-3 11:46:31
测试环境:Windows 10

目的将当前目录所有的lua程序转换为gbk格式,原来的文件则备份为*.bak
#include <windows.h>
#include <stdio.h>

void Utf8ToGbk(char* utf8, int len_utf8, char* gbk, int len_gbk) {
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    wchar_t* wszGBK = (wchar_t*)malloc(sizeof(wchar_t) * len);
    memset(wszGBK, 0, len * sizeof(wchar_t));
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wszGBK, len);

    len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
    WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, gbk, len, NULL, NULL);
    
    if (wszGBK) {
        free(wszGBK);
    }
}

void convert_file(const char* filename) {
    // Open file in binary mode, read into buffer
    FILE* file = fopen(filename, "rb");
    fseek(file, 0, SEEK_END);
    long fsize = ftell(file);
    fseek(file, 0, SEEK_SET); 

    char* string = (char*)malloc(fsize + 1);
    fread(string, fsize, 1, file);
    fclose(file);
    string[fsize] = 0;

    // Convert buffer from UTF-8 to GBK
    char* gbk = (char*)malloc(fsize * 2 + 1);  // allocate enough space
    Utf8ToGbk(string, fsize + 1, gbk, fsize * 2 + 1);

    // Check if a backup already exists, if not, create one
    char backupFileName[260];
    snprintf(backupFileName, sizeof(backupFileName), "%s.bak", filename);
    if (fopen(backupFileName, "r") == NULL) {
        rename(filename, backupFileName);
    }

    // Write the converted buffer into original file
    file = fopen(filename, "wb");
    fwrite(gbk, strlen(gbk), 1, file);
    fclose(file);

    printf("已修复的文件:%s\n", filename);

    free(string);
    free(gbk);
}

int main() {
    WIN32_FIND_DATAA findData;
    HANDLE hFind = FindFirstFileA(".\\*.lua", &findData);

    if (hFind == INVALID_HANDLE_VALUE) {
        printf("FindFirstFile failed (%d)\n", GetLastError());
        return -1;
    }

    do {
        if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
            printf("%s\n", findData.cFileName);
            convert_file(findData.cFileName);
        }
    } while (FindNextFileA(hFind, &findData) != 0);

    FindClose(hFind);

    printf("按回车键退出...");
    getchar();

    return 0;
}


您需要登录后才可以回帖 登录 | register

本版积分规则

雪舞知识库 | 浙ICP备15015590号-1 | 萌ICP备20232229号|浙公网安备33048102000118号 |天天打卡

GMT+8, 2025-1-20 02:01 , Processed in 0.104306 second(s), 7 queries , Redis On.

Powered by XueWu Licensed

Copyright © Tencent Cloud.

快速回复 返回顶部 返回列表