flat assembler
Message board for the users of flat assembler.

Index > Windows > How to convert Integer to a String?

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Hello everyone. I'm interesting if there's something to convert Integer to a String without ITOA function from MSVCRT ? In assembly style or other library with STDCALL ? Smile Thanks.
Post 25 Sep 2010, 09:50
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Check out my win32 utility - BitBox
http://board.flatassembler.net/topic.php?t=11615

It has routines for the following conversions:

binary to string
decimal to string
hexadecimal to string

string to binary
string to decimal
string to hexadecimal

Note: they are not the safest or most robust routines.
Mainly because of under\overflow checking is not performed.
I have an updated version with more features i will upload soon.

Also if you search around there are routines which do all 3 in one proc.

Have fun Smile
Post 25 Sep 2010, 10:04
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Mate, I'm starter I dont understand lot of code from that code.. I need just simple code Smile Thank you.
Post 25 Sep 2010, 10:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17662
Location: In your JS exploiting you and your system
revolution
Overflowz: windwakr already gave you the code:

http://board.flatassembler.net/topic.php?p=119655#119655
Post 25 Sep 2010, 10:23
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Yes I know but isn't there more easy way ? Smile
Post 25 Sep 2010, 10:51
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
If not, can someone rewrite that without error messages and things like that.. just MAIN functions what will convert integer to string. I'll learn that things later. thank you. ! Smile
Post 25 Sep 2010, 11:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17662
Location: In your JS exploiting you and your system
revolution
Overflowz wrote:
... but isn't there more easy way ? Smile
No. Progressive reduction by the base and then storing remainders in reverse order is about as simple as it gets. There are other much faster methods but they would be even more complex.
Post 25 Sep 2010, 11:06
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
never mind. just little example, not so much >.<
Post 25 Sep 2010, 11:52
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
BTW, do you really want itoa functionality or just simple, homework-like, convert to decimal (i.e. base 10) string?
Post 25 Sep 2010, 14:41
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Just convert so I can display that in MessageBox API Smile Thanks.
Post 25 Sep 2010, 16:26
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
Nobody knows ? Smile
Post 26 Sep 2010, 10:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17662
Location: In your JS exploiting you and your system
revolution
Nobody cares. There are so many examples already on this board you can just search to find one to your liking.

Besides, we don't much fancy doing your homework for you. Wink
Post 26 Sep 2010, 10:47
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
I dont have any homework it's just for interest. Can you write the EASY Examples of that code what you're saying ?
Post 26 Sep 2010, 11:04
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
First to have decimal to convert...
Code:
g_value dd 1234    

Then to have a buffer for string...
Code:
g_buffer rb 256    

Now to have conversion routine...
Code:
dec2str:
        ; EAX = value
        ; EDI = buffer
        mov     ecx,10
    .stack_dec:
        xor     edx,edx
        div     ecx
        add     edx,'0'
        push    edx
        test    eax,eax
        jz      .purge_dec
        call    .stack_dec
    .purge_dec:
        pop     dword[edi]
        inc     edi
        ret    

Then to format number into buffer...
Code:
mov eax,[g_value]
mov edi,g_buffer
call dec2str
    

Then to display results...
Code:
invoke MessageBox,0,g_buffer,0,0    

Are you able to integrate into your own app, or do you need full example?


Last edited by bitshifter on 26 Sep 2010, 19:19; edited 1 time in total
Post 26 Sep 2010, 11:46
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless
i hope this helps, i re-wrote it in Delphi, almost porting the asm code line by line to Delphi.
if u want it in C just ask
Code:
{
Base (var) = ecx
Number (var) = eax
div instruction in performs Unsigned division of edx:eax by
  r/m32 (ecx in out case), with result stored in
  eax = Quotient
  edx = Remainder
so
 Reminder (var) = edx
}

function Dec2Str(Number, Base: Integer):string;
var
 Reminder: Integer;
 i: Integer;
begin
 i := 1;
 while Number <> 0 do
 begin
  Reminder := Number mod Base;  //store the reminder of the division in Reminder = edx
  Number := Number div Base;    //eq. to "div ecx", in delphi 2 steps needs, no function for division and reminder at the same time
  Inc(Reminder, Ord('0'));     //eq. to " add edx, '0' ", add the POSITION of zero in the ascii table to each reminder to get its ASCII equivalent
  SetLength(Result, I);        //Delphi Stuff.....
  Result[i] := chr(Reminder);  //convert ASCII code to its Char equivalent
  Inc(i);                      //More Delphi Stuff.....
 end;
 Result := ReverseString(Result); //works the same way as " pop dword[edx] ", since stack follows LIFO, reversing is required
end;

//Usage:
// _String := Dec2Str(2588, 10);
    


take a look at the ascii table here to know what " add edx, '0' " means
http://www.asciitable.com/
Post 26 Sep 2010, 16:23
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
bitshifter thank you!! No, I was just interested how to do that.. nothing special Razz and btw is there any way to do without "stdcall blabla" ? Razz ty =D
Nameless I dont know delphi but thanks for reply anyway. Smile
Post 26 Sep 2010, 18:56
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: Paradise Falls
Picnic
Simple itoa & atoi 16-bit implementations along with a whole bunch of other routines can also be found inside MBASIC source code.
Post 28 Sep 2010, 08:30
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
simple right ?.. mate its hard for me I'm starter >.< and btw I need in 32bit mode Razz very small example on the world Very Happy
Post 28 Sep 2010, 09:45
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1608
Location: Ukraine
shoorick
Code:
proc fitoa num, string

    local buf: rb 10

    finit
    fild [num] ; load integer
    fbstp tword [buf] ; store packed bcd

    mov ecx,9 ; counter: 18 digits - 9 bytes
    mov edx,[string] ; pointer to the string to fill
    cmp [buf + ecx],0 ; if highest byte is not null - negative number
    je  @F
    mov byte [edx],"-" ; then set "-" at the string start
    inc edx
@@:    
    mov al,[buf + ecx - 1] ; get bytes in backward direction (higher bytes have higher address
    mov ah,al ; split packed bcd in al to two unpacked in al/ah
    shr al,4
    and ah,15
    add ax,3030h ; add 30h to each number to get ascii code
    mov [edx],ax ; store couple of digits into string
    inc edx ; get next string address
    inc edx
    loop @B ; get next offset in buf, if 0 - done
    mov [edx],cl ; set end of string 
    ret

endp    
simple decoding, but long unpacking packed bcd to unpacked Wink


Last edited by shoorick on 28 Sep 2010, 13:31; edited 1 time in total
Post 28 Sep 2010, 10:36
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz
fbstp! Thank you but can you write with comments and how to use ? Razz Thanks..
Post 28 Sep 2010, 13:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.