Iconv - Character set conversion
To use this binding add the following resolver and the dependency:
- sbt
-
resolvers += Resolver.bintrayRepo("scala-native-bindgen", "maven") libraryDependencies += "org.scala-native.bindgen" %%% "libiconv" % "0.3.1"
- Maven
-
<repositories> <repository> <id>maven</id> <url>http://dl.bintray.com/scala-native-bindgen/maven</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.scala-native.bindgen</groupId> <artifactId>libiconv_native0.3_2.11</artifactId> <version>0.3.1</version> </dependency> </dependencies>
- Gradle
-
repositories { maven { url "http://dl.bintray.com/scala-native-bindgen/maven" } } dependencies { compile group: 'org.scala-native.bindgen', name: 'libiconv_native0.3_2.11', version: '0.3.1' }
The iconv.h
header allows to convert text between different character sets, for example UTF-8 to ISO-8859-1.
Example
import org.scalanative.bindgen.bindings.iconv.iconv._
import scala.scalanative.native._
import java.nio.charset.Charset
val UTF8 = Charset.forName("UTF-8")
val encode = iconv_open(c"UTF-8", c"ISO-8859-1")
val decode = iconv_open(c"ISO-8859-1", c"UTF-8")
Zone { implicit zone =>
val originalBuf = toCString("øre", UTF8) // Ear in Danish
val originalBufPtr = alloc[CString]
!originalBufPtr = originalBuf
val originalBytesLeft = alloc[CSize]
!originalBytesLeft = string.strlen(originalBuf)
val translatedBuf = alloc[Byte](32)
val translatedBufPtr = alloc[CString]
!translatedBufPtr = translatedBuf
val translatedBytesLeft = alloc[CSize]
!translatedBytesLeft = 32
val translatedCode = iconv(
encode,
originalBufPtr,
originalBytesLeft,
translatedBufPtr,
translatedBytesLeft
)
}
iconv_close(encode)
Full source at GitHub
0.3.1