predseda Posted June 27, 2004 Posted June 27, 2004 255(dec) = FF(hex) = 11111111(BIN) I have a decimal number (Dim i As Byte / Integer) an i need to convert it to string and transform it to hex or binary. I know how to do this manually, but i think in .NET are implemented instructions for tihs. Example: Dim i As Byte = 255 Dim str As String str = i.ConvertToHEX ' How to do this? Console.WriteLine str ---- Output : FF Thanx predseda Quote
Administrators PlausiblyDamp Posted June 27, 2004 Administrators Posted June 27, 2004 Dim i As Integer = 255 Dim s As String s = i.ToString("X") Console.Writeline(s) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
predseda Posted June 27, 2004 Author Posted June 27, 2004 Thanks a lot PlausiblyDamp, s = i.ToString("X") So this is hexadecimal number system, what about Binary? --- And next situation, convert string with hex/binary value to integer variabile Dim str As String = "FF" Dim int As Integer int = Convert_To_Integer_From_HEX(str) Thanks predseda Quote
*Experts* Nerseus Posted June 28, 2004 *Experts* Posted June 28, 2004 To convert from/to arbitrary bases, use Convert: int i = 33; string binary = Convert.ToString(i, 2); string hex = Convert.ToString(i, 16); int binaryToInt = Convert.ToInt32(binary, 2); int hexToInt = Convert.ToInt32(hex, 16); Though I prefer the ToString method to convert an int to hex, when needed. I would guess that internally, the ToString method for an int is using Convert.ToString behind the scenes so it might be a bit faster. I'd go with whatever seems easier to read/understand for you. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
predseda Posted June 29, 2004 Author Posted June 29, 2004 Yes...! This is exactly what I need. Thanks a lot Nerseus. predseda Quote
VBExpert Posted October 18, 2011 Posted October 18, 2011 An easier way to do convert to hex would be to use the Conversion.Hex() function: C#: int i = 65; string hex = Conversion.Hex(i); VB: Dim i As Integer = 65 Dim hex As String = Conversion.Hex(i) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.