473,434 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

math conversions

Hello,
Are there any classes that do the conversions from
hex2Dec(), dec2Hex()

decimal, hex, octal ..and back to a Base
or do i have to implement them myself based
on existing conversion utilities in Net

Thanks
MJ

Jun 21 '07 #1
4 5345
Mike-

I'm not sure about an actual method for it, but Hex-Dec, you can use the
Int32.Parse method.

string hexString = "000a01af";
int intOutput = Int32.Parse(hexString, NumberStyles.HexNumber);

NumberStyles is part of the System.Globalization namespace.

For Dec -Hex, a String.Format should work in most cases (using integer
from above) :

UInt32 unicodeInt = Convert.ToUInt32(intOutput);
string hexOutput = String.Format("{0:x2}", unicodeInt);

Octal and others... I'm not sure, but I'd assume _someone_ has come up with
the libraries or at least has posted the source code somewhere.

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

MHello,
MAre there any classes that do the conversions from
Mhex2Dec(), dec2Hex()
Mdecimal, hex, octal ..and back to a Base
Mor do i have to implement them myself based
Mon existing conversion utilities in Net
MThanks
MMJ
Jun 21 '07 #2

1) for hex2Dec()
look at Convert.ToInt32(value, fromBase)
http://msdn2.microsoft.com/en-us/library/1k20k614.aspx

int i = Convert.ToInt32 ("A", 16);
// i is now 10

2) for dec2Hex()
look at Int32.ToString(format)
http://msdn2.microsoft.com/en-us/library/8wch342y.aspx

string s = 10.ToString("X")
// s is now "A"

Regards, Petar Repac
MikeJ wrote:
Hello,
Are there any classes that do the conversions from
hex2Dec(), dec2Hex()

decimal, hex, octal ..and back to a Base
or do i have to implement them myself based
on existing conversion utilities in Net

Thanks
MJ
Jun 21 '07 #3
Thank you much
MJ

"MikeJ" <ve***********@sbcglobal.netwrote in message
news:NO*****************@newssvr22.news.prodigy.ne t...
Hello,
Are there any classes that do the conversions from
hex2Dec(), dec2Hex()

decimal, hex, octal ..and back to a Base
or do i have to implement them myself based
on existing conversion utilities in Net

Thanks
MJ

Jun 21 '07 #4
MikeJ wrote:
Are there any classes that do the conversions from
hex2Dec(), dec2Hex()

decimal, hex, octal ..and back to a Base
or do i have to implement them myself based
on existing conversion utilities in Net
A very general approach:

private static string DIGITS = "0123456789ABCDEF";
private static int FromAny(string s, int radix)
{
int res = 0;
char[] sa = s.ToCharArray();
for (int i = 0; i < s.Length; i++) {
res = res * radix + DIGITS.IndexOf(sa[i]);
}
return res;
}
private static string ToAny(int i, int radix)
{
string res = "";
int tmp = i;
while (tmp 0) {
res = DIGITS.ToCharArray()[tmp % radix] + res;
tmp = tmp / radix;
}
return res;
}
public static int FromDec(string s)
{
return FromAny(s, 10);
}
public static string ToDec(int i)
{
return ToAny(i, 10);
}
public static int FromHex(string s)
{
return FromAny(s, 16);
}
public static string ToHex(int i)
{
return ToAny(i, 16);
}
public static int FromBin(string s)
{
return FromAny(s, 2);
}
public static string ToBin(int i)
{
return ToAny(i, 2);
}

Arne
Jul 5 '07 #5

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

Similar topics

3
by: Reneé | last post by:
I wanted to know the order of implicit conversions and which sort of values allow them. From searching around in books and the archive of this mailing list, it seems to be that only numbers are...
16
by: Frank Millman | last post by:
Hi all I was helping my niece with her trigonometry homework last night. Her calculator's batteries were flat, so I thought I would use Python's math module to calculate sin, cos, and tan. I...
5
by: John Fouhy | last post by:
Can someone explain these differences? >>> from math import pi >>> pi 3.1415926535897931 >>> type(pi) <type 'float'> >>> repr(pi) '3.1415926535897931' >>> str(pi)
3
by: Christian Brechbühler | last post by:
Let me use complex numbers as a familiar example. The following is taken verbatim from Bjarne Stroustrup, "The C++ Programming Language", except the include line and the "no match" comments. It...
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
10
by: vb | last post by:
Hi all, I am a newbie in C and i want to know what all pointer conversions are "legal" according to ANSI C standard. For Example, int* to char*, some_struct* to char* and so on .. According to...
7
by: Mark Healey | last post by:
Do the trig functions in math.h work in degrees, radians or what? For some reason it doesn't say which in "man math.h" IIRC the arctan of a slope gives the angle. So, shouldn't atanf((float)1)...
16
by: Bill Nguyen | last post by:
I'm running into a very weird problem regarding subtraction. Subtraction behaves as if it's an addition in the below sub txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0") ...
2
by: star01daisy | last post by:
This is what the assignment says to do: Write a C++ program to do decimal-binary number conversions. The program gives the user a choice of conversion type (binary to decimal or decimal to binary)....
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.