UTF-8 conversion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

UTF-8 conversion

Frank Sonnemans-3
Does anyone know how I convert a string to the UTF-8 format?

Thanks,

Frank


Reply | Threaded
Open this post in threaded view
|

Re: UTF-8 conversion

Steve Waring-2
"Frank Sonnemans" <[hidden email]> wrote in message
news:a57p9q$1dgph$[hidden email]...
> Does anyone know how I convert a string to the UTF-8 format?

Hi Frank,

I use the following loose method to convert from a UnicodeString instance to
a String instance. It should give you the correct byte representation of a
UTF-8 string, but the String class does not recognise that characters >
16r7F are multiByte characters.

Hope this helps,
Steve Waring


!UnicodeString methodsFor!
asUTF8String
"Answer a byte string representation of the receiver.
-Not supported in Win95, but should work in Win98"
  | buf size bytes |
  size := self size.
  buf := String new: size+size+size.
  size == 0 ifTrue: [^buf]. "Avoid 'The Parameter is Incorrect' error"
  bytes := KernelLibrary default
    wideCharToMultiByte: 65001 "CP_UTF8"
    dwFlags: 0
    lpWideCharStr: self
    cchWideChar: size
    lpMultiByteStr: buf
    cchMultiByte: buf size
    lpDefaultChar: nil
    lpUsedDefaultChar: nil.
    bytes == 0 ifTrue: [^KernelLibrary default systemError].
    buf resize: bytes.
    ^buf! !
!UnicodeString categoriesFor: #asUTF8String!converting!public! !


>
> Thanks,
>
> Frank
>
>