关于Variant的类型转换问题~~~~~~在线等

YJS050320001 2006-07-17 09:36:31
我在开发一个程序时 使用了VB编写的一控件,其中在VB中他是这么定义这个数据格式的:
Private Sub D_ReceiveData(ByVal User_Id As String, ByVal User_GetDataASC As Variant, ByVal User_GetDataHEX As Variant)
当我把该控件加入BCB后是这样定义的:
void __fastcall TForm1::D_ReceiveData(TObject *Sender, BSTR User_Id,
Variant User_GetDataASC, Variant User_GetDataHEX)
我想得到User_GetDataASC的数据并将他转化为String类型的数据,如何解决呢?
十分感谢!!!!
...全文
962 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
laowang2 2007-03-26
  • 打赏
  • 举报
回复
up
YJS050320001 2006-07-18
  • 打赏
  • 举报
回复
谢谢各位大哥
待小弟调试一下~~~~ ^_^
我不懂电脑 2006-07-18
  • 打赏
  • 举报
回复
CheckType(vBoolean, varBoolean);
CheckType(vByte, varByte);
CheckType(vString, varString);
CheckType(vOleStr, varOleStr);
CheckType(vDispatch, varDispatch);
CheckType(vUnknown, varUnknown);

cout << endl << "TestAssignments (by ref) output:" << endl;
CheckType(vrSmallint, varSmallint | varByRef);

CheckType(vrInteger, varInteger | varByRef);
CheckType(vrSingle, varSingle | varByRef);
CheckType(vrDouble, varDouble | varByRef);
CheckType(vrCurrency, varCurrency | varByRef);
CheckType(vrDate, varDate | varByRef);
CheckType(vrBoolean, varBoolean | varByRef);
CheckType(vrByte, varByte | varByRef);
CheckType(vrOleStr, varOleStr | varByRef);

SysFreeString(oleStr);
}

//--------------------------------------------------------------------
void TestTypeChanging()

{
Variant vSmallint = 3;
Variant vx, vy;

cout << endl << "TestTypeChanging output:" << endl;

vx = vSmallint.AsType(varString);
CheckType(vx, varString);

vx.ChangeType(varInteger);
CheckType(vx, varInteger);

vy = vx.AsType(varSingle);
CheckType(vy, varSingle);
CheckType(vx, varInteger);

vx.Clear();
CheckType(vx, varEmpty);
}

//--------------------------------------------------------------------
void TestOperators()
{
Variant vx, vy;

cout << endl <<"TestOperators output:" << endl;

vx = 5;
vy = vx + 0;
Compare(vy, vx);

Compare(vx-0, vx);
Compare(vx*1, vx);
Compare(vx/1, vx);

vx = 5;
vy = vx + 0;
Compare(vy, vx);

Compare(vx-0, vx);
Compare(vx*1, vx);
Compare(vx/1, vx);

vy = vx;
Compare(vy+=0, vx);
Compare(vy-=0, vx);
Compare(vy*=1, vx);
Compare(vy/=1, vx);

vx = 5;
vy = 46;
vx = 2*vx + (vy-6)/2;
Compare(vx, 30);

vx = "hi ";

vy = "there";
vx += vy;
Compare(vx, "hi there");

//force a variant so we can test variant's operator+
vx = 1 + 20.0 + Variant(String("300"));
Compare(vx, 321);

vx = 15;
vy = (vx == 15);
Compare(vy, true);

vy = (vx < 15);
Compare(vy, false);

vy = (vx > 15);
Compare(vy, false);

vy = (vx != 15.3);
Compare(vy, true);
}

//--------------------------------------------------------------------
void TestArrays()
{
Variant a, b;

int i, tmp;

cout << endl <<"TestArrays output:" << endl;

a = VarArrayCreate(OPENARRAY(int, (0, 9)), varInteger);
b = VarArrayCreate(OPENARRAY(int, (1, 3, 0, 9)), varInteger);

// Do it with array lock
int* ax = (int*) a.ArrayLock();
for (i=0; i < 10; i++)
ax[i] = i*i;

Compare(ax[0], 0);
Compare(ax[9], 81);

a.ArrayUnlock();

//Do it again with Put/GetElement
for (i=0; i < 10; i++)
{
tmp = i*i*i;
a.PutElement(&tmp, i);

}
Compare(a.GetElement(0), 0);
Compare(a.GetElement(9), 9*9*9);

// Try a 2-dimensional array
for (i=0; i < 10; i++)
{
b.PutElement(&i, 1, i);
tmp = sqrt(i);
b.PutElement(&tmp, 2, i);
tmp = i*i;
b.PutElement(&tmp, 3, i);
}

Compare(b.GetElement(1,0), 0);
Compare(b.GetElement(1,9), 9);

Compare(b.GetElement(2, 0), 0);
Compare(b.GetElement(2, 9), 3);

Compare(b.GetElement(3, 0), 0);
Compare(b.GetElement(3, 9), 81);

}

//--------------------------------------------------------------------
void TestOle()
{
Variant wordBasic, res;
Function fileNew("FileNew");
Procedure insert("Insert");
Function fileSaveAs("FileSaveAs");

cout << endl << "TestOle: check contents of test.txt" << endl;
wordBasic = Variant::CreateObject("Word.Basic");
res = wordBasic.Exec(fileNew <<"Normal");

wordBasic.Exec(insert <<"This is the first line\n");
wordBasic.Exec(insert.ClearArgs() <<"This is the second line\n");

res = wordBasic.Exec(fileSaveAs <<"test.txt" <<3);
}
我不懂电脑 2006-07-18
  • 打赏
  • 举报
回复
#include <vcl.h>

#pragma hdrstop
#include <iostream.h>
#include <oleauto.hpp>
#include <math.h>

//--------------------------------------------------------------------
void TestConstructors();
void TestAssignments();
void TestTypeChanging();
void TestOperators();
void TestArrays();
void TestOle();

//--------------------------------------------------------------------
main()
{
(*(void(*)(void))InitProc)();
TestConstructors();
TestAssignments();
TestTypeChanging();

TestOperators();

TestArrays();
TestOle();

return 0;
}

//--------------------------------------------------------------------
void CheckType(const Variant& v, Word type)
{
if (v.VType == type)
cout << "Type Check OK: ";
else cout << "Type Check ERROR: ";
cout << "type: " << v.VType;
cout << ", expected: " << type << endl;
}

//--------------------------------------------------------------------
void Compare(Variant &lhs, Variant &rhs)

{
if (lhs == rhs)

cout << "Compare OK: ";
else
cout << "Compare ERROR: ";

cout << "lhs type: " << lhs.VType;
cout << ", rhs type: " << rhs.VType;
cout << ", lhs val: " << String(lhs).c_str();
cout << ", rhs val: " << String(rhs).c_str() << endl;
}

//--------------------------------------------------------------------
void TestConstructors()
{
static short smallint = 3;
static int integer = 4;
static float flt = 5;
static double dbl = 6;

static Currency currency = 7;

static TDateTime date = String("05/11/95");
static WordBool boolean = true;
static Byte byte = 9;
static wchar_t* oleStr = SysAllocString(L"Hey, welcome to the world of variant and OLE");

Variant vEmpty;

//can't actually construct a null
Variant vNull;
vNull.ChangeType(varNull);

// by value
Variant vSmallint(short(3));
Variant vInteger(4);
Variant vSingle(float(5));
Variant vDouble(double(6));

Variant vCurrency(Currency(7));

Variant vDate(TDateTime(String("05/11/95")));
Variant vBoolean(true);
Variant vByte((Byte)9);
Variant vString(String("This is an ANSI string, buddy!"));
Variant vOleStr(L"Hey, welcome to the world of Variants & OLE Automation");
Variant vDispatch(Variant::CreateObject("Word.Basic"));
Variant vUnknown(Variant::CreateObject("Word.Basic").AsType(varUnknown));

// by reference
Variant vrSmallint = &smallint;
Variant vrInteger = &integer;

Variant vrSingle = &flt;

Variant vrDouble = &dbl;
Variant vrCurrency = ¤cy;
Variant vrDate = &date;
Variant vrBoolean = &boolean;
Variant vrByte = &byte;
Variant vrOleStr = &oleStr;

cout << endl << "TestConstructors output:" << endl;
CheckType(vEmpty, varEmpty);
CheckType(vNull, varNull);

cout << endl << "TestConstructors(byval) output:" << endl;
CheckType(vSmallint, varSmallint);
CheckType(vInteger, varInteger);
CheckType(vSingle, varSingle);

CheckType(vDouble, varDouble);

CheckType(vCurrency, varCurrency);
CheckType(vDate, varDate);
CheckType(vBoolean, varBoolean);
CheckType(vByte, varByte);
CheckType(vString, varString);
CheckType(vOleStr, varOleStr);
CheckType(vDispatch, varDispatch);
CheckType(vUnknown, varUnknown);

cout << endl << "TestConstructors(byref) output:" << endl;
CheckType(vrSmallint, varSmallint | varByRef);
CheckType(vrInteger, varInteger | varByRef);
CheckType(vrSingle, varSingle | varByRef);

CheckType(vrDouble, varDouble | varByRef);
CheckType(vrCurrency, varCurrency | varByRef);
CheckType(vrDate, varDate | varByRef);
CheckType(vrBoolean, varBoolean | varByRef);
CheckType(vrByte, varByte | varByRef);
CheckType(vrOleStr, varOleStr | varByRef);

SysFreeString(oleStr);
}

//--------------------------------------------------------------------
void TestAssignments()
{
Variant vEmpty, vNull;

//by val
Variant vSmallint, vInteger, vSingle, vDouble, vCurrency, vDate;

Variant vOleStr, vDispatch, vBoolean, vByte, vString, vUnknown;

//by ref
Variant vrSmallint, vrInteger, vrSingle, vrDouble, vrCurrency, vrDate;
Variant vrOleStr, vrDispatch, vrBoolean, vrByte, vrString, vrUnknown;

static short smallint = 3;
static int integer = 4;
static float flt = 5;
static double dbl = 6;
static Currency currency = 7;
static TDateTime date = String("05/11/95");
static WordBool boolean = true;
static Byte byte = 9;

static wchar_t* oleStr = SysAllocString(L"Hey, welcome to the world of variant and OLE");

vEmpty = Variant();
vNull = Variant().ChangeType(varNull);

//by value
vSmallint = (short)3;
vInteger = 4;
vSingle = (float)5;
vDouble = (double)6;
vCurrency = Currency(7);
vDate = (TDateTime) StrToDate(String("03/27/77"));
vBoolean = true;
vByte = (Byte)9;
vString = "This is a Ansi string";
vOleStr = L"This is an OLE string";
vDispatch = Variant::CreateObject("Word.Basic");

vUnknown = Variant::CreateObject("Word.Basic").AsType(varUnknown);

// by reference
vrSmallint = &smallint;
vrInteger = &integer;
vrSingle = &flt;
vrDouble = &dbl;
vrCurrency = ¤cy;
vrDate = &date;
vrBoolean = &boolean;
vrByte = &byte;
vrOleStr = &oleStr;

cout << endl << "TestAssignments output:" << endl;
CheckType(vEmpty, varEmpty);

cout << endl << "TestAssignments (by val) output:" << endl;
CheckType(vSmallint, varSmallint);

CheckType(vInteger, varInteger);
CheckType(vSingle, varSingle);
CheckType(vDouble, varDouble);
CheckType(vCurrency, varCurrency);
CheckType(vDate, varDate);
我不懂电脑 2006-07-18
  • 打赏
  • 举报
回复
Variant is a C++ implementation of the Object Pascal intrinsic type Variant.

Header

sysvari.h

Description

The Variant type is capable of representing values that change type dynamically. Whereas a variable of any other type is statically bound to that type, a variable of the Variant type can assume values of differing types at run-time. The Variant type is most commonly used in situations where the actual type to be operated upon varies or is unknown at compile-time. While variants offer great flexibility, they also consume more memory than regular variables, and operations on variants are substantially slower than operations on statically typed values.

A Variant has the following characteristics:

Variants can contain integer values, real values, string values, boolean values, date-and-time values, and OLE Automation objects. In addition, variants can contain arrays of varying size and dimension with elements of any of these types.
The special Variant value Unassigned is used to indicate that a variant has not yet been assigned a value, and the special variant value Null is used to indicate unknown or missing data.
A Variant can be combined with other variants and it can be constructed from any of the following data types. The compiler automatically performs the necessary type conversions.

?short
?int
?float
?double
?Currency
?TDateTime
?bool
?WordBool
?Byte
?AnsiString&
?char *
?wchar_t * const
?Ole2::IDispatch* const
?Ole2::IUnknown* const
When a Variant contains an OLE Automation object, the variant can be used to get and set properties of the object, and to invoke methods on the object.
Variant variables are always initialized to be Unassigned when they are first created. This is true whether a variant variable is global, local, or part of a structure such as an array.

Note: Use only the operators declared within Variant. The compiler ignores any operators you overload yourself.
Note: For binary operators, if one operand is of type Variant, the other operand is automatically converted to type Variant.

Differences from Object Pascal Variants

The C++Builder syntax for creating Variant type arrays is different from the Object Pascal usage. For example, consider the following Object Pascal code:

V: Variant;

V := VarArrayCreate([0,HighVal,0,HighVal],varInteger);

In C++Builder, you can use Variant and the OPENARRAY macro like this.

Variant V(OPENARRAY(int,(0,HighVal,0,HighVal)),varInteger);
daydayup234 2006-07-18
  • 打赏
  • 举报
回复
1 在CB中VARIANT等于tagVARIANT,它是一个结构
2 而varVariant 是一个静态短整型常量,就是12
static const Shortint varVariant = 0xc;
3 严格说Variant 与VARIANT 不同。Variant是TVarData的子类,它常常用于OLE操作
Variant 是对象pasical内部类型变量的 c++封装。
4 这样事例化Variant类,让它封装字串:
Variant vString(String("类Variant的一个事例vString,它封装本字串"));
5 直接可以将封装字串的Variant实例转化成字串:
AnsiString s;
Variant v(String("封字串"));
if(v2.VType==varString)
{
s=AnsiString(v);
ShowMessage(s);
}
YJS050320001 2006-07-18
  • 打赏
  • 举报
回复
对不起 刚才丢人了
ab=AnsiString(User_GetDataASC.VType=varString);
这条应该是返回的String的长度;
现在我的问题就是:
实际数据为:>+0.0000+0.0000+0.0000+0.0000+0.0000+0.0000+0.0000+0.0000+0.0000
存储到User_GetDataASC这个VARIANT的变量中,
我该如何将他转换为我想要得String类型
YJS050320001 2006-07-18
  • 打赏
  • 举报
回复
我在调试过程中使用了下面的命令:
AnsiString ab;
ab=String(User_GetDataASC.AsType(varString));
Memo2->Text=Memo2->Text+ab; //数据在Memo2中显示
当数据返回时系统会报错,告诉我类型不匹配;
但如果我将它改为:
AnsiString ab;
ab=AnsiString(User_GetDataASC.VType=varString);
Memo2->Text=Memo2->Text+ab;
数据有返回 但数据不对 返回数据为:256
实际数据应该为:>+0.0000+0.0000+0.0000+0.0000+0.0000+0.0000+0.0000+0.0000+0.0000
YJS050320001 2006-07-17
  • 打赏
  • 举报
回复
还有一个比较丢人的问题就是 怎么VARIANT是什么类型呢?
在头文件中,他声明的是VARIANT类型,VARIANT和Variant是一样的吗?

YJS050320001 2006-07-17
  • 打赏
  • 举报
回复
如果它定义的又是一个Variant类型呢
即是上面提到的varVariant A variant
这样该如何转换成String呢?
YJS050320001 2006-07-17
  • 打赏
  • 举报
回复
谢谢 我想我的问题应该是没有判断VARIANT的类型
CACACACACA 2006-07-17
  • 打赏
  • 举报
回复
extern PACKAGE TVarType __fastcall VarType(const Variant &V);

VarType Contents of variant

varEmpty The variant is Unassigned.
varNull The variant is Null.
varSmallint 16-bit signed integer (type Smallint in Delphi, short in C++ ).
varInteger 32-bit signed integer (type Integer in Delphi, int in C++).
varSingle Single-precision floating-point value (type Single in Delphi, float in C++).
varDouble Double-precision floating-point value (type double).
varCurrency Currency floating-point value (type Currency).
varDate Date and time value (type TDateTime).

varOleStr Reference to a dynamically allocated UNICODE string.
varDispatch Reference to an Automation object (an IDispatch interface pointer).
varError Operating system error code.
varBoolean 16-bit boolean (type WordBool).
varVariant A variant.
varUnknown Reference to an unknown object (an IInterface or IUnknown interface pointer).
varShortInt 8-bit signed integer (type ShortInt in Delphi or signed char in C++)
varByte A Byte
varWord unsigned 16-bit value (Word)

varLongWord unsigned 32-bit value (type LongWord in Delphi or unsigned long in C++)
varInt64 64-bit signed integer (Int64 in Delphi or __int64 in C++)
varStrArg COM-compatible string.
varString Reference to a dynamically allocated string (not COM compatible).
varAny A CORBA Any value.
判断一下类型,再转换.
记得Sring有这个重载的方法.
String s = String( User_GetDataHEX );



13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧