flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > How to convert from dec to hex or vice versa? |
Author |
|
KIRK 09 Oct 2011, 08:15
I want to do something like this:
Code: rept 256 n { if defined hex(n) db 1 else db 0 end if } or: Code: dc 0FFh macro dc Code { int(Code): db Code } |
|||
09 Oct 2011, 08:15 |
|
AsmGuru62 10 Oct 2011, 17:21
This one is good:
http://www.df.lth.se/~john_e/gems/gem003a.html |
|||
10 Oct 2011, 17:21 |
|
cod3b453 10 Oct 2011, 17:43
Since this is under macros I'll assume you want similiar to this, which displays strings from value:
Code: @DISPLAY_SIGNED equ 0 @DISPLAY_UNSIGNED equ 1 @DISPLAY_GROUP4 equ 4 @DISPLAY_GROUP8 equ 8 @DISPLAY_GROUP16 equ 16 @DISPLAY_GROUP32 equ 32 @DISPLAY_GROUP64 equ 64 @DISPLAY_GROUPNONE equ ((1 shl 63) - 1) @DISPLAY_SCALE10 equ 1000 @DISPLAY_SCALE2 equ 1024 macro @display_newline { display CR,LF } macro @display_bin8 val,group { display 'bin:' repeat 8 display '0'+((val shr (8 - %)) and 0x01) if ((% mod group) = 0) display ' ' end if end repeat } macro @display_bin16 val,group { display 'bin:' repeat 16 display '0'+((val shr (16 - %)) and 0x01) if ((% mod group) = 0) display ' ' end if end repeat } macro @display_bin32 val,group { display 'bin:' repeat 32 display '0'+((val shr (32 - %)) and 0x01) if ((% mod group) = 0) display ' ' end if end repeat } macro @display_dec8 val,type { local number number = (val and 0xFF) if type @display_dec64 number,type else if (number and 0x80) number = (number or 0xFFFFFFFFFFFFFF00) end if @display_dec64 number,type end if } macro @display_dec16 val,type { local number number = (val and 0xFFFF) if type @display_dec64 number,type else if (number and 0x8000) number = (number or 0xFFFFFFFFFFFF0000) end if @display_dec64 number,type end if } macro @display_dec32 val,type { local number number = (val and 0xFFFFFFFF) if type @display_dec64 number,type else if (number and 0x80000000) number = (number or 0xFFFFFFFF00000000) end if @display_dec64 number,type end if } ; ; @display_dec64 adapted from macro by revolution 2011 ; macro @display_dec64 val,type { local number local leading_zero local digit local divisor display 'dec:' number = (val and 0xFFFFFFFFFFFFFFFF) if type if number < 0 number = number - 10000000000000000000 if number < 0 number = number + 10000000000000000000 display '9' else display '1' end if end if end if if number = (1 shl 63) display '-9223372036854775808' else if number < 0 number = -number display '-' end if leading_zero = 0 divisor = 1000000000000000000 while divisor > 0 digit = number / divisor leading_zero = leading_zero + digit if leading_zero | (divisor = 1) display digit+'0' number = number - (digit * divisor) end if divisor = divisor / 10 end while end if } macro @display_dec_scaled val,scale { __display_scale equ 0 ,\ 'k',\ 'M',\ 'G',\ 'T',\ 'P',\ 'E',\ 'Z',\ 'Y' local number local index local suffix number = val index = 0 if (scale = @DISPLAY_SCALE10) if number < 0 number = number - 10000000000000000000 if number < 0 number = number + 10000000000000000000 end if number = (number / 1000) + 10000000000000000 index = index + 1 end if else end if while (number > scale) number = number / scale index = index + 1 end while @display_dec64 number,@DISPLAY_SIGNED virtual at 0 db __display_scale load suffix byte from index end virtual display suffix } macro @display_hex8 val,group { local .x display 'hex:' repeat 2 .x = (val and (0x0F shl ((2 - %) shl 2))) shr ((2 - %) shl 2) or '0' if .x > '9' .x = .x + 'A' - ':' end if display .x if (((% * 4) mod group) = 0) display ' ' end if end repeat } macro @display_hex16 val,group { local .x display 'hex:' repeat 4 .x = (val and (0x0F shl ((4 - %) shl 2))) shr ((4 - %) shl 2) or '0' if .x > '9' .x = .x + 'A' - ':' end if display .x if (((% * 4) mod group) = 0) display ' ' end if end repeat } macro @display_hex32 val,group { local .x display 'hex:' repeat 8 .x = (val and (0x0F shl ((8 - %) shl 2))) shr ((8 - %) shl 2) or '0' if .x > '9' .x = .x + 'A' - ':' end if display .x if (((% * 4) mod group) = 0) display ' ' end if end repeat } macro @display_hex64 val,group { local .x display 'hex:' repeat 16 .x = (val and (0x0F shl ((16 - %) shl 2))) shr ((16 - %) shl 2) or '0' if .x > '9' .x = .x + 'A' - ':' end if display .x if (((% * 4) mod group) = 0) display ' ' end if end repeat } macro @display_timestamp { local .t local .t_s local .t_m local .t_h .t = %t .t_s = .t mod 60 .t_m = ((.t - .t_s) / 60) mod 60 .t_h = ((.t - (.t_m * 60) - .t_s) / 3600) mod 24 display '0'+(.t_h / 10),'0'+(.t_h mod 10) display ':' display '0'+(.t_m / 10),'0'+(.t_m mod 10) display ':' display '0'+(.t_s / 10),'0'+(.t_s mod 10) } Code: macro @convert_hex8 dst,val { local .x repeat 2 .x = (val and (0x0F shl ((2 - %) shl 2))) shr ((2 - %) shl 2) or '0' if .x > '9' .x = .x + 'A' - ':' end if store byte .x at dst+(%-1) end repeat } my_str dq 0 @convert_hex8 my_str,0xFF |
|||
10 Oct 2011, 17:43 |
|
AE 12 Nov 2022, 09:29
Madis731 wrote: Strange that I couldn't find something this simple on the board. And there's one for Unicode, too? |
|||
12 Nov 2022, 09:29 |
|
revolution 12 Nov 2022, 09:49
AE wrote: And there's one for Unicode, too? Do you want UTF-8 encoding. Or UTF-16 (WTF-16)? UCS2? UTF-32? |
|||
12 Nov 2022, 09:49 |
|
AE 12 Nov 2022, 10:03
I mean that with include 'win64w.inc' it will not work
|
|||
12 Nov 2022, 10:03 |
|
revolution 12 Nov 2022, 10:07
That would be widechar in MS parlance.
So you need to store two bytes per character. Code: ;... mov ecx,15*2 mov word[strbuf+32],0 ;... mov [strbuf+rcx],dx sub ecx,2 ;... |
|||
12 Nov 2022, 10:07 |
|
AE 12 Nov 2022, 11:15
revolution wrote: So you need to store two bytes per character. Thanks! |
|||
12 Nov 2022, 11:15 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.