Convert UNICODE to ASCII

290 views
Skip to first unread message

Roberto

unread,
Oct 25, 2016, 5:17:41 PM10/25/16
to Caché, Ensemble, DeepSee
Can someone explain this to me?

I was looking for a way to convert UNICODE to ASCII and found a similar code here in the Cache group:

CONVERTUNICODE(str)
NEW j,strlength
Set returnValue=""
Set strlength=$LENGTH(str)
For j=1:1:strlength {
Set char=$EXTRACT(str,j)
Set char=$A(char)
Set returnValue=returnValue _ $C($ZBOOLEAN(char,64,6))
}

Set returnValue=$EXTRACT(returnValue,2)
Quit returnValue


Can someone explain why this would work?

I've tested with some UNICODE characters and it does work.

-Roberto

David Whitten

unread,
Oct 25, 2016, 5:32:22 PM10/25/16
to intersystems...@googlegroups.com
If I remember correctly, UNICODE is a strict superset of ASCII.
An ASCII character is directly stored as the same bit pattern
as the bottom 7 bits of the UNICODE two byte sequence.
The top 9 bits are all zero for the ASCII in UNICODE bit sequence.

It appears that the UNICODE Cache $EXTRACT, gives back the UNICODE representation of the character, and since it is a number from 0 to 127, when $CHARACTER is given it as an input, it gives back the single byte ASCII character.

Perhaps someone else on the mailing list could explain the significance of $ZBOOLEAN(char,64,6) ... I know 64 is 2 ** 6, but I think ASCII has
7 significant bits (0 thru 127), so I think I am missing something in
that part of the code.

Dave

--
--
Caché, Ensemble, DeepSee

---
You received this message because you are subscribed to the Google Groups "Caché, Ensemble, DeepSee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to intersystems-public-cache+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roberto

unread,
Oct 25, 2016, 5:38:36 PM10/25/16
to Caché, Ensemble, DeepSee, whi...@worldvista.org
I can't seem to find the original post from this group where I found this code, but I remember that Bill from Intersystems says that the code works.

I did test and it does work.

I'm just concerned why are we discarding the first character though.

-Roberto

Roberto

unread,
Oct 25, 2016, 5:44:53 PM10/25/16
to Caché, Ensemble, DeepSee
Reply all
Reply to author
Forward
0 new messages