The MASM Forum

General => The Campus => Topic started by: Ar0n on July 11, 2015, 07:08:45 AM

Title: How to send a byte array to a richedit control
Post by: Ar0n on July 11, 2015, 07:08:45 AM
so basically what I want is to send a byte array to a richedit control, currently I send a string by using EM_REPLACESEL message but the SendMessage function takes a null-terminated string, I want this control to show me a dump of a file.

Does anyone know a way to do this?  Thanks.
Title: Re: How to send a byte array to a richedit control
Post by: jj2007 on July 11, 2015, 11:27:23 AM
You need to translate the byte array to a string of hex values:

include \masm32\include\masm32rt.inc

.data?
buffer db 1000 dup(?)

.code
txBytes db "Just a test", 0, "with an embedded nullbyte", 0

start:
  ; method A:
  invoke bin2hex, addr txBytes, sizeof txBytes, addr buffer ; use somefiletobuffermacro here
  print offset buffer, 13, 10, 10

  ; method B:
  mov ebx, sizeof txBytes
  mov esi, offset txBytes
  mov edi, offset buffer
  push edi
  .Repeat
lodsb
movsx eax, al
push uhex$(eax) ; Masm32 macro
pop edx
mov eax, [edx+6]
stosb
xchg al, ah
stosb
mov al, " " ; for readability
stosb
dec ebx
  .Until Sign?
  pop edi
  print edi, 13, 10
  exit

end start


Output:
4A 75 73 74 20 61 20 74 - 65 73 74 00 77 69 74 68
20 61 6E 20 65 6D 62 65 - 64 64 65 64 20 6E 75 6C
6C 62 79 74 65 00

4A 75 73 74 20 61 20 74 65 73 74 00 77 69 74 68 20 61 6E 20 65 6D 62 65 64 64 65 64 20 6E 75 6C 6C 62 79 74 65 00 68 00
Title: Re: How to send a byte array to a richedit control
Post by: Ar0n on July 11, 2015, 12:42:35 PM
Hello  jj2007, Thanks for your reply but I would like to see the result in characters, I think it's more readable.
Title: Re: How to send a byte array to a richedit control
Post by: jj2007 on July 11, 2015, 05:06:36 PM
OK. Technically speaking, a zero-delimited string finishes where there is a zero byte. Logical, right? So just go through the byte array and replace all zeroes with "." or "_".
Title: Re: How to send a byte array to a richedit control
Post by: Ar0n on July 11, 2015, 05:22:05 PM
that seems like a good idea, thank you.  :biggrin:
Title: Re: How to send a byte array to a richedit control
Post by: Tedd on July 14, 2015, 01:24:50 AM
It may be worth 'sanitising' any bytes below 20h -- there are control characters and shouldn't be printed, especially BEL, TAB, LF, CR.