zoukankan      html  css  js  c++  java
  • HOW TO:在 Visual C++ .NET 中从 System::String* 转换为 Char*

    本文介绍使用 Visual C++ .NET 中的托管扩展从 System::String* 转换为 char* 的若干方法。 

    方法 1

    PtrToStringChars 指定了一个指向实际 String 对象的内部指针。如果将此指针传递给非托管函数调用,则必须先锁定该指针,以确保在进行异步垃圾回收过程中对象不会移动:
    //#include <vcclr.h>
    System::String * str = S"Hello world\n";
    const __wchar_t __pin * str1 = PtrToStringChars(str);
    wprintf(str1);	

    方法 2

    StringToHGlobalAnsi 将托管 String 对象的内容复制到本机堆,然后动态地将它转换为美国国家标准学会 (ANSI) 格式。此方法将分配所需的本机堆内存:
    //using namespace System::Runtime::InteropServices;
    System::String * str = S"Hello world\n";
    char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
    printf(str2);
    Marshal::FreeHGlobal(str2);

    方法 3

    VC7 CString 类有一个构造函数,它带有一个托管 String 指针并将其内容加载到 CString 中:
    //#include <atlstr.h>
    System::String * str = S"Hello world\n";
    CString str3(str);
    printf(str3);

    完整代码示例

    //compiler option:cl /clr
    #include <vcclr.h>
    #include <atlstr.h>
    #include <stdio.h>
    #using <mscorlib.dll>
    using namespace System;
    using namespace System::Runtime::InteropServices;
    int _tmain(void)
    {
    System::String * str = S"Hello world\n";
    //method 1
    const __wchar_t __pin * str1 = PtrToStringChars(str);
    wprintf(str1);
    //method 2
    char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(str);
    printf(str2);
    Marshal::FreeHGlobal(str2);
    //method 3
    CString str3(str);
    printf(str3);
    return 0;
    }

    System.String to float

     代码

            public void ConvertStringFloat(string stringVal) {
                
    float floatVal = 0;
                
                
    try {
                    floatVal = System.Convert.ToSingle(stringVal);
                    System.Console.WriteLine(
                        
    "The string as a float is {0}.", floatVal);
                } 
                
    catch (System.OverflowException){
                    System.Console.WriteLine(
                        
    "The conversion from string-to-float overflowed.");
                }
                
    catch (System.FormatException) {
                    System.Console.WriteLine(
                        
    "The string is not formatted as a float.");
                }
                
    catch (System.ArgumentNullException) {
                    System.Console.WriteLine(
                        
    "The string is null.");
                }

                
    // Float to string conversion will not overflow.
                stringVal = System.Convert.ToString(floatVal);
                System.Console.WriteLine(
                    
    "The float as a string is {0}.", stringVal);
            }
  • 相关阅读:
    STM32的“外部中断”和“事件”区别和理解
    非线性函数的最小二乘拟合——兼论Jupyter notebook中使用公式 [原创]
    Jupyter 快捷键总结
    自制导纳信号发生器 [原创cnblogs.com/helesheng]
    Verilog HDL数组(存储器)操作
    一个有趣的异步时序逻辑电路设计实例 ——MFM调制模块设计笔记
    用NI的数据采集卡实现简单电子测试之6——数字I/O及测试平台
    用NI的数据采集卡实现简单电子测试之5——压控振荡器的测试
    用NI的数据采集卡实现简单电子测试之4——半导体温度传感器
    黑客用我们服务器挖矿了
  • 原文地址:https://www.cnblogs.com/lai3d/p/1792859.html
Copyright © 2011-2022 走看看