C_and_CPP 板


LINE

开发平台(Platform): (Ex: VC++, GCC, Linux, ...) Code Blocks C++ "A"这个字,在UTF8为一字节编码 16进位表示法为:41 10进位表示法为:65 "您"这个字,在UTF8为三字节编码 16进位表示法为:E6 82 A8 10进位表示法为:230 130 168 UTF8.txt内容为:您 \n A 我现在想将UTF8.txt内容转化成10进位表示法 #include <stdlib.h> #include <fstream> #include <iostream> #include <cstdlib> using namespace std; int main(void) { int x;int y; char txt[80]=""; ifstream ifile("C:\\Users\\Gon\\Desktop\\UTF8.txt",ios::binary); if(ifile.is_open()) { while(!ifile.eof()) { ifile >> txt; cout << txt<< endl; x=char (txt[0]); switch(x) { case 0-127: cout <<"1st byte~ " <<x << endl; break; case 240-247: cout <<"1st byte~ " <<x << endl; y=char (txt[1]); cout <<"2nd byte~ " <<y << endl; y=char (txt[2]); cout <<"3rd byte~ " <<y << endl; break; default: cout <<"1st byte~ " <<x << endl; y=char (txt[1]); cout <<"2nd byte~ " <<y << endl; y=char (txt[2]); cout << "3rd byte~ " <<y << endl; y=char (txt[3]); cout << "4th byte~ " <<y << endl; y=char (txt[4]); cout << "5th byte~ " <<y << endl; y=char (txt[5]); cout << "6th byte~ " <<y << endl; } } } else cout << "fail to open file" << endl; ifile.close(); // close file system("pause"); return 0; } 我想要得到的结果是: 您 1st byte~ 230 2nd byte~ 130 3rd byte~ 168 A 1st byte~ 65 可是实际跑出来的结果是: 您 1st byte~ -26 2nd byte~ -126 3rd byte~ -88 4th byte~ 0 5th byte~ 0 6th byte~ 0 A 1st byte~ 65 2nd byte~ 0 3rd byte~ -88 4th byte~ 0 5th byte~ 0 6th byte~ 0 几个问题点: 1. "A"的1st byte是65 应该代入case 0-127 可实际上却代入default case 为何? 2. "A"跑出来是单字节 数值65没错 "您"跑出来是三个字节 数值完全不对 请问修改法? 有请大大们帮忙指出问题所在 感谢!! --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.231.54.192
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1473664008.A.DD8.html
1F:推 yvb: x=char (txt[0]); 改为 x = (unsigned char) txt[0]; 09/12 15:15
x跟y都改了 数值都正确了 感谢!! 但强迫代入default case的问题还在
2F:→ PkmX: C和C++没有range case的写法啊... 09/12 15:21
我刚看洪维恩范例有耶 上面写 case 6 ... 8: cout << "夏天" << endl; //6~8月是夏天 现在我把我的 case 0-127 修改成 case 0 ... 127: case 240-247修改成 case 240 ... 247: "A"代入的case正确了 可是"您"还是代入错误的default case 问题还在 原来是我case写错了 应该是224 ... 239 抱歉!! 问题解决了
3F:→ PkmX: case m ... n: 是编译器的 extension 09/12 15:43
4F:→ Caesar08: 为啥default是印6 byte?然後又有印3 byte的UTF8? 09/12 16:30
5F:→ Caesar08: 另外,你的while与ifstream那样写,会出错喔 09/12 16:34
因为中文是3个byte 6个byte就顺便看第4,5,6个byte是否都是0 都是0表示UTF8档读取正确 ※ 编辑: ReiFu21 (61.231.54.192), 09/12/2016 17:37:14
6F:→ Caesar08: ㄜ,标准的UTF8是没有支援到6个byte的。另外,还是建议 09/12 19:15
7F:→ Caesar08: 2跟4 byte的都做一下处理比较好 09/12 19:15
8F:→ uranusjr: UTF-8 的 encoding 规则可以 encode 到 6 bytes, 但只会 09/13 19:28
9F:→ uranusjr: 用到 4-byte encoding 因为更後面的用不到了 09/13 19:28







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP