-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_iso_2_utf.cpp
68 lines (65 loc) · 2.66 KB
/
convert_iso_2_utf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Gustavo Saracca.
2024-06-07
v 0.1
Tested and supplied without any warranty. Use at your own risk.
*/
// ------------------------------------------------------------------------------------------------
// Función para reemplazar un carácter por otro en una cadena
// ------------------------------------------------------------------------------------------------
extern "C" void replaceChar( char *str, char oldChar, char newChar) {
while (*str) {
if (*str == oldChar) {
*str = newChar;
}
str++;
}
}
// ------------------------------------------------------------------------------------------------
// Función para convertir texto de ISO-8859-1 a UTF-8
// ------------------------------------------------------------------------------------------------
extern "C" void convertISO88591ToUTF8(const char *input, char *output) {
while (*input) {
unsigned char c = *input++;
if (c < 0x80) {
*output++ = c; // ASCII characters map directly
} else {
*output++ = 0xC0 | (c >> 6);
*output++ = 0x80 | (c & 0x3F);
}
}
*output = '\0'; // Null-terminate the output string
}
// ------------------------------------------------------------------------------------------------
// Función para convertir texto de UTF-8 a ISO-8859-1
// ------------------------------------------------------------------------------------------------
extern "C" void convertUTF8ToISO88591(const char *input, char *output) {
while (*input) {
unsigned char c = *input++;
if (c < 0x80) {
*output++ = c; // ASCII characters map directly
} else if ((c & 0xE0) == 0xC0) {
// Check for valid 2-byte sequence
unsigned char c2 = *input++;
if ((c2 & 0xC0) == 0x80) {
unsigned char finalChar = ((c & 0x1F) << 6) | (c2 & 0x3F);
if (finalChar >= 0xA0) {
*output++ = finalChar;
} else {
// Invalid character for ISO-8859-1
// You might want to handle this case differently
*output++ = '?';
}
} else {
// Invalid UTF-8 sequence
// You might want to handle this case differently
*output++ = '?';
}
} else {
// Invalid UTF-8 sequence or character not representable in ISO-8859-1
// You might want to handle this case differently
*output++ = '?';
}
}
*output = '\0'; // Null-terminate the output string
}