473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

UTF-8 Encoding

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
5 2466
> 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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alban Hertroys | last post by:
Another python/psycopg question, for which the solution is probably quite simple; I just don't know where to look. I have a query that inserts data originating from an utf-8 encoded XML file....
38
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I...
16
by: lawrence | last post by:
I was told in another newsgroup (about XML, I was wondering how to control user input) that most modern browsers empower the designer to cast the user created input to a particular character...
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
7
by: Jimmy Shaw | last post by:
Hi everybody, Is there any SIMPLE way to convert from UTF-16 to UTF-32? I may be mixed up, but is it possible that all UTF-16 "code points" that are 16 bits long appear just the same in UTF-32,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.