UTF-8 Encoding

P

Peter Romero

All I want to do is write a simple app that takes a user-entered character
and gives me the 1, 2, or 4 bytes that represent it in UTF-8. Common sense
indicates that the active code should be about 1 line long. However, being
new to dot net, I can't seem to figure out any way to do it, and everything
I read about encodings and encoders suggests it must be at least 450,890
lines of code. I'm about ready to write my own conversion function.

Does anyone have any tips?
 
P

Peter Romero

All I want to do is write a simple app that takes a user-entered character
and gives me the 1, 2, or 4 bytes that represent it in UTF-8. Common sense
indicates that the active code should be about 1 line long. However, being
new to dot net, I can't seem to figure out any way to do it, and everything
I read about encodings and encoders suggests it must be at least 450,890
lines of code. I'm about ready to write my own conversion function.

One would think that this would be somewhat easier than splitting uranium
atoms with sheer mental power. I used to think I could "learn" dot net...
but now I'll just be happy if I can stand near it without it hurting me.
 
G

Guest

Imports System.Text

Private Function sCodeString(ByVal s As String, ByVal Ecode As Integer,
Optional ByVal bgend As Boolean = False) As Byte()
'Codes String into byte array where s is string, Ecode is encoding,
bgend is big indian if true for encoding Ecode =1 UTF16
Dim b As Byte()
Select Case Ecode
Case Encode.ISO8 'Iso8 (Ascii)
b = Encoding.ASCII.GetBytes(s)
Case Encode.UTF16 'UTF16 (Unicode with BOM)
Dim c As Byte()
If bgend Then 's(StartIndex) = &HFE And s(StartIndex + 1) =
&HFF Then
'Big-Endian
c = Encoding.BigEndianUnicode.GetBytes(s)
ReDim b(UBound(c, 1) + 2)
b(0) = CByte(&HFE) : b(1) = CByte(&HFF)
Else
'Little-Endian is the default
c = Encoding.Unicode.GetBytes(s)
ReDim b(UBound(c, 1) + 2)
b(0) = CByte(&HFF) : b(1) = CByte(&HFE)
End If
c.CopyTo(b, 2)
Case Encode.UTF16BE 'UTF16BE (Unicode without BOM
b = Encoding.BigEndianUnicode.GetBytes(s)
Case Encode.UTF8 'UTF-8
b = Encoding.UTF8.GetBytes(s)
End Select
Return b
End Function
 
P

Peter Romero

Dennis,

Thank you for your help! It was very nice of you to go to all the trouble to
write that for me.

—Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top