By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,064 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,064 IT Pros & Developers. It's quick & easy.

UTF-8 Encoding

P: n/a
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?

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> 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.

Nov 21 '05 #2

P: n/a
Perhaps slightly over-exaggerating me thinks! :-)

See http://www.developerfusion.co.uk/show/4710/3/

HTH, Phil
"Peter Romero" <pe***@meesez.com> wrote in message
news:6Y****************@newsread3.news.atl.earthli nk.net...
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.

Nov 21 '05 #3

P: n/a
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
--
Dennis in Houston
"Peter Romero" wrote:
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.

Nov 21 '05 #4

P: n/a
Dennis,

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

—Peter

Nov 21 '05 #5

P: n/a
> Perhaps slightly over-exaggerating me thinks! :-)

Well of course! I was needing to take a break and go ride the bike.

See http://www.developerfusion.co.uk/show/4710/3/


Thank you.
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.