Author Topic: How to convert UTF-16 <--> ascii ?  (Read 3401 times)

0 Members and 1 Guest are viewing this topic.

hunterxyz

  • Guest
How to convert UTF-16 <--> ascii ?
« on: September 10, 2008, 06:47:41 PM »


How convert UTF-16 "\U+73AF" to ascii code ?
How convert ascii "185" to UTF-16 code ?

Thank you!

Jeff_M

  • King Gator
  • Posts: 4100
  • C3D user & customizer
Re: How to convert UTF-16 <--> ascii ?
« Reply #1 on: September 10, 2008, 08:01:28 PM »
Now you did it! You made me go look into this, and after reading THIS my head hurts.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to convert UTF-16 <--> ascii ?
« Reply #2 on: September 10, 2008, 09:25:34 PM »
Isn't that just a Hex to Decimal conversion & back the other way?
Base 16 to base 10, right.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Jeff_M

  • King Gator
  • Posts: 4100
  • C3D user & customizer
Re: How to convert UTF-16 <--> ascii ?
« Reply #3 on: September 11, 2008, 12:49:35 AM »
That's what I thought, too, Alan. But after reading that articale and a few other sites, I think there's more to it than that. But I've got so many other things going on right now, I may just be making it seem more difficult than it is. Perhaps someone in the know will be able to chime in. 

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to convert UTF-16 <--> ascii ?
« Reply #4 on: September 11, 2008, 09:42:09 AM »
Here are two offerings:
You will need to strip the "\U+" from the string.

The Hex2Dec is a rework of gile's code from  here.
Code: [Select]
;;  CAB 09.11.08
;;  Convert Hexadecimal string to integer
(defun Hex2Dec (str / n)
  (if (zerop (setq n (strlen str)))
    0
    (+
      (*
       (cond
         ((< "@" (setq let (strcase (substr str 1 1))) "G")(- (ascii let) 55))
         ((< "/" (setq let (strcase (substr str 1 1))) ":")(atoi let))
       )
       (expt 16 (1- n)))
      (Hex2Dec (substr str 2))
    )
  )
)

;;  CAB 09.11.08
;;  Convert integer to Hexadecimal string
(defun Dec2Hex (num / str r)
  (setq str "")
  (while (progn
    (setq r (rem num 16.))
    (cond
      ((< r 10) (setq str (strcat (itoa (fix r)) str)))
      ((< r 16) (setq str (strcat (chr (+ (fix r) 55)) str)))
    )
    (> (setq num (fix (/ num 16.))) 0)
    )
  )
  str
)


Code: [Select]
_$ (Hex2Dec "73AF" )
29615
_$ (Dec2Hex 185)
"B9"
_$
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.