首页 > 解决方案 > 在 C++ 中为 Android 应用程序编码 ASCII

问题描述

我有一些在我的 android 应用程序中使用的 C++ 代码。它接受一个字符串并将每个字符转换为这个ascii 表中的一个 int 。然后我基于此渲染正确的字形。它适用于我测试了一些不同的谷歌像素和三星手机的大多数设备,但是当我在 api 24 - 30 上的 3.3" WQVGA 模拟器中测试时,它无法正常工作。

我想我在这一切都错了。有没有更好的方法来做到这一点?我的代码有修复吗?

谢谢你的时间。

void text::drawtexthorizontal( double x, double y, int textheight, string textin)
{
  int n = textin.length(); 
  // declaring character array 
  char char_array[n + 1]; 
  // copying the contents of the string to char array 
  strcpy(char_array, textin.c_str()); 
  for (int i = 0; i < n; i++){ 
    //get ascii or "extended" ascii index and store it in symbol
    int symbol;
    if( (char_array[i] & ( 1 << 7 )) >> 7 == 1){
      if( (char_array[i] & ( 1 << 6 )) >> 6 == 1){
        //comb takes 2 byte representation and returns an int between 128 and 255 corresponding to extended ascii
        symbol = comb(int(char_array[i]),int(char_array[i+1]));
        i++;
      }
      else{
        symbol = comb(int(char_array[i+1]),int(char_array[i]));
        i++;
      }
    } 
    else symbol = int(char_array[i]);

    //render chars[symbol]
  }
}


int text::comb( int n, int m){
/*https://naveenr.net/unicode-character-set-and-utf-8-utf-16-utf-32-encoding/ for 2 byte encoding*/
  int a[8] = { 0 },b[8] = { 0 };;
  int i,k=0; 
  for (i = 0; n > 0; i++) { 
     a[i] = n % 2; 
     n /= 2; 
  }

  for (i = 0; m > 0; i++){ 
     b[i] = m % 2; 
     m /= 2; 
  }

  for (i = 4; i >=0 ; i--){
    k = 10 * k + a[i];
  }

  for (i = 5; i >=0 ; i--){
    k = 10 * k + b[i];
  }

  int dec_value = 0; 
// Initializing base value to 1, i.e 2^0 
  int base = 1; 
  int temp = k; 
  while (temp) { 
    int last_digit = temp % 10; 
    temp = temp / 10; 
    dec_value += last_digit * base; 
    base = base * 2; 
  } 
  
  if(dec_value > 255) dec_value = 32;
  return dec_value; 

}

标签: androidc++asciiextended-ascii

解决方案


推荐阅读