 |
|
 |
Rudy,
I'm planning to use your Bin.dll.
Do you have explicit evidence to state that this DLL is licensed freeware?
Khadar
modified on Tuesday, March 31, 2009 8:11 AM
|
|
|
|
 |
|
 |
I don't know what you mean other than the fact that I wrote the code from scratch.
I think there is a general license for codeproject users so this code falls under that license. Other than that, feel free to use it as needed. I think I did a couple of fixes later on. I will search for the update and will let you know the fixes.
|
|
|
|
 |
|
 |
Hey,
Amazing conversions! The code was very very helpful!
Thanks!
|
|
|
|
 |
|
 |
hi!
i m new on this site..i m unable to open zip folder,
visual studio conversion wizard appears and after finishing.a window pops up but not any code?please help in case
|
|
|
|
 |
|
 |
Can you please give me a code for Binary,Octal,hexadecimal systems to decimal system in c?
Thanks!
|
|
|
|
 |
|
 |
If you meant C# the code is already part of the zip contents.
If you really meant "C" then you can convert based on my code.
|
|
|
|
 |
|
 |
can u show me plsss....
hex to dec simple program code.
ranzJR ^^
|
|
|
|
 |
|
 |
I am not familiar with Java but it should be simple. Following is a simple example on how to do this conversion using vb.net:
Public Shared Function HexToInt(ByVal hexString As String) As Integer
Dim result As Integer = 0
Dim currValue As Integer
Dim hexChars() As Char = hexString.ToCharArray()
For i As Integer = 0 To hexChars.Length - 1
currValue = Uri.FromHex(hexChars(i))
result <<= 4
result = result Or currValue
Next
Return result
End Function
In java you probably don't have "Uri.FromHex" so what you can do in this case is look at the character and if it's 0 to 9 then it's just the number itself and if it's A to F then convert to number 10 to 15.
|
|
|
|
 |
|
 |
Bonjour,
I find your code very interesting but
I'm a newbie and i need some helps:
I need to diplay a binary file (from a mail's attachment) to hexdecimal and I don't know how to do that.
Could you show me an example by using your class ?
many thanks
Neksa
|
|
|
|
 |
|
 |
Neksa,
You just need to create a Bin.Hex variable then call ToString() method.
Following is an example which has extra lines of code to measure time. The ToString() method may need some optimization for very large files.
private string ReadBinaryFile(string path)
{
FileStream fs = new FileStream(this.txtFilepath.Text, System.IO.FileMode.Open);
int numBytes = (int)fs.Length;
byte[] buffer = new byte[numBytes];
BinaryReader reader = new BinaryReader(fs);
reader.Read(buffer, 0, numBytes);
fs.Close();
long t1 = DateTime.Now.Ticks;
Bin.Hex hexData = new Bin.Hex(buffer);
long t2 = DateTime.Now.Ticks;
string result = hexData.ToString();
long t3 = DateTime.Now.Ticks;
TimeSpan ts1 = new TimeSpan(t2-t1);
TimeSpan ts2 = new TimeSpan(t3-t2);
Trace.WriteLine("Constructor: " + ts1.Milliseconds.ToString()+"ms ToString:"+ts2.Milliseconds.ToString()+"ms");
return result;
}
Regards,
Rudy.
|
|
|
|
 |
|
 |
Hi,
Good piece of work! I am using your code for int to bin conversion. Actually I have added some methods to convert from ulong to bin. I have a general remark concerning signed and unsigned integers, mostly for people who are unfamiliar with binary numbers. Here goes:
An overflow exception is thrown (without being caught) when the number of bits we provide is bigger than the expected number. That's normal. However, there is a little thing to note. When converting from Int32 to bin, the biggest number of bits we are allowed to enter is 31 and not 32 as Int32 indicates. That's because the leftmost bit is used for the sign, since Int32 is a SIGNED integer. The leftmost bit is the 32nd bit when counting from the right, its position is 31 since we begin counting from 0 and not from 1. The code provided takes this into consideration and provides 2 overloads of the constructor:
public Bits(int data, int numBits) and public Bits(uint data, int numBits)
So in order not to have an exception thrown, we must be careful to provide a maximum number of 31 when using the 1st method, but we can use 32 bits when calling the second method and providing an unsigned integer.
That's it. I hope this small contribution helps some of you out there
Talal
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
--Rich Cook
|
|
|
|
 |
|
 |
Talal,
Thanks for your input. Soon I will modify the code to have more error checking on several functions.
Regards,
Rudy.
|
|
|
|
 |
|
 |
hello,
I couldn't convert from Hex into binary.
The following Error is given:An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Could not find any parsable digits.
I traced the code and it stops on this line:
byteArray[i] = Convert.ToByte(sTemp, 16); which is included in this function:
private byte [] ParseByteString(string data).
Please an help !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Regards Rany,
|
|
|
|
 |
|
 |
Rany,
The conversion doesn't do any verification that the data passed is valid. Most likely the hex string passed to the conversion is not valid.
To get you going, just do a verification that the data is a valid hex number before you do the conversion. I will post a new release that verifies all inputs are valid before doing any conversions.
Regards,
Rudy.
|
|
|
|
 |
|
 |
I made some updates to this project to work with a low-level debugger I'm writing. I wanted to use this as an edit box that displays the contents of a CPU register in 4 possible modes: Hex, Decimal Signed, Decimal Unsigned, Binary.
Also, I needed it to support several states: modified by system, modified by user, and committed. I show modified by system with a red foreground, modified by user with a yellow background, and committed with normal black on white. It also keeps the previous value if the user modifies it so it can rollback the value.
I am not using the HexTextBox in a grid at this time, but that may be a future enhancement. I've made a new HexGrid that has many enhancements but I'm not ready to publish that yet. It's turned into a big project. There are many limitations of the Windows DataGrid that came into play, but thanks to the OOP design, I was able to customize all of those areas to make it behave as I wanted it to.
You can download my current HexTextBox from here. I'll make changes to this sometimes, so please submit bugs to me.
http://www.EricEngler.com/downloads/HexTextBox.zip
|
|
|
|
 |
|
 |
Thanks Eric, I will try it out.
I also made some minor enhancements / bug fixes. Will compare with your project and will send you an update.
Regards,
Rudy.
|
|
|
|
 |
|
 |
Have you updated this bin.dll to Ver 2.0 .NET?
SAG, CANADA
|
|
|
|
 |
|
 |
All the classes used in this project are backwards compatible with VS2.0 so you should be able to load it from VS2.0 with no problems at all. I haven't had a chance to verify functionality, but it should work just as well.
Regards,
Rudy.
|
|
|
|
 |
|
 |
You should never name a project "Bin" - this is confusing because everyone expects "bin" to be the name of a folder.
The "bye position" is just an indicator of which cell is selected in the grid, and it's not really a filter like the other items in that group box (which are both really bit filters).
The Grid is not updated after a user edits data. This is OK, but the demo would be more cool if you'd update the grid cell after the user edits data.
|
|
|
|
 |
|
 |
Hi!
Your code will be faster for your defined data types, but you can have my code for general conversion up to base 36 or something.
I must have written it about 5 years ago; I was only 10 then so it's in VB6, probably inefficient and poorly written. I'm sorry if it is and you'll be glad that I've moved from VB 6 to VB .NET then to C#.
Do whatever you want with the code below
'Can convert from any base to any other base
Option Explicit
Private Sub cmdCalculate_Click()
Dim aInputNum() As Double
Dim aOutputNum() As Double
Dim i As Long
Dim Base10Num As Variant
On Error GoTo defaulterr
'Convert the input number to base 10
'resize the array holding the numbers of the characters
ReDim aInputNum(Len(InputNum.Text))
'put the characters into an array of characters from a string and converts other symbols
For i = 0 To Len(InputNum.Text) - 1
aInputNum(i) = ConvertIntoNum10(Mid(InputNum.Text, i + 1, 1))
Next i
On Error GoTo base10err
'convert to base 10
For i = 0 To Len(InputNum.Text) - 1
Base10Num = Base10Num + (aInputNum(i) * (FirstBase.Text ^ (Len(InputNum.Text) - i - 1)))
Next i
Erase aInputNum
NumInBase10.Text = Format$(Base10Num, "########################################")
'convert base 10 to the other base
'find the biggest number that fits into Base10Num
Dim MaxTableSize As Long
Do
If Val(SecondBase.Text) ^ MaxTableSize > Base10Num Then Exit Do
MaxTableSize = MaxTableSize + 1
DoEvents
Loop
ReDim aOutputNum(MaxTableSize)
Dim TableVal As Variant
Dim TempNum As Double
Dim OutputStr As String
TempNum = Base10Num
For i = 0 To MaxTableSize - 1
TableVal = Val(SecondBase.Text) ^ (MaxTableSize - i - 1)
Do
If Not (TempNum - TableVal) > -1 Then
Exit Do
Else
TempNum = TempNum - TableVal
aOutputNum(i) = aOutputNum(i) + 1
End If
DoEvents
Loop
Next i
'make output string
For i = 0 To MaxTableSize - 1
OutputStr = OutputStr & ConvertIntoChar(aOutputNum(i))
Next i
'output, clipping off the leading zeros
OutputNum.Text = Format$(OutputStr, "########################################")
Exit Sub
base10err:
Debug.Print Err.Description
If Err.Number = 6 Then 'overflow
MsgBox "The number to be converted is too large", vbExclamation, "Number Transformer"
Else
GoTo defaulterr
End If
Exit Sub
defaulterr:
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Number Transformer"
End Sub
Private Sub Form_Load()
Me.Show
#If FDEBUG = 1 Then
MsgBox "using conditional compilation!"
#End If
End Sub
Public Function ConvertIntoNum10(character As String) As Double
character = UCase(character)
Select Case character
Case "0": ConvertIntoNum10 = 0
Case "1": ConvertIntoNum10 = 1
Case "2": ConvertIntoNum10 = 2
Case "3": ConvertIntoNum10 = 3
Case "4": ConvertIntoNum10 = 4
Case "5": ConvertIntoNum10 = 5
Case "6": ConvertIntoNum10 = 6
Case "7": ConvertIntoNum10 = 7
Case "8": ConvertIntoNum10 = 8
Case "9": ConvertIntoNum10 = 9
Case "A": ConvertIntoNum10 = 10
Case "B": ConvertIntoNum10 = 11
Case "C": ConvertIntoNum10 = 12
Case "D": ConvertIntoNum10 = 13
Case "E": ConvertIntoNum10 = 14
Case "F": ConvertIntoNum10 = 15
Case "G": ConvertIntoNum10 = 16
Case "H": ConvertIntoNum10 = 17
Case "I": ConvertIntoNum10 = 18
Case "J": ConvertIntoNum10 = 19
Case "K": ConvertIntoNum10 = 20
Case "L": ConvertIntoNum10 = 21
Case "M": ConvertIntoNum10 = 22
Case "N": ConvertIntoNum10 = 23
Case "O": ConvertIntoNum10 = 24
Case "P": ConvertIntoNum10 = 25
Case "Q": ConvertIntoNum10 = 26
Case "R": ConvertIntoNum10 = 27
Case "S": ConvertIntoNum10 = 28
Case "T": ConvertIntoNum10 = 29
Case "U": ConvertIntoNum10 = 30
Case "V": ConvertIntoNum10 = 31
Case "W": ConvertIntoNum10 = 32
Case "X": ConvertIntoNum10 = 33
Case "Y": ConvertIntoNum10 = 34
Case "Z": ConvertIntoNum10 = 35
End Select
End Function
Public Function ConvertIntoChar(num10 As Double) As String
Select Case num10
Case 0: ConvertIntoChar = "0"
Case 1: ConvertIntoChar = "1"
Case 2: ConvertIntoChar = "2"
Case 3: ConvertIntoChar = "3"
Case 4: ConvertIntoChar = "4"
Case 5: ConvertIntoChar = "5"
Case 6: ConvertIntoChar = "6"
Case 7: ConvertIntoChar = "7"
Case 8: ConvertIntoChar = "8"
Case 9: ConvertIntoChar = "9"
Case 10: ConvertIntoChar = "A"
Case 11: ConvertIntoChar = "B"
Case 12: ConvertIntoChar = "C"
Case 13: ConvertIntoChar = "D"
Case 14: ConvertIntoChar = "E"
Case 15: ConvertIntoChar = "F"
Case 16: ConvertIntoChar = "G"
Case 17: ConvertIntoChar = "H"
Case 18: ConvertIntoChar = "I"
Case 19: ConvertIntoChar = "J"
Case 20: ConvertIntoChar = "K"
Case 21: ConvertIntoChar = "L"
Case 22: ConvertIntoChar = "M"
Case 23: ConvertIntoChar = "N"
Case 24: ConvertIntoChar = "O"
Case 25: ConvertIntoChar = "P"
Case 26: ConvertIntoChar = "Q"
Case 27: ConvertIntoChar = "R"
Case 28: ConvertIntoChar = "S"
Case 29: ConvertIntoChar = "T"
Case 30: ConvertIntoChar = "U"
Case 31: ConvertIntoChar = "V"
Case 32: ConvertIntoChar = "W"
Case 33: ConvertIntoChar = "X"
Case 34: ConvertIntoChar = "Y"
Case 35: ConvertIntoChar = "Z"
End Select
End Function
Private Sub Form_Terminate()
End
End Sub
|
|
|
|
 |
|
 |
I realize that you wrote that when you were ten, but since then we now have .ToString and Convert.To... in VS.NET.
For example, a long number which derives from System.Object and overloads .ToString to returns the string version of it. And I use Convert.ToInt64( string ) to get back.
For bin to hex, .NET has System.Byte.ToString( "X" ).PadLeft( 2, '0' ) which will give you a properly aligned hex number.
|
|
|
|
 |
|
 |
how can i read a binary file into binary digits and write those binarydigits into another file
|
|
|
|
 |
|
 |
Open the binary file using the FileStream class, write to a MemoryStream class and then write from the MemoryStream into another FileStream.
You may be able to just write directly from one FileStream to another FileStream.
|
|
|
|
 |
|
 |
i am very new ro the vb.net can you please send me the sample code i will be very greatful to you.
Thanks
|
|
|
|
 |
|
|
 |