473,568 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data Conversion (binary, hex, dec., etc.)

Hi,

Are there any built-in methods in VB.NET that convert one number in one
format to its equivalent in another format?

For example I want to convert the number 162 (decimal) to 10100010 (binary)
or to A2 (hexadecimal) and vice versa.

Thanks.
Nov 21 '05 #1
3 17830
"Amjad" <Am***@discussi ons.microsoft.c om> schrieb:
Are there any built-in methods in VB.NET that convert one number in one
format to its equivalent in another format?

For example I want to convert the number 162 (decimal) to 10100010
(binary)
or to A2 (hexadecimal) and vice versa.


VB provides the functions 'Hex' and 'Oct'. You can use 'CLng' to parse a
string containing a number in dexadecimal or octal format:

\\\
Dim s As String = Hex(222)
MsgBox(s)
Dim i As Integer = CInt("&H" & s)
MsgBox(CStr(i))
///

The same using the .NET FCL:

\\\
Dim s As String = Convert.ToStrin g(222, 16)
MsgBox(s)
Dim i As Integer = Convert.ToInt32 (s, 16)
MsgBox(CStr(i))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2
Thanks.

Is there any built-in methods that convert decimal to BINARY?
Or do I have to write this conversion function myself?

Amjad

"Herfried K. Wagner [MVP]" wrote:
"Amjad" <Am***@discussi ons.microsoft.c om> schrieb:
Are there any built-in methods in VB.NET that convert one number in one
format to its equivalent in another format?

For example I want to convert the number 162 (decimal) to 10100010
(binary)
or to A2 (hexadecimal) and vice versa.


VB provides the functions 'Hex' and 'Oct'. You can use 'CLng' to parse a
string containing a number in dexadecimal or octal format:

\\\
Dim s As String = Hex(222)
MsgBox(s)
Dim i As Integer = CInt("&H" & s)
MsgBox(CStr(i))
///

The same using the .NET FCL:

\\\
Dim s As String = Convert.ToStrin g(222, 16)
MsgBox(s)
Dim i As Integer = Convert.ToInt32 (s, 16)
MsgBox(CStr(i))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
Amjad,
As Herfried stated, you can use the Convert class to do this.

An integer is always stored as an Integer. When you are talking decimal &
binary you are "really" talking about string representations of those
numbers

The second parameter to Convert.ToStrin g & Convert.ToInt32 is the base you
desire.

So to convert an Integer to Binary you would use Convert.ToStrin g(222, 2),
to convert the same integer to Decimal you can use Convert.ToStrin g(222,
10). Of course Decimal is the normal string reprentation of numbers so you
can just as easily use Convert.ToStrin g(222) or 222.ToString().

Hope this helps
Jay
"Amjad" <Am***@discussi ons.microsoft.c om> wrote in message
news:C5******** *************** ***********@mic rosoft.com...
Thanks.

Is there any built-in methods that convert decimal to BINARY?
Or do I have to write this conversion function myself?

Amjad

"Herfried K. Wagner [MVP]" wrote:
"Amjad" <Am***@discussi ons.microsoft.c om> schrieb:
> Are there any built-in methods in VB.NET that convert one number in one
> format to its equivalent in another format?
>
> For example I want to convert the number 162 (decimal) to 10100010
> (binary)
> or to A2 (hexadecimal) and vice versa.


VB provides the functions 'Hex' and 'Oct'. You can use 'CLng' to parse a
string containing a number in dexadecimal or octal format:

\\\
Dim s As String = Hex(222)
MsgBox(s)
Dim i As Integer = CInt("&H" & s)
MsgBox(CStr(i))
///

The same using the .NET FCL:

\\\
Dim s As String = Convert.ToStrin g(222, 16)
MsgBox(s)
Dim i As Integer = Convert.ToInt32 (s, 16)
MsgBox(CStr(i))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4903
by: Dillon Mantle | last post by:
HI All Does any one know how to convert a binary file into a readable (by human) file? Its from a bin file, has addresses and stuff which I can work out... but how do you do the actual conversion? -- Regards Dillon Mantle Different Angle Solutions
21
4506
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
1
6287
by: Ferran Foz | last post by:
Hello, I'm using ADODB.Stream to open a binary file on the server and write it down to the browser using Response.BinaryWrite. It's working fine, but i need to make some changes to the binary data before it is send to the browser. I'm trying to use REPLACE, but it's not finding a string that i know it's in the binary file. Using InstrB...
4
4999
by: Ivan | last post by:
I am running a statically bound COBOL program on DB2 7.1 OS/390 trying to insert EBCDIC strings into a varchar column in a table on DB2 UDB 7.2 FP10a Workgroup edition. The varchar column is defined as FOR BIT DATA which according to the SQL Reference - "Specifies that the contents of the column are to be treated as bit (binary) data. During...
7
1868
by: Drake | last post by:
Well, I'm stuck in legacy land and I need a helping hand. We're trying to give some modern value-added functionality to a circa-1985 fortran proggie. The program produces a binary file, by itself no problem... each record needs to be converted into std::vector<mystruct> I'm having a helluva time with the binary-->(pod)datatype conversion....
16
5106
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the...
2
4355
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. The binary data is how they send numbers (they call it "Big Endian"). I only know at run time whether a byte is going to be text or binary (one of the...
6
4078
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my thesis. I have decided to add the capability to convert words using some sort of lookup algorithm as a sidekick to the "normal" conversion algorithm,...
7
2100
by: JoeC | last post by:
I am trying to create a windows program that reads binary graphics as a resource. This has nothing to do with win32 but conversion of data with memcpy. graphic::graphic(UINT uiResID, HINSTANCE hinstance){ size = 32; bitData.clear(); void * p = NULL; // point to the data int end; BYTE data;
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.