How to convert unsignedshort[] to string ?

  • Thread starter Thread starter Prakash
  • Start date Start date
P

Prakash

Hi Friends,

Using Remoting concept, i have send WCHAR[] values from VC++.net
applicaiton to C# applicaiton. In my C# application it is treated as
unsigned shot[]. I need to convert it back to string and display it.

I tried with TypeConverter.GetConverter().ConvertToString()
functionality. and String.Format() functionality. But i get the value
as "System.UInt16" as string.

Please help me to solve this issue.

Regards,
Prakash.
 
I suggest you try as following:

ushort[] uShortArray = {65, 66, 32, 67};
char[] charArray = new char[uShortArray.Length];
Array.Copy(uShortArray, charArray, uShortArray.Length);
string s = new String(charArray);
Console.WriteLine(s);

Hope it helps,
Thi
 
C# is a bit different than C++ in handling the primative types...in fact,
quite a bit different. C# actually defines structures (UInt16, UInt32, etc)
and uses the familiar syntax from C++ (int, long, ushort) as aliases for the
new structures. The kewl thing is...in the UInt16 (or unsigned
short/ushort) structure there is a built in function to convert a ushort to
a string (as there is with all of the other primative types.) If you wanted
to write the value out to the console you might do this...

System.UInt16 newint = new System.UInt16();
newint = 16;
Console.WriteLine(newint.ToString());

This would then write out 16 to the screen.

Just remember that in C# all of the primative types have structures
associated with them, and in fact aren't primative types anymore. Your
familiar keywords in C++ have been turned into aliases that are COMPLETELY
interchangable with the newly defined structures in the System namespace.

Hope this helps,

Chris

--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
 
I wanted to correct a statement i made. I said they "aren't" primative
types anymore and that isn't true...they are still considered the primative
types but are encapsulated in structures now. Just wanted to cover my butt
from the flamers...

Chris

--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
Chris Springer said:
C# is a bit different than C++ in handling the primative types...in fact,
quite a bit different. C# actually defines structures (UInt16, UInt32,
etc) and uses the familiar syntax from C++ (int, long, ushort) as aliases
for the new structures. The kewl thing is...in the UInt16 (or unsigned
short/ushort) structure there is a built in function to convert a ushort
to a string (as there is with all of the other primative types.) If you
wanted to write the value out to the console you might do this...

System.UInt16 newint = new System.UInt16();
newint = 16;
Console.WriteLine(newint.ToString());

This would then write out 16 to the screen.

Just remember that in C# all of the primative types have structures
associated with them, and in fact aren't primative types anymore. Your
familiar keywords in C++ have been turned into aliases that are COMPLETELY
interchangable with the newly defined structures in the System namespace.

Hope this helps,

Chris

--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
Prakash said:
Hi Friends,

Using Remoting concept, i have send WCHAR[] values from VC++.net
applicaiton to C# applicaiton. In my C# application it is treated as
unsigned shot[]. I need to convert it back to string and display it.

I tried with TypeConverter.GetConverter().ConvertToString()
functionality. and String.Format() functionality. But i get the value
as "System.UInt16" as string.

Please help me to solve this issue.

Regards,
Prakash.
 
Hi Chris,

I already tried with ToString() method.. its not working.

Now i have achieved through Array.copy method.

Regards,
Prakash.
 
Well, I'm glad you solved your problem but that method seems like a very
round about way to do it. If you post your full snippet of code we might be
able to optimize it for you. If you're content then that's ok too :P

Good Luck!

Chris

--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
 
Well, I'm glad you solved your problem but that method seems like a very
round about way to do it
I suggested Array.Copy basing on the fact that ushort and char are both
unsigned 2-byte integer. In other cases, a type-safe approach is
iterating through the source array, cast each element to target type,
and fill-in the destination array.
If you post your full snippet of code we might be
able to optimize it for you.
If there is something to optimized, I don't think it is for converting
array of ushort to array of char. It is: is there a way to unmarshall
WCHAR[] to a string instead of an array of ushort? The original poster
did not supply enough info to say.

Thi
 
Chris Springer said:
Well, I'm glad you solved your problem but that method seems like a very
round about way to do it. If you post your full snippet of code we might be
able to optimize it for you. If you're content then that's ok too :P

I think you've misunderstood the problem. The idea isn't to convert the
unsigned short 32 to "32" - it's to convert that to space, convert 65
to 'A' etc - at least, that's what it sounds like...
 
Prakash said:
Using Remoting concept, i have send WCHAR[] values from VC++.net
applicaiton to C# applicaiton. In my C# application it is treated as
unsigned shot[]. I need to convert it back to string and display it.

Can you get it to treat it as a char[] instead (on the C# side)? That
*should* work, and would make things a lot simpler - you could then
just call the String constructor which takes a char array.
 
Back
Top