# UTF-8 to MacRoman conversion

**URL:** <https://www.ruby-forum.com/t/utf-8-to-macroman-conversion/187378>\
**Category:** Ruby\
**Created:** [April 22, 2010, 11:20am UTC](https://www.ruby-forum.com/t/utf-8-to-macroman-conversion/187378 "2010-04-22T11:20:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Une\_BSSSSvue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b4bc9f/32.png) [@Une\_BSSSSvue](https://www.ruby-forum.com/u/Une_BSSSSvue)\
**Post date:** [April 22, 2010, 11:20am UTC](https://www.ruby-forum.com/t/utf-8-to-macroman-conversion/187378/1 "2010-04-22T11:20:28Z")

</div>

I need to convert a String from UTF-8 to MacRoman, MacRuby makes use of  
a “special” iconv.rb.

When doing a Mac Roman to UTF-8 converions no prob however when doing  
reversal from UTF-8 to MacRoman the String is still in UTF-8.

Here is my code :  
puts “UTF-8 back to MacRoman :”  
mcr = Iconv.iconv(‘macintosh’, ‘utf-8’,  
str\_utf8).split(/#{LF}/).join(CR)  
puts “#{mcr.size} chars, #{mcr.encoding}”

giving :

# =\> 34 chars, UTF-8

## the icon.rb file (from MacRuby) being :

## def iconv(str, start=0, length=-1) # Not sure if it’s the right thing to do… data = CFStringCreateExternalRepresentation(nil, str, @to\_enc, 0) if data.nil? raise “can’t retrieve data from `#{str}'" end dest = CFStringCreateFromExternalRepresentation(nil, data, @to_enc) if dest.nil? raise "can't convert data from`#{str}’” end CFRelease(data) CFRelease(dest) dest.mutableCopy end

which seems to work from “some encoding” to UTF-8 but not the reverse…

I’d like to help the designer but i’m lost in Apple’s documentation  
about CFStringxyz !
