zoukankan      html  css  js  c++  java
  • truncate narrow string converted from unicode string

    Now we have one Unicode string, and we want to convert it to be narrow string which means multi byte string and truncate it to desired length.
    There is one key point: in Unicode string, two bytes represent one charactor; after it is converted into multi byte string, two or one bytes can represent one charactor. Wether the charactor can be displayed correctly without being '?', firstly the correct code page for non-Unicode program should be set; second we need to make sure the Unicode byte after truncating must be in a pair. Below function is for such purpose use.

    void truncateFileName(UTxString8& narrStrFileName, unsigned nDestiNum)
     {
      // No need to truncate file name.
      if(narrStrFileName.length() < nDestiNum)
       return;

      // Truncate file name to be desired length.
      narrStrFileName = narrStrFileName.substr(narrStrFileName.length()-nDestiNum, nDestiNum);

      // Truncate one more byte if finding the number of Unicode char byte is odd, which means
      // half of the original Unicode character has been truncated so we need to remove the other
      // half.
      //
      int nCharNum = narrStrFileName.length();
      int nUnicodeCharNum = 0;
      for (int i = 0; i != nCharNum; ++i)
      {
       char cChar = narrStrFileName.at(i);

       // Half of an Unicode character occupies one byte and it can always be translated to be
       // a negative integer.
       // Once reach to a ASCII char, stop the counting because left Unicode chars are definitely
       // pair by pair.
       if(cChar < 0)
        ++nUnicodeCharNum;
       else
        break;
      }

      if(nUnicodeCharNum%2 == 1) // odd number.
       narrStrFileName = narrStrFileName.substr(1, nCharNum-1);

     }

  • 相关阅读:
    Google maps not working IE11
    谷歌地图插件
    杨辉三角python的最佳实现方式,牛的不能再牛了
    今日头条推荐算法原理全文详解
    项目启动大会要点
    学金融应该看的书籍
    【从0到1】经典语录
    appium学习链接记录
    Axure-计算输入字数
    如何提升个人专业能力
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1078025.html
Copyright © 2011-2022 走看看