News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Quick question about number conversion

Started by Lightman, November 18, 2015, 07:25:25 PM

Previous topic - Next topic

Lightman

Hi Everybody,

A quick question about displaying numbers, or rather, converting them to strings. Consider the following code...


invoke SendMessage, hComboBox, CB_GETCURSEL, 0, 0


This function is  going to return the Combobox's current cursor position as an integer in eax. I'd like to print this number to a widget on the screen, but if I send it out to a text box it won't work...


invoke      SendMessage, hEdit, WM_SETTEXT, eax, 0


It won't work, as it's not a printable character. Is there a API call to do conversions like this?

Regards,

Lightman
Regards,

Lightman

ragdog

Hi

You can use simply wsprintf and %d for display it or other Api
or look in the Masm32 lib


invoke    wsprintf, addr sTempBuf, chr$ ("%d"), eax
invoke    SendMessage, hWnd, WM_SETTEXT, 0,addr sTempBuf

jj2007

Ragdog's solution will work. Try also invoke SendMessage, hEdit, WM_SETTEXT, str$(eax), 0

ragdog

Hello Jochen

invoke SendMessage, hEdit, WM_SETTEXT, 0, str$(eax)  :t

Lightman

Hi Everybody,

Thank you for the prompt replies, I've tried to implement both examples into my code but I an unable to get it to compile. Am I missing an inc or a lib?

Just for background, I'm planning a small application that would rely heavily on Combo Boxes. I wrote the attached package to practice interaction with Combo Boxes and be sure I understood them. Mmm, looks like I don't.

As you can see, I use the SendMessage function to return the index and save it off for future use. I planned to show the current index in a  editbox or on a message box. Not much use with a combo box, bit it did raise the question of how to convert an integer stored in a register to a string value I can write out. I'm bound to need to do that at some point.

Regards,

Lightman. 
Regards,

Lightman

ragdog

Hello

I use the Chr$ macro you must include

Quoteinclude   \masm32\macros\macros.asm

By jj2007 example must you include
Quote
include    \masm32\macros\macros.asm
include    masm32.inc
includelib masm32.lib

Thats all :t

Siekmanski

 edit: sorry.... have misread it. My answer was wrong.
Creative coders use backward thinking techniques as a strategy.

jj2007

Quote from: ragdog on November 18, 2015, 09:47:55 PM
By jj2007 example must you include
Quote
include    \masm32\macros\macros.asm
include    masm32.inc
includelib masm32.lib

That will work only if the include and lib folders are in the PATH environment variable, which is a messy exercise. Much better is to replace all that include stuff with a single line:

include \masm32\include\masm32rt.inc

This makes sure that the standard macros and libraries are included. Only on rare occasions, you may see a "symbol not found" error, in which case you need to search \Masm32\include and add (for example) uselib advapi32 under the first line.

Lightman

Hi Everybody,

Yay! I added the line to my .inc file and it compiled and worked! Thanks guys.

Taking a quick look at the file, it appear to load in all the incs 'n' libs I need. So I can go from...


include windows.inc
include user32.inc
include kernel32.inc
include shell32.inc
include comctl32.inc
include comdlg32.inc

includelib user32.lib
includelib kernel32.lib
includelib shell32.lib
includelib comctl32.lib
includelib comdlg32.lib


...to...


include \masm32\include\masm32rt.inc


Regards,

Lightman
Regards,

Lightman

dedndave

Quote from: Lightman on November 18, 2015, 09:36:50 PM
...it did raise the question of how to convert an integer stored in a register to a string value I can write out...

i would guess these indexes are probably unsigned integers

you can use the "uhex$" macro to display it as hexadecimal
you don't have to use the macro, though - "uhex$" uses the "dw2hex" function (masm32 library)

the "str$" macro will convert signed integers to decimal
"str$" uses "dwtoa" (masm32 library)

the "ustr$" macro will convert unsigned integers to decimal
"ustr$" uses "crt_ultoa" (ANSI, msvcrt library) or "crt__ultow" (UNICODE, msvcrt library)

the "sstr$" macro will convert signed integers to decimal
"ustr$" uses "crt_ltoa" (ANSI, msvcrt library) or "crt__ltow" (UNICODE, msvcrt library)

the msvcrt functions have a variable radix
this value can be set to 10 for decimal, 8 for octal - whatever base you like from 2 to 36

dedndave

the latest version of masm32 has a little problem with the ustr$ macro
i modified my copy of macros.asm to correct the issue

http://masm32.com/board/index.php?topic=1811.msg33370#msg33370

CCurl

Here is my code to convert a number into a string. It is the implementation for the "." word in my Forth. The BASE is in EBX, so it can convert to DECIMAL, HEX, BINARY, whatever. The loop at doDiv does the work. To write it to a buffer, you would replace the m_PUSH with code that stores DL (the converted digit) to the buffer. Note that it works "right to left" if you will, so the first digit out is rightmost, and the last digit out is the leftmost.

My implementation always follows the output with a space, hence the m_PUSH 20h in the beginning.


; ---------------------------------------------------------------------------------------------------------
; ** TESTED **
fDOT proc

m_Pop eax
mov ebx, var_BASE
m_Push 20h
xor edx, edx
mov ecx, 1

; is this a negative number?
push edx
test eax, eax
jns doDiv
pop edx ; yes, it is negative
inc edx
push edx
xor eax, 0ffffffffh
inc eax
doDiv: xor edx, edx ; DIV uses EDX:EAX
div ebx ; EAX gets quotient, EDX gets remainder
.IF edx < 10
add edx, 48
.ELSE
add edx, 55
.ENDIF
m_Push edx ; Push the digit
inc ecx ; Number of digits
test eax, eax
jnz doDiv

pop edx ; Start negative number with a '-'
test edx, edx
jz doOut
m_push '-'
inc ecx

doOut: call fEMIT
loop doOut
ret

fDOT endp