|
String <-> Binary is easy:
Int to Binary String:
string s = Convert.ToString(myInt, 2);
Binary string to int:
int i = Convert.ToString(myString, 2);
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 can some speak english...
i want to convert string to binary.
example;
string myString = stringToBinary("a"); //return value will be 01100001
string stringToBinary(string str)
{
.....
.....
.....
}
|
|
|
|
|
You can cast char s to int s, an other post tells you what to do with the resulting int
|
|
|
|
|
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
|
|
|
|