Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

  • *Experts*
Posted

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

"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
  • 7 years later...
Posted

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)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...