Click to See Complete Forum and Search --> : ASCII Conversion
sivaprakash.shanmugam
May 25th, 2006, 07:28 AM
I have Bytes in the following variable "resultArray"
byte resultArray __gc[] = new byte __gc[1];
I need to Take string from this variable with ASCII encoding.
For reference in C# we will do the following to get the string
result = Convert.FromBase64String(resultArray );
Encoding.ASCII.GetString(result ) ;
Whats the corresponding VC++.
Siddhartha
May 25th, 2006, 07:54 AM
[ redirected ]
Regards,
Siddhartha
sivaprakash.shanmugam
May 25th, 2006, 09:35 AM
For this i got some idea from the following code but insted of using const wchar_t i have "byte resultArray __gc[] = new byte __gc[1];" how can encode my string.
const wchar_t* pwcTemp = L"Some text\0";
int nLength = _tcslen(pwcTemp );
char* pMultiByte = new char[nLength];
WideCharToMultiByte( CP_ACP, 0, pwcTemp, nLength,pMultiByte, nLength,0,0 );
++++++++++++++++++++++++++++++++
ThermoSight
May 26th, 2006, 10:16 PM
It's not entirely clear to me what environment you're working in, but C++ .NET has a class called ASCIIEncoding containing a method called GetString() which takes an unsigned char array as an argument and returns a String *.
I note that your array is a byte array. I believe that in C++ .net, a byte array IS an unsigned char array.
If you're running C++ .NET you might try ASCIIEncoding/GetString(), and perhaps a dynamic_cast or static_cast to convert from byte array to unsigned char array if necessary.
Don't know if it'll work, but it's something to consider.
Best wishes,
bill
sivaprakash.shanmugam
May 26th, 2006, 11:12 PM
CString *str = ASCIIEncoding::GetString (resultArray);
Yes as you mentioned i am working in VC++.Net; when i was trying to use the above statement to convert the unsigned character array to string i am getting
error C2352: 'System::Text::ASCIIEncoding::GetString' : illegal call of non-static member function
How to resolve this.
Shambler
May 27th, 2006, 08:21 AM
Yes you need to use:
Encoding::ASCII->GetString(blah);
However the ASCII conversion functions are bugged, it can't convert characters higher than hex value \x79 or something like that... (basicly, half the ASCII character set can't be converted)
You might get away with it if your string doesn't need characters above \x79 but if you do, then you will have to use Unicode strings:
Encoding::Unicode->GetString(blah);
ThermoSight
May 27th, 2006, 01:52 PM
When I use that class, which I do frequently, I do the following ....
ASCIIEncoding *utility = new ASCIIEncoding();
......
......
utility->GetBytes(pQuitSg, 0, pQuitSg->Length, pQuitRequest, 0);
(the line above is from an FTP client - you'll use method GetString(...))
re Shambler's comment regarding unsigned chars greater than 0x79, I've never encountered that situation since the only use I've had for strings has been to contain ordinary text which is by definition .lt. 0x80.
The inability to convert anything greater than 0x79 is not, to my mind, a bug .... it's simply a fact of life regarding types. In a char, 0x80 is the sign bit. To an unsigned char that's just another bit whereas to a signed entity it's a whole new context.
So, I don't think of that inability as a bug. I think of it as just one more pitfall of which the programmer must be aware.
You can get much of the information you need from the HELP section of the .NET IDE. It defines all the classes, identifying those members which are static, and those which are not.
Microsoft put out a Hell of a good package with their.NET. It's worth ten times what I paid. I am blown away by the power of it all. But..... that's just one old man's opinion!
Best wishes,
bill
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.