|
The byte array is made of bytes. They do not represent characters.
|
|
|
|
|
Then... uhm. why, if I may ask, on earth are you trying to cast an array of bytes into a string?
--
I am of The Clan of Xymox. I wear a pink kilt!
|
|
|
|
|
Well, that is not necessarily my ultimate solution, but I have gotten runtime errors when trying to pass the data as arrays. I may be able to, but I can't quite get everything right.
I chose a string at first because it is in a struct that mimics an unmanaged struct and the original, unmanaged struct defines it as a string. The whole struct is then passed by reference in an interop call to a windows API function. When I use a string, the code runs, but the data in the string is incorrect and I get incorrect results.
When I have used byte[] or char[] or even StringBuilder, I got runtime errors, saying the struct cannot converted to unmanaged.... the size cannot be determined.
I might be able to do it both ways, I just need to figure out what I'm missing...
|
|
|
|
|
Tym! wrote:
Well, that is not necessarily my ultimate solution, but I have gotten runtime errors when trying to pass the data as arrays. I may be able to, but I can't quite get everything right.
I'm still confident though that this is the right way to do it. God knows what the .NET runtime does to strings when you're not watching...
--
I am of The Clan of Xymox. I wear a pink kilt!
|
|
|
|
|
While it is counter-intuitive, what you want in this case is UnicodeEncoding.GetString(byte[]) . Because the data inside a string is natively stored using that UnicodeEncoding, it is a null transform, leaving your bytes alone.
(I suspect you'd better have an even number of bytes in your array if you want this to work. Herein lies a subtle inconsistency between BSTR's and System.Strings. BSTR's kind of admit to being used to pass random data around, and hence support odd numbers of bytes even though they are supposedly a strings of 16bit values. Your API hopefully won't care if you pad an extra null on the end if the length was odd.)
-Blake
|
|
|
|
|
yeah, it's always going to be a multiple of 4 bytes, so I don't need to worry about padding. I used the UnicodeEncoding and get a different, yet still incorrect, result.
I wonder if I am not declaring the struct correctly or I don't know, I feel that I am so close, but am just missing something...
Thanks for your help.
|
|
|
|
|
What about something like this? I didn't test, so it might have a typo, but would this sort of approach work? I don't know how you want the ordering. -Jeff
private const string blankString = "";
public static string ConvertToString(byte[] bytes) {
if (bytes == null) {
return blankString;
}
int length = bytes.Length;
if (length == 0) {
return blankString;
}
else if ((length % 2) == 0) {
length /= 2;
char[] chars = new char[length];
for(int charIndex = 0, byteIndex = 0; charIndex < length; charIndex++, byteIndex += 2) {
chars[charIndex] = (char)((((int)bytes[byteIndex]) << 8) + ((int)bytes[byteIndex + 1]));
}
return new string(chars);
}
else {
length /= 2;
char[] chars = new char[length + 1];
for(int charIndex = 0, byteIndex = 0; charIndex < length; charIndex++, byteIndex += 2) {
chars[charIndex] = (char)((((int)bytes[byteIndex]) << 8) + ((int)bytes[byteIndex + 1]));
}
chars[length] = (char)(((int)bytes[bytes.Length - 1]) << 8);
return new string(chars);
}
}
|
|
|
|
|
Like Jorgen said u need to get your codepage.
Get it with Encoding.Default . that should fix the issue.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
Thanks everyone for your help. Just to let you know, i ended up using encoding.default to get it working. Got a little lesson on .net text...
Thanks again!
|
|
|
|
|
Hi!
I have just begun lerning C#, so I still don’t know about it clearly!
I want to ask:
Are there any ways to put some codes into an event of a control similar to ClassWizard of VC++ 6.0? or do we have to create a delegate, then add it to event collection of the control? I think the latter is difficulty and complex!
Thanks!
xyz
|
|
|
|
|
I'm not sure I follow. The event system for the C# language works as follows:
someControl.Click += new EventHandler(MyHandleMethod);
someControl.Click -= new EventHandler(MyHandleMethod) If you think this is difficult or complex, try the "event" system in Java!
This code is automatically translated by the C# compiler into something else that does amount to creating a new delegate and adding it to a collection. Internally, class events essentially become a table with collections keys (the table key is the event name, the value is a collection of delegate), but - again - you don't need to worry about doing this in your source code (unless you really want to)! Just type event [EventHandlerDelegate Type] [EventName] . There's plenty of documentation on the event system in C# and VB.NET in the .NET SDK docs, which is always a good place to check-out for newbies (especially, IMO, at least glancing over the entirety of the base class library to see what's available).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
...and in the Whidbey release, it'll be even easier, less code to write.
The graveyards are filled with indispensible men.
|
|
|
|
|
I'm not sure about that myself. Seems like they're starting to victimize C# as VB was from the start by making too many shortcuts for lazy programmers! Besides, think about answers in the forums. Some n00b asks how to do something, someone answers him one way using the delegate short-form. The n00b disagrees (that always makes me laugh!) because he saw it using the long-form. I think we're going to have lots of cases where typical answer-providers are going to have to explain that quite a bit.
There's already way too many lazy programmers in this field - just look at the VB forum!
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
There's already way too many lazy programmers in this field - just look at the VB forum!
-Nick Parker
DeveloperNotes.com
|
|
|
|
|
If you have a location X and Y, how do you move a control from where it is on a windows form to the location x and y?
Something like
mycontrol.Move(x, y); ???
thanks
|
|
|
|
|
Try this:
mycontrol.Location = new Point(x, y);
|
|
|
|
|
thank you
that was simple and elegant
|
|
|
|
|
Please can u help on this.
I have a one dataset contains the one datatable. I want pass this datatable as parameter to the storedprocedure because we have one datatype i.e table.
is it possible?if so can u help me on this.
p.v.babu
|
|
|
|
|
I don't think there is an easy way. One article here on CP shows how to use an image data type to pass through an array, but I found that a bit complex.
This KB article though on HOW TO: Perform Bulk Updates and Inserts Using OpenXML with .NET Providers in Visual C++ .NET was brilliantly helpful to me. I had to pass through a huge DataTable array to a SP, and it worked like a bomb.
regards,
Paul Watson
Bluegrass
South Africa
Miszou wrote:
I have read the entire internet. on how boring his day was.
Crikey! ain't life grand?
|
|
|
|
|
Hi,
How to launch an external programm from the application, like the C commanc System ("filename") would do ?
Regards,
Matthieu
|
|
|
|
|
System.Diagnostics.Process.Start("filename", "arguments");
|
|
|
|
|
Is there a way to change the background color of a single character (or
maybe even a larger selection) in a RichTextBox?
|
|
|
|
|
|
Hey! I already answered this.
The graveyards are filled with indispensible men.
|
|
|
|
|
Hello!
I would like to know how to beautify the Progress Bar? e.g. use gradient color or image for the progression index. Any sample coding here?
fm Softair
|
|
|
|