|
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
|
|
|
|
|
Owner-draw it yourself. The ProgressBar in .NET is actually a wrapper class for a progress bar common control in Windows. There's only so much you can do with it before it's easier to do your own.
It's not hard, though. It's just (usually) a rectangle with a certain drawing region that's a percentage, such as value / max + min . There were some great articles by a certain person that went commercial and took all his articles with him...and after he used CP to help beta-test his code.
So, you can either P/Invoke code and try to override the functionality afforded by Windows for the ProgressBar wrapper, or just write your own. Again, it isn't hard. You can use the ControlPaint class to assist (i.e., do all the work) in drawing the correct 3D borders for a rectangle and draw whatever you like in the client area inside the borders (if you want borders, that is). Just use some similar functionality as the progress bar, which is basically a min, max, and value property, and a step method to increment the value (or use value++ and have the property's setter force a repaint). The OnPaint override should do all the drawing (using the ControlPaint for borders, and using the current min, max, and value properties to correctly draw the progress meter). You should also use Control.SetStyle to set the AllPaintingInWmPaint , UserPaint , DoubleBuffer , ResizeRedraw , and Opaque ControlStyles since you'll be handling all the drawing yourself.
-----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-----
|
|
|
|
|
Hello!
I would like to beautify my menuitem for the SystemTray Example, does it possible? But it always show "int numOldMenu = myMenu.Count;" has problem? Why?
The coding of SystemTrayExample is listed as following:
using System;
using System.Drawing;
using System.Resources;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading ;
using Utility.NiceMenu;
namespace Demo
{
public class SystemTray : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon WSNotifyIcon;
private System.ComponentModel.IContainer components;
private Icon mDirIcon = new Icon(typeof(SystemTray).Assembly.GetManifestResourceStream("SystemTrayExample.FLGUSA02.ICO"));
//private NiceMenu myMenu ;
public SystemTray()
{
//constructor for the form
InitializeComponent();
//keep the form hidden
this.Hide();
//OnMyMenu();
//InitializeNotifyIcon();
NiceMenu myNiceMenu = new NiceMenu();
myNiceMenu.UpdateMenu (this.ContextMenu ,new NiceMenuClickEvent (InitializeNotifyIcon));
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.WSNotifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
//
// WSNotifyIcon
//
this.WSNotifyIcon.Text = "";
this.WSNotifyIcon.Visible = true;
//this.WSNotifyIcon.MouseDown += new System.Windows.Forms.MouseEventHandler(this.WSNotifyIcon_MouseDown);
//
// SystemTray
//
this.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(8, 7);
this.ControlBox = false;
this.Enabled = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SystemTray";
this.Opacity = 0;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new SystemTray());
}
public void ExitControlForm(object sender, EventArgs e)
{
//Hide the NotifyIcon.
WSNotifyIcon.Visible=false;
this.Close();
}
//private void InitializeNotifyIcon()
private void InitializeNotifyIcon(object sender, System.EventArgs e)
{
//setup the Icon
NotifyIcon WSNotifyIcon = new NotifyIcon();
WSNotifyIcon.Icon = mDirIcon;
WSNotifyIcon.Text = "System Tray Example";
WSNotifyIcon.Visible = true;
//Create the submenus - any number will do
MenuItem[] subMnuItems1 = new MenuItem[3];
subMnuItems1[0] = new MenuItem("Submenu 1", new EventHandler(this.ExitControlForm));
subMnuItems1[1] = new MenuItem("Submenu 2", new EventHandler(this.ExitControlForm));
subMnuItems1[2] = new MenuItem("Submenu 3", new EventHandler(this.ExitControlForm));
//Create the MenuItem objects and add them to
//the context menu of the NotifyIcon.
MenuItem[] mnuItems = new MenuItem[3];
//create the menu items array
mnuItems[0] = new MenuItem("Sub Menu Items");
mnuItems[0].MenuItems.AddRange(subMnuItems1);
mnuItems[0].DefaultItem=true;
mnuItems[1] = new MenuItem("-");
mnuItems[2] = new MenuItem("Exit", new EventHandler(this.ExitControlForm));
//add the menu items to the context menu of the NotifyIcon
ContextMenu notifyIconMenu = new ContextMenu(mnuItems);
WSNotifyIcon.ContextMenu=notifyIconMenu;
}
}
}
fm Softair
|
|
|
|
|
To what "problems" are you referring? Specifics are important. If an exception is being thrown, what exception is it and what is its message?
Also, please post only relevent code to the forums.
-----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-----
|
|
|
|
|
i was wondering if anyone could give me some good information on shell programming...im intrested in tieing my C# program into the right click menu. i want it to be easily uninstallable (like winzip does) any ideas or suggestions ?
Jesse M
The Code Project Is Your Friend...
|
|
|
|