|
Hi,
let me put a couple of things straight first: there are no decimal numbers, no hexadecimal numbers, no binary numbers, there are just numbers. A number is a number, it does not have a base.
It is only when a number gets represented by a string (maybe because it gets shown to the user, or input by the user) that it becomes a decimal, hexadecimal, binary, or whatever representation of that number.
And here are some typical operations:
int num=20;
string decimal1=num.ToString();
string decimal2=num.ToString("D5");
Console.WriteLine("decimal representation: "+decimal1);
string hex4=num.ToString("X4");
Console.WriteLine("hexdecimal representation: "+hex4);
int val16;
int32.TryParse(hex4, NumberStyles.Hexadecimal, null, out val16);
Console.WriteLine("The value of hex "+hex4+" is "+val16);
string binary=Convert.ToString(num, 2);
int val2=Convert.ToInt32(binary, 2);
Console.WriteLine("The value of binary "+binary+" is "+val2);
string octal=Convert.ToString(num, 8);
int val8=Convert.ToInt32(binary, 8);
Console.WriteLine("The value of octal"+octal+" is "+val8);
|
|
|
|
|
i try your code
string binary = Convert.ToString(Encoding.UTF8.GetBytes("a")[0], 2);
--------------------
//binary value is "1100001"
//but "1100001" is 7 bits. its must be 8 bit. must be value of binary "01100001".
|
|
|
|
|
sure, when you mean "eight" do you write "0000000008" or "8"? it isn't because a 32-bit integer could hold up to 10 digits that you must prefix all those zeroes, is it? Not in decimal, not in hex, not in octal, not in binary.
If you want something prefixed, use string.PadLeft()
FYI: There is a shortcut for hexadecimal, the format specifier "X4" enforces at least four digits, prefixing zeroes as necessary. However there is no binary or octal format specifier in .NET
|
|
|
|
|
Having just seen his reply to you, I think you are going to have fun with this, Luc! He seems to be saying that leading zeros are significant...
Why do they let these people loose with a keyboard?
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
I do not know English fully. so I could not understand the full last message.
Thank you for your reply
|
|
|
|
|
Don't worry, it is Friday, for some people it is time for a spirit shift.
|
|
|
|
|
OriginalGriff wrote: leading zeros are significant
of course they are, you need them as soon as you want to perform shifts, rotates, concatenates, and all kinds of advanced bit fiddling. Come and see next week.
|
|
|
|
|
No, no, zero means "nothing". Therefore you can just delete them from your bit stream with impunity - it's an advance form of data compression.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
OriginalGriff wrote: it's an advance form of data compression
Funny you mention that. It is how I explain the infinite power of compression:
1. look at your data in binary format
2. remove the zeroes
3. count the ones
4. write that down as a binary number
5. if zeroes in sight go to step 2
A more advanced scheme goes on by incrementing the remaining data, and repeating everything.
|
|
|
|
|
I prefer a single stage compression technique, which if used correctly is extremely compact and fast to decompress. It consists of finding the random number seed that re-generates the input stream. You then only have to transmit the seed, and you are there.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
That's all hunky dory for handling a local situation, however there are certain legal restrictions that prevent world-wide use of your seeding scheme; e.g. you can't ship most kinds of seeds into Australia.
|
|
|
|
|
"There is no sadder sight in the world than to see a beautiful theory killed by a brutal fact."
Thomas Huxley
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
I had a housemate in back in college who came up with a similar algorithm. He was insitutionalized shortly afterward. The depressing part was that he was able to return the following semester, graduate, and got a full ride to Yale for grad school.
|
|
|
|
|
I know, prior art sunk my patent application.
|
|
|
|
|
I am not able to do still. if you write method....
-----
string myString = stringToBinary("a"); //return value will be 01100001 ( 8 bit )
string stringToBinary(string str)
{
.....
.....
.....
}
|
|
|
|
|
Something like this:
string alphabet = "abcdefg";
char[] chars = alphabet.ToCharArray();
foreach (char character in chars)
Console.WriteLine(Convert.ToString(character, 2).PadLeft(8, '0'));
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hi,
I have created toolbar for Excel. To one of the toolbar item i am trying associate the image. But it is failing.
stdole.IPictureDisp p = AxHost.GetIPictureDispFromPicture(image) as stdole.IPictureDisp;
commandBarButton.Picture = p;
When I am trying to assigning to command bar picture it is throwing following error.
Error HRESULT E_FAIL has been returned from a call to a COM component. (source: Office)
Exception info:System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
at Microsoft.Office.Core._CommandBarButton.set_Picture(IPictureDisp ppdispPicture)
Based on some events this tool bar image is changing.
I am not sure why it failing.But same thing is working for menu bar item.
Please help.
|
|
|
|
|
Are you sure p is not null ?
/ravi
|
|
|
|
|
|
Hi,
I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder.
now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows")
Can anybody tell me how to get this path?
Thanks.
Ted
- Life would be so much easier if I had the source code!
- If C# had true garbage collection, most applications would delete themselves upon execution
|
|
|
|
|
found it, this does the trick:
Directory.GetCurrentDirectory();
- Life would be so much easier if I had the source code!
- If C# had true garbage collection, most applications would delete themselves upon execution
|
|
|
|
|
If I have understood your requirement correctly you could create an environment variable to give you accees to your application form anywhere.
On the Advanced tab of System Properties click on the Environment Variables button. Add the path to your app to the PATH variable and you will be able to start it from anywhere.
me, me, me
"The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!"
Larry Niven
|
|
|
|
|
You can pick up the location of many different "special" folders. This will get you the Program Files folder:
string strProgFilesDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
If, however, you really meant that you wanted the name of the directory where the assembly was located, then you should use:
using System.IO;
.
.
.
string strMyHomeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
As an aside, rather than adding a program to the Windows folder, you should create a folder elsewhere (I have one called Tools) and add that folder to your environment Path. Perhaps it's just me but I don't like cluttering up system file locations.
|
|
|
|
|
I was spend for a long time looking for a decision of a problem, about the TextBox control that is included with Windows CE has a very limited opportunities for events related to mouse. I discovered that the team that developed the platform as it is a Compact Edition some controls are "clipped" from "unnecessary" events and an elaborate on them using unmanaged code by hooks and messages from the shell remitted. After a long search Internet and Googl-ing discovered fragments code and I have them so that they become finished code which help us for future implementations. You are welcome!
using System;
using System.Drawing;
using System.Runtime.InteropServices;
internal sealed class Win32
{
public delegate int WndProc(IntPtr hwnd, uint msg, uint
wParam, int lParam);
[DllImport("coredll.dll")]
public extern static int DefWindowProc(IntPtr hwnd,
uint msg, uint wParam, int lParam);
[DllImport("coredll.dll")]
public extern static IntPtr SetWindowLong(IntPtr hwnd, int nIndex,
IntPtr dwNewLong);
[DllImport("coredll.dll")]
public extern static int CallWindowProc(IntPtr lpPrevWndFunc,
IntPtr hwnd, uint msg, uint wParam, int lParam);
public const int GWL_WNDPROC = -4;
public const uint WM_PAINT = 0x000F;
public const uint WM_ERASEBKGND = 0x0014;
public const uint WM_KEYDOWN = 0x0100;
public const uint WM_KEYUP = 0x0101;
public const uint WM_MOUSEMOVE = 0x0200;
public const uint WM_LBUTTONDOWN = 0x0201;
public const uint WM_LBUTTONUP = 0x0202;
public const uint WM_LBUTTONDBLCLK = 0x0203;
public const uint WM_NOTIFY = 0x4E;
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
internal class WndProcHooker
{
public delegate int WndProcCallback(IntPtr hwnd, uint msg,
uint wParam, int lParam, ref bool handled);
private static Dictionary<IntPtr, HookedProcInformation> hwndDict = new Dictionary<IntPtr, HookedProcInformation>();
private static Dictionary<Control, HookedProcInformation> ctlDict = new Dictionary<Control, HookedProcInformation>();
public static void HookWndProc(Control ctl,
WndProcCallback callback, uint msg)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}
if (hpi == null)
{
hpi = new HookedProcInformation(ctl,
new Win32.WndProc(WndProcHooker.WindowProc));
ctl.HandleCreated += new EventHandler(ctl_HandleCreated);
ctl.HandleDestroyed += new EventHandler(ctl_HandleDestroyed);
ctl.Disposed += new EventHandler(ctl_Disposed);
if (ctl.Handle != IntPtr.Zero)
{
hpi.SetHook();
}
}
if (ctl.Handle == IntPtr.Zero)
{
ctlDict[ctl] = hpi;
}
else
{
hwndDict[ctl.Handle] = hpi;
}
hpi.messageMap[msg] = callback;
}
static void ctl_Disposed(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
{
ctlDict.Remove(ctl);
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}
static void ctl_HandleDestroyed(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (hwndDict.ContainsKey(ctl.Handle))
{
HookedProcInformation hpi = hwndDict[ctl.Handle];
UnhookWndProc(ctl, false);
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}
static void ctl_HandleCreated(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
{
HookedProcInformation hpi = ctlDict[ctl];
hwndDict[ctl.Handle] = hpi;
ctlDict.Remove(ctl);
hpi.SetHook();
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}
private static int WindowProc(IntPtr hwnd, uint msg,
uint wParam, int lParam)
{
if (hwndDict.ContainsKey(hwnd))
{
HookedProcInformation hpi = hwndDict[hwnd];
if (hpi.messageMap.ContainsKey(msg))
{
WndProcCallback callback = hpi.messageMap[msg];
bool handled = false;
int retval = callback(hwnd, msg, wParam, lParam, ref handled);
if (handled)
return retval;
}
return hpi.CallOldWindowProc(hwnd, msg, wParam, lParam);
}
System.Diagnostics.Debug.Assert(false,
"WindowProc called for hwnd we don't know about");
return Win32.DefWindowProc(hwnd, msg, wParam, lParam);
}
public static void UnhookWndProc(Control ctl, uint msg)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}
if (hpi == null)
{
throw new ArgumentException("No hook exists for this control");
}
if (hpi.messageMap.ContainsKey(msg))
{
hpi.messageMap.Remove(msg);
}
else
{
throw new ArgumentException(String.Format(
"No hook exists for message ({0}) on this control",
msg));
}
}
public static void UnhookWndProc(Control ctl, bool disposing)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}
if (hpi == null)
{
throw new ArgumentException("No hook exists for this control");
}
if (ctlDict.ContainsKey(ctl) && disposing)
{
ctlDict.Remove(ctl);
}
if (hwndDict.ContainsKey(ctl.Handle))
{
hpi.Unhook();
hwndDict.Remove(ctl.Handle);
if (!disposing)
{
ctlDict[ctl] = hpi;
}
}
}
class HookedProcInformation
{
public Dictionary<uint, WndProcCallback> messageMap;
private IntPtr oldWndProc;
private Win32.WndProc newWndProc;
private Control control;
public HookedProcInformation(Control ctl, Win32.WndProc wndproc)
{
control = ctl;
newWndProc = wndproc;
messageMap = new Dictionary<uint, WndProcCallback>();
}
public void SetHook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
{
throw new InvalidOperationException(
"Handle for control has not been created");
}
oldWndProc = Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(newWndProc));
}
public void Unhook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
{
throw new InvalidOperationException(
"Handle for control has not been created");
}
Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC, oldWndProc);
}
public int CallOldWindowProc(IntPtr hwnd,
uint msg, uint wParam, int lParam)
{
return Win32.CallWindowProc(oldWndProc, hwnd, msg,
wParam, lParam);
}
}
}
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace karlovo
{
public partial class TextBoxCustom : TextBox
{
new public event EventHandler DoubleClick;
public TextBoxCustom()
: base()
{
InitializeComponent();
WndProcHooker.HookWndProc(this, new WndProcHooker.WndProcCallback(WindowMessageHandler), Win32.WM_LBUTTONDBLCLK);
}
private int WindowMessageHandler(IntPtr hwnd, uint msg, uint wParam,int lParam, ref bool handled)
{
OnMouseDoubleClick(EventArgs.Empty);
handled = true;
return 0;
}
protected void OnMouseDoubleClick(EventArgs e)
{
if (DoubleClick != null)
{
DoubleClick(this, e);
}
}
}
}
So,all u need to do is to put this code in your custom control and implements the event. Thanks!!!
|
|
|
|
|
What a Great Help!Thank you very much!
|
|
|
|
|