日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

將中文轉(zhuǎn)為unicode 及轉(zhuǎn)回中文函數(shù)

 duduwolf 2006-03-03
//轉(zhuǎn)為unicode
public static void writeUnicode(final DataOutputStream out, final String value)  {
  try {
  final String unicode = gbEncoding( value );
  final byte[] data = unicode.getBytes();
  final int dataLength = data.length;

  System.out.println( "Data Length is: " + dataLength );
  System.out.println( "Data is: " + value );
  out.writeInt( dataLength ); //先寫出字符串的長度
  out.write( data, 0, dataLength ); //然后寫出轉(zhuǎn)化后的字符串
  } catch (IOException e) {
 
  }
  }

 
  public static String gbEncoding( final String gbString ) {
  char[] utfBytes = gbString.toCharArray();
  String unicodeBytes = "";
  for( int byteIndex = 0; byteIndex < utfBytes.length; byteIndex ++ ) {
  String hexB = Integer.toHexString( utfBytes[ byteIndex ] );
  if( hexB.length() <= 2 ) {
  hexB = "00" + hexB;
  }
  unicodeBytes = unicodeBytes + "\\\\u" + hexB;
  }
  System.out.println( "unicodeBytes is: " + unicodeBytes );
  return unicodeBytes;
  }




/*****************************************************
  * 功能介紹:將unicode字符串轉(zhuǎn)為漢字
  * 輸入?yún)?shù):源unicode字符串
  * 輸出參數(shù):轉(zhuǎn)換后的字符串
  *****************************************************/
 private String decodeUnicode( final String dataStr ) {
  int start = 0;
  int end = 0;
  final StringBuffer buffer = new StringBuffer();
  while( start > -1 ) {
  end = dataStr.indexOf( "\\\\u", start + 2 );
  String charStr = "";
  if( end == -1 ) {
  charStr = dataStr.substring( start + 2, dataStr.length() );
  } else {
  charStr = dataStr.substring( start + 2, end);
  }
  char letter = (char) Integer.parseInt( charStr, 16 ); // 16進制parse整形字符串。
  buffer.append( new Character( letter ).toString() );
  start = end;
  }
  return buffer.toString();
 }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多