托管c ++ / cli .net将固定字节数组转换为字符串^ [英] Managed c++/cli .net convert fixed Byte array to a String^

查看:94
本文介绍了托管c ++ / cli .net将固定字节数组转换为字符串^的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在托管c ++ / cli中将固定字节数组转换为字符串?

例如,我有以下字节数组。

How do I convert a fixed byte array to a String in managed c++/cli ?
For example I have the following Byte array.

Byte byte_data[5];
byte_data[0]='a';
byte_data[1]='b';
byte_data[2]='c';
byte_data[3]='d';
byte_data[4]='e';

我尝试了以下代码

String ^ mytext = System :: Text :: UTF8Encoding :: UTF8-> GetString(byte_data);

我遇到以下错误:

错误C2664:'System :: String ^ System :: Text :: Encoding :: GetString(cli :: array< Type,dimension> ^)':无法将参数1从' unsigned char [5]'改为'cli :: array< Type,dimension> ^'

推荐答案

使用一些在指向有符号和无符号类型的指针之间进行强制转换的知识来武装自己,然后应该设置为使用 String :: String(SByte *,Int32,Int32) 。还可能需要阅读页面上的备注,特别是围绕编码。

Arm yourself with some knowledge about casting between pointers to signed and unsigned types and then you should be set to use String::String(SByte*, Int32, Int32). It might also pay to read the Remarks on the page, specifically around encoding.

我从此处的页面复制了示例:

I've reproduced the sample from the page here:

// Null terminated ASCII characters in a simple char array 
char charArray3[4] = {0x41,0x42,0x43,0x00};
char * pstr3 =  &charArray3[ 0 ];
String^ szAsciiUpper = gcnew String( pstr3 );
char charArray4[4] = {0x61,0x62,0x63,0x00};
char * pstr4 =  &charArray4[ 0 ];
String^ szAsciiLower = gcnew String( pstr4,0,sizeof(charArray4) );

// Prints "ABC abc"
Console::WriteLine( String::Concat( szAsciiUpper,  " ", szAsciiLower ) );

// Compare Strings - the result is true
Console::WriteLine( String::Concat(  "The Strings are equal when capitalized ? ", (0 == String::Compare( szAsciiUpper->ToUpper(), szAsciiLower->ToUpper() ) ? (String^)"TRUE" :  "FALSE") ) );

// This is the effective equivalent of another Compare method, which ignores case
Console::WriteLine( String::Concat(  "The Strings are equal when capitalized ? ", (0 == String::Compare( szAsciiUpper, szAsciiLower, true ) ? (String^)"TRUE" :  "FALSE") ) );

这篇关于托管c ++ / cli .net将固定字节数组转换为字符串^的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆