News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

convert a decimal number into a binary number.

Started by supernicky, June 23, 2011, 12:21:10 PM

Previous topic - Next topic

supernicky

hello,

I want to convert a decimal number into a binary number.
for example:

9.5 in a 32 bit binary number.

this number is to be read by a editbox-control.
maybe someone can help me.

thanks and greetings

nicky

dedndave

hi Nicky - welcome to the forum

what you probably want is a floating point value, or REAL4
a 32-bit binary integer has no place for digits after the decimal point

you can define 4-byte floating point values using REAL4...
fVal1   REAL4   9.5

now, things can be simpler if all your values have a single digit after the decimal point
you can multiply them all by 10 and store them as integers   :bg

supernicky

hello back

I'm from Germany and I have to apologize for my english.

I work with the masm32 package. of real4 I have not heard or read: (

The declaration is always done with the data-type dd
for example:

number1 dd 3.54

I would like to understand is how a decimal (3.54) represented in binary.

I've created this one editbox in a decimal number to be entered.
then I need to process the input and convert binary.

32bit is too small? I should take 80bit?

how do I convert a binary number from 3.54?

I'm no math genius

thanks and greetings, nicky

dedndave

you are dealing with floating point values because there are digits beyond the decimal point
the format is one of a few that are used by the FPU
you can do a lot of math with the FPU   :P
at first, it may seem overwhelming, but it isn't that difficult to learn
Ray has a good tutorial
the tutorial may be found in the masm32\tutorial\fputute folder
however, i think Ray has updated it since the masm32 package was put together
he also has a nice floating point library
for the most recent version, visit his site...
http://www.ray.masmcode.com/

your English is great   :bg

to read a value from the edit box, you will want to convert an ASCII numeric float string into real4 format
i think Ray's library has a function to do that

qWord

hi,
you can use CRT function crt_sscanf for reading an floatingpoint value in a string. However, using the macros a2r4() and a2r8() for converting is easier (these macros use sscanf):
...
myreal REAL4 ?
sz db "1234.5",0
...
mov eax,a2r4(ADDR sz)
m2m myreal,REAL4 ptr [eax]

(the macros return a pointer to a REAL4/REAL8 variable)
FPU in a trice: SmplMath
It's that simple!

baltoro

Hi, SUPERNICKY,   
In one of my programs, I did something similar. qWord's approach is the best,...using the a2r4 macro.
In my program, I had several routines. It was overkill, but, I'm a novice, and I wanted it to be reliable.
I had a routine to check the that the value entered in the edit box was actually a string in the correct decimal format. This is important, because, a2r4 will fail, and, you won't know why. The second routine, I actually adapted from the atodw MASM routine. My routine is seriously bloated, but, returns about a dozen different error codes if the routibne fails, so you can determine why the code doesn't work correctly,...otherwise it returns a pointer to a REAL 4.
Baltoro