Author Topic: How print UTF8 char  (Read 25357 times)

Offline Jutro

  • Jr. Member
  • *
  • Posts: 4
How print UTF8 char
« on: September 01, 2015, 12:13:09 PM »
Hello,
I have one integer variable. How writing UTF8 char coresponding integer value.

A=41
Print A
Return character number 41 in unicode table.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How print UTF8 char
« Reply #1 on: September 01, 2015, 09:25:41 PM »
Dunno. My understanding is that UTF8 characters with the high bit clear are the same as ASCII, so 'A' may not be a problem. My understanding is that if the high (sign?) bit is set in the first byte, the number of set bits (not the value) indicates the number of following bytes in the UNICODE encoding (little endian?). There's some info in the Friendly Manual - http://www.nasm.us/doc/nasmdoc3.html#section-3.4.5
- but it does not seem too helpful. I strongly suspect "what OS?" makes a difference... Sorry I can't help you more.

Best,
Frank


Offline Jutro

  • Jr. Member
  • *
  • Posts: 4
Re: How print UTF8 char
« Reply #2 on: September 02, 2015, 07:21:58 PM »
I need linux code.
I need procedure to covert integer to UTF8
Your link is in situation when i create string.

Look this http://www.fileformat.info/info/unicode/char/2692/index.htm

UTF-16 (decimal)   9 874

UTF-8 (hex)   0xE2 0x9A 0x92 (e29a92)

How covert integer a= 9874 to string UTF8

Im beginer in nasm

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: How print UTF8 char
« Reply #3 on: September 03, 2015, 03:50:33 PM »
I need linux code.
I need procedure to covert integer to UTF8

So you want to be spoon-fed code?  Google is your friend.

Im beginer in nasm

Nothing wrong with that.  Everyone starts out that way.  But to gain knowledge and experience requires effort.  Show us what you've got and we'll work with you.

Offline Jutro

  • Jr. Member
  • *
  • Posts: 4
Re: How print UTF8 char
« Reply #4 on: September 07, 2015, 02:07:12 PM »
alle example showing hello word
string not one char
adressing string and register are different.
I need some help.
If this is that easy erite

Offline I8086

  • New Member
  • Posts: 1
Re: How print UTF8 char
« Reply #5 on: September 10, 2015, 10:04:13 PM »
Wikipedia is a good place to start. https://en.wikipedia.org/wiki/UTF-8#Examples

Offline Jutro

  • Jr. Member
  • *
  • Posts: 4
Re: How print UTF8 char
« Reply #6 on: September 19, 2015, 11:29:21 AM »
Wikipedia is a good place to start. https://en.wikipedia.org/wiki/UTF-8#Examples
Im not have troouble with utf i have troouble with integer.
How print integer data to char data and show ?
Linux

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How print UTF8 char
« Reply #7 on: September 19, 2015, 01:50:05 PM »
Hi Jutro,

Ah, just printing an integer is something I can help you with. For 32-bit Linux, at least...

This is a very simple-minded one I use for debugging purposes, mostly.
Code: [Select]
;---------------------------------
showeaxd:
    push eax
    push ebx
    push ecx
    push edx
    push esi
   
    sub esp, 10h
    lea ecx, [esp + 12]
    mov ebx, 10
    xor esi, esi
    mov byte [ecx], 0  ; zero-teminated - we don't reall use this
.top:   
    dec ecx
    xor edx, edx
    div ebx
    add dl, '0'
    mov [ecx], dl
    inc esi
    or eax, eax
    jnz .top
   
    mov edx, esi
    mov ebx, 1
    mov eax, 4
    int 80h
   
    add esp, 10h

    pop esi
    pop edx
    pop ecx
    pop ebx
    pop eax

    ret
;---------------------------------

Here's one the late Chuck Crayne showed me. It just puts the characters in a buffer - doesn't print them. (which means it can be used in Windows too) It can do right-justified numbers - looks nice if you're printing a column of numbers...
Code: [Select]
; from Chuck Crayne - RIP, Chuck.
;convert binary to ascii
;call with eax = signed binary number
; esi = address of output string
; ecx = length of output string
;returns esi = 1st printed digit
; ecx = no of digits printed (includes sign if any)
; other registers preserved
binasc: push edx
push ebx
push edi
push eax
mov edi,esi ;save start of string
ba1: mov byte [esi],' ' ;fill string with blanks
inc esi
loop ba1
mov ebx,10 ;initialize divisor
or eax,eax ;value negative?
jns ba2 ;no problem
neg eax ;make it positive
ba2: xor edx,edx ;clear high part of dividend
div ebx ;divide by 10
add dl,'0' ;convert to ascii digit
dec esi ;step backwards through buffer
mov [esi],dl ;store digit
inc ecx
cmp esi,edi ;out of space
jz ba4 ;yes - quit
or eax,eax ;all digits printed?
jnz ba2 ;no - keep trucking
pop eax ;get original value
or eax,eax ;negative?
jns ba3 ;no - quit
dec esi ;place for sign
mov byte [esi],'-'
inc ecx ;add to char count
ba3: pop edi
pop ebx
pop edx
ret
ba4: pop eax
jmp ba3
;-------------------
There's also "just call printf" which some people prefer...

Here's a routine to get a number (characters) from the user and convert to integer, if you need it.
Code: [Select]
;--------------------
atoi:
    mov edx, [esp + 4]  ; pointer to string
    xor eax, eax        ; clear "result"
.top:
    movzx ecx, byte [edx]
    inc edx
    cmp ecx, byte '0'
    jb .done
    cmp ecx, byte '9'
    ja .done
   
    ; we have a valid character - multiply
    ; result-so-far by 10, subtract '0'
    ; from the character to convert it to
    ; a number, and add it to result.
   
    lea eax, [eax + eax * 4]
    lea eax, [eax * 2 + ecx - '0']

    jmp short .top
.done:
    ret
;------------------------

If you have trouble seeing how to use these, just ask. Got hex version, too.

I've been meaning to experiment with UTF-8, too, but haven't gotten around to it yet. Maybe I never will...

Best,
Frank