471,319 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to convert unsignedshort[] to string ?

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.

Dec 1 '05 #1
8 7682
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

Dec 1 '05 #2
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" <p.************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.

Dec 1 '05 #3
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" <cs*****@adelphia.net> wrote in message
news:XM********************@adelphia.com...
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" <p.************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.


Dec 1 '05 #4
Hi Chris,

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

Now i have achieved through Array.copy method.

Regards,
Prakash.

Dec 1 '05 #5
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
"Prakash" <p.************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi Chris,

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

Now i have achieved through Array.copy method.

Regards,
Prakash.

Dec 1 '05 #6
>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

Dec 1 '05 #7
Chris Springer <cs*****@adelphia.net> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 1 '05 #8
Prakash <p.************@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 1 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Joel Moore | last post: by
3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
7 posts views Thread by patang | last post: by
6 posts views Thread by patang | last post: by
4 posts views Thread by tshad | last post: by
9 posts views Thread by Marco Nef | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.