Qt_IM_26 - 8BitsCoding/RobotMentor GitHub Wiki
์ฐ์ ์ ๋์ฝ๋(Unicode)๋ฅผ ๋จผ์ ์์์ผ ํ๋ค.
์ ๋์ฝ๋๋ ์ธ์ฝ๋ฉ์ ๊ฐ๋ ์ด ์๋๋ผ ๋ฌธ์์ด์ ๋งคํ์ด๋ค.
์๋ฅผ๋ค์ด A = U+0041, ๊ฐ = U+AC00์ผ๋ก ๋งคํ๋์ด ์๋ค.
์ด ๋งคํ ๋ฐฉ๋ฒ ์ค ๋ช๊ฐ์ง๊ฐ UTF-8, UTF-16 ... ๋ฑ์ด ์๋ค.
ASCII ์ฝ๋ ๊ธฐ๋ฐ์ ๋งคํ ๋ฐฉ๋ฒ ์คํ๋
์์ด์ ๊ฒฝ์ฐ ๊ทธ๋๋ก ASCII ์ฝ๋๋ฅผ ์ฌ์ฉํ๊ณ
ํ๊ธ์ ๊ฒฝ์ฐ 3๋ฐ์ดํธ๋ก ๋งคํ์ด ๋์๋๋ฐ ์ธ์ด์ ๋ฐ๋ผ 1~4๋ฐ์ดํธ๋ฅผ ์ฌ์ฉํ๊ธฐ์ UTF์ ๊ฐ์ ๋งคํ ๋ฐฉ์์ ๊ฐ๋ณ ํ๊ธฐ ์ธ์ฝ๋ฉ ๋ฐฉ์์ด๋ผ ํ๋ค.
์๋ฅผ ๋ค์ด '๊ฐ'๋ฅผ ํํํ๊ณ ์ถ๋ค๋ฉด ํ๊ธ์ 3๋ฐ์ดํธ๊ฐ ํ ๋น๋์๊ธฐ์ 1110xxxx 10xxxxxx 10xxxxxx์ x์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ผ๋ฉด๋๋ค.
'๊ฐ' = U+AC00 = 0xAC00 = 44,032 = 10101100 00000000 ์ ์ ๋ฐ์ดํฐ์ ์์๋๋ก ๋ฃ์ผ๋ฉด ๋๋ค.
#include <QtCore/QCoreApplication>
#include <qdebug.h>
#include <qfile.h>
#include <qdir.h>
#include <qtextstream.h>
#include <qrandom.h>
/*
UTF-8 is a variable width character encoding capable of encoding all 1,112,064 valid code points in
Unicode using one to four 8-bits bytes.
The encoding is defined by Unicode standard, and was originally designed by Ken Thopson and Rob Pike.
Called "Unicode"
*/
QString makeData() {
QString data;
data.append("Unicode test\r\n");
for (int i = 0; i < 10; i++) {
int number = QRandomGenerator::global()->bounded(1112064);
data.append(number);
}
data.append(10); // \r
data.append(13); // \n
// data.append("\r\n");
return data;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString data = makeData();
QString path = QDir::currentPath() + QDir::separator() + "data.txt";
QFile file(path);
if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream.setCodec("UTF-8");
stream << data;
stream.flush();
file.close();
}
qInfo() << "Done";
qInfo() << "Unicode:" << data;
qInfo("");
qInfo() << "ASCII" << data.toLatin1();
return a.exec();
}
Unicode์ ๊ฒฝ์ฐ ์ฝ์ ํ ๋๋ค๋๋ฒ๋ฅผ ์ถ๋ ฅํ๋ ๋ฐ๋ฉด
ASCII์ ๊ฒฝ์ฐ ๋๋ค๋ค๋ฒ๋ฅผ ์ถ๋ ฅํ์ง ๋ชปํ๋ค.(๋ฒ์๋ฅผ ๋ฒ์ด๋๊ธฐ ๋๋ฌธ)