字符串十进制转换为ASCII II Visual C ++ [英] String Decimal to ASCII II Visual C++

查看:117
本文介绍了字符串十进制转换为ASCII II Visual C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码,通过此代码,我得到的输出为十进制.我想做的是用ASCII输出.不确定如何在Visual C ++中实现它

I am using the code below, with this code I am getting an output in decimal. What I would like to do is output this in ASCII. Not sure how to implement that in visual c++

array<Byte>^ loc= gcnew array<Byte>(18);
            serialPort1->Read(loc,0,18);
            this->richTextBox3->AppendText(String::Format("{0}{1}{2}{3}{4}{5}{6}{7}",loc[9],loc[10],loc[11],loc[12],loc[14],loc[15],loc[16],loc[17])) ;

推荐答案

在您的值上使用Convert.ToChar,或使用格式说明符. 此处 [
Use Convert.ToChar on your values, or use a format specifier. Here,[^] they are the same for all .NET languages.


    array<Byte>^ loc= gcnew array<Byte>(18);
    int byteRead = serialPort1->Read(loc,0,18);

    StringBuilder builder;
    for (int i = 0; i != byteRead; ++)
    {
        // You must convert the "byte" into a "Char" to display the
        // character instead of its value (i.e. "A" instead of 65).
        Char theChar = loc[i];
        builder.Append(theChar);

        // The following line would also works (instead of the above 2).
        // builder.Append(static_cast<char>(loc[i]));
    }
    this->richTextBox3->AppendText(builder.ToString());
</char>


忘记ASCII ; .NET内部支持Unicode UTF-16.如果将.NET字符串写入文件,则可以使用ASCII,当然,如果使用的所有代码点均限制为0..127.在这种情况下,如果使用不带BOM的UTF-8,则输出将相同.但是您并没有询问文件/流.

参见:
http://en.wikipedia.org/wiki/ASCII [ http://en.wikipedia.org/wiki/Unicode [ http://unicode.org/ [ ^ ],
http://unicode.org/faq/utf_bom.html [
Forget about ASCII; .NET internally supports Unicode UTF-16. If you write .NET string to a file, you can use ASCII, of course, if all code points used are limited to 0..127. In this case, if you use UTF-8 without BOM, the output will be the same. But you''re not asking about file/stream.

See:
http://en.wikipedia.org/wiki/ASCII[^],
http://en.wikipedia.org/wiki/Unicode[^],
http://unicode.org/[^],
http://unicode.org/faq/utf_bom.html[^].

—SA


这篇关于字符串十进制转换为ASCII II Visual C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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