用官方模板运行,英文转中文正常,中文转英文返回错误信息“SYSTEM ERROR”,原因在于没有仔细阅读官方文档:
我们使用如下函数测试字符串是否为UTF-8编码:
bool is_str_utf8(const char* str)
{
unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
unsigned char chr = *str;
bool bAllAscii = true;
for (unsigned int i = 0; str[i] != '\0'; ++i) {
chr = *(str + i);
//判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
if (nBytes == 0 && (chr & 0x80) != 0) {
bAllAscii = false;
}
if (nBytes == 0) {
//如果不是ASCII码,应该是多字节符,计算字节数
if (chr >= 0x80) {
if (chr >= 0xFC && chr <= 0xFD) {
nBytes = 6;
}
else if (chr >= 0xF8) {
nBytes = 5;
}
else if (chr >= 0xF0) {
nBytes = 4;
}
else if (chr >= 0xE0) {
nBytes = 3;
}
else if (chr >= 0xC0) {
nBytes = 2;
}
else {
return false;
}
nBytes--;
}
}
else {
//多字节符的非首字节,应为 10xxxxxx
if ((chr & 0xC0) != 0x80) {
return false;
}
//减到为零为止
nBytes--;
}
}
//违返UTF8编码规则
if (nBytes != 0) {
return false;
}
if (bAllAscii) { //如果全部都是ASCII, 也是UTF8
return true;
}
return true;
}
使用如下函数将GBK转换为UTF-8:
string GbkToUtf8(const char* src_str)
{
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
string strTemp = str;
if (wstr) delete[] wstr;
if (str) delete[] str;
return strTemp;
}
再对源Demo进行一点修改:
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
#include "fun.h"
#include <iostream>
using namespace std;
int main(void)
{
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
char myurl[1000] = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
char* appid = (char*)"20210605000854454";
char* p = (char*)"かせい";
char* from = (char*)"auto";
char* to = (char*)"zh";
char salt[60];
int a = rand();
sprintf_s(salt, "%d", a);
char* secret_key = (char*)"***************";
char sign[120] = "";
string temp = GbkToUtf8(p);
char* q = (char*)temp.data();
//cout << is_str_utf8(q) << endl;
strcat_s(sign, appid);
strcat_s(sign, q);
strcat_s(sign, salt);
strcat_s(sign, secret_key);
printf("%s\n", sign);
unsigned char md[16];
int i;
char tmp[3] = { '\0' }, buf[33] = { '\0' };
MD5((unsigned char*)sign, strlen((char*)sign), md);
for (i = 0; i < 16; i++) {
sprintf_s(tmp, "%2.2x", md[i]);
strcat_s(buf, tmp);
}
printf("%s\n", buf);
strcat_s(myurl, "appid=");
strcat_s(myurl, appid);
strcat_s(myurl, "&q=");
strcat_s(myurl, q);
strcat_s(myurl, "&from=");
strcat_s(myurl, from);
strcat_s(myurl, "&to=");
strcat_s(myurl, to);
strcat_s(myurl, "&salt=");
strcat_s(myurl, salt);
strcat_s(myurl, "&sign=");
strcat_s(myurl, buf);
printf("%s\n", myurl);
//设置访问的地址
curl_easy_setopt(curl, CURLOPT_URL, myurl);
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
成功运行!