473,789 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mysterious functions of text encoding....

For me is a little bit mysterious how work encoding and decoding functions,
what is underneath of their calling?

Encoding1.GetBy tes(string1); in particularly ASCII.GetBytes( string1)

Encoding1.GetCh ars(string1);

Encoding1.GetCh ars(arrayofbyte s1);

string1=Encodin g1.GetString(ar rayofbytes1);

If I know (perhaps) that a char is based on 2 bytes (16 bits)

and all Strings in C#(NET) are a set of chars

P.S. Please explain on plane of working with bytes (I come from C world)

I will appreciate


Jul 19 '05 #1
4 3420
They read a Unicode char, and then determine the actual byte sequence
in whatever encoding you choose. For instance, if you use ASCII or
UTF-8 and pass "hi", it'll make a byte[] {0x68, 0x69}. If using
Unicode, it'd be 0x68, 0x00, 0x69, 0x00.

The same applies when you get chars or a string from bytes. It reads
the bytes and then determines what actual Unicode characters they are.

Also, there is no Encoding.GetCha rs(string) method (since it'd only
return chars of a string, which would always be the .NET internal
representation (Unicode)).

-mike
MVP

"Viorel" <vm*********@mo ldova.cc> wrote in message
news:eP******** *****@TK2MSFTNG P10.phx.gbl...
For me is a little bit mysterious how work encoding and decoding functions, what is underneath of their calling?

Encoding1.GetBy tes(string1); in particularly ASCII.GetBytes( string1)

Encoding1.GetCh ars(string1);

Encoding1.GetCh ars(arrayofbyte s1);

string1=Encodin g1.GetString(ar rayofbytes1);

If I know (perhaps) that a char is based on 2 bytes (16 bits)

and all Strings in C#(NET) are a set of chars

P.S. Please explain on plane of working with bytes (I come from C world)
I will appreciate

Jul 19 '05 #2
Viorel <vm*********@mo ldova.cc> wrote:
So, if I understand correctly Encoding1.GetBy tes (string1); takes content of
string1 represented in Unicode coverts (internally) the content in Encoding1
and then takes the bytes and returns to me as an array of bytes. It means
that internally it always happens conversions from Unicode to Encoding1.

And string1=Encodin g1.GetString(ar rayofbytes1);cr eates(internall y) a string
in Encoding1 and then converts it to Unicode to be assigned to string1

Thus the rule(of language) of keeping all strings in Unicode is never
broken.
No.

Strings are *always* in Unicode. Encoding.GetStr ing takes the sequence
of Unicode characters and converts them into a sequence of bytes which
represents (in the specified encoding) that sequence of characters.

For an example of how this might be done, have a look at my EBCDIC
encoding:
http://www.pobox.com/~skeet/csharp/ebcdic/

You might also find this article useful:
http://www.pobox.com/~skeet/csharp/unicode.html
My notice: First time I thought that all strings are kept in their
encoding.
Nope. Strings don't have any encoding associated with them.
I thought string1 is in Encoding1 and if
string2=Encodin g2.GetString(ar rayofbytes1); and Encoding1!= Encoding2
trying to assign string2 to string1 (string1=string 2) it will arise an
exception. Thus it wouldn't be the need of internal (out of my view)
conversion from Encoding1 to Unicode and it would be more explicit .Am I
right?


Nope.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #3
Strings are *always* in Unicode. Encoding.GetStr ing takes (&from where?&)
the sequence
of Unicode characters and converts them into a sequence of bytes, which
represents (in the specified encoding) that sequence of characters (&if it
can else it truncates&).

If I am not wrong GetString:

1) takes a sequence of bytes

2) creates from that sequence a string('lying' on that bytes ) based
on encoder object(which called GetString)

3) that string are converted in Unicode format(finally to be used by
me)

If these steps are not implemented in C#, could they achieve the same
result?

If no, explain me more detailed the way from bytes to string in same manner
as above

Thank you very much.

Jul 19 '05 #4
[If you could make it clearer which bit you're quoting, it would make
your posts easier to read... I've reformatted it here.]

Viorel <vm*********@mo ldova.cc> wrote:
Jon Skeet wrote:
Strings are *always* in Unicode. Encoding.GetStr ing takes
from where?
From the parameter you pass it.
the sequence of Unicode characters and converts them into a sequence of
bytes, which represents (in the specified encoding) that sequence of
characters

if it can else it truncates
What do you mean by "truncates" here? It doesn't just truncate the
string or byte array. If the encoding is passed a sequence of bytes it
doesn't fully understand (eg including some bits with the top bit set
where the encoding is ASCII) I don't believe there's any particular
specified behaviour - I prefer to end up with '?' in the returned
string, myself.
If I am not wrong GetString:

1) takes a sequence of bytes
Yes.
2) creates from that sequence a string('lying' on that bytes ) based
on encoder object(which called GetString)
No. I don't know where you get this idea from.
3) that string are converted in Unicode format(finally to be used by
me)
You're wrong. There's no need for some strange middle string.
If these steps are not implemented in C#, could they achieve the same
result?
No, because a string is *always* in Unicode.
If no, explain me more detailed the way from bytes to string in same manner
as above


It depends on how the encoding implementation wants to do it - as I
said before, if you want an example implementation, look at my EBCDIC
encoding.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2503
by: SteveS | last post by:
Can anyone help with a mysterious problem that has arisen since 'upgrading' from 8 to 9.2.0.4? The situation is this: Queries that worked fine under 8 are now producing *really* strange results under 9.2.0.4. The queries all involve functions, including built-in functions like NVL. The strangeness is that less than the expected number of rows is returned *or* and ORA-01422 is generated. Here's an example:
2
7270
by: Peter Gerstbach | last post by:
Hi, I want to convert with XSLT/XPATH a String like "Aaa bbb ccc" with variant length into to "AaaBbbCcc". I think it should be possible with these steps: 1) tokenize the String with ' ' as separator with tokenize() 2) make the first character uppercase with substring() and upper-case() 3) put them together with concat()
2
3792
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
8
321
by: Viorel | last post by:
For me is a little bit mysterious how work encoding and decoding functions, what is underneath of their calling? Encoding1.GetBytes(string1); in particularly ASCII.GetBytes(string1) Encoding1.GetChars(string1); Encoding1.GetChars(arrayofbytes1);
8
11887
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
11
5187
by: Spencer | last post by:
I am working on a program that uses System.Xml and an XML file. I have the following code in my project that returns a NullReferenceException: profileDataDoc = new XmlDocument(); profileDataDoc.Load(fileName); XmlElement root = profileDataDoc.DocumentElement; XmlNode GoalNode = root.SelectSingleNode("/Profile_Data/Profile
10
2114
by: tshad | last post by:
I have a Dll I created in VS 2000. The namespace is MyFunctions and the Class is CryptoUtil. I have a program that is using the Class but it can't access it directly. I have a class (below) called CryptoUtil. The functions are Shared functions. I have this Dll in the bin folder of my program. If I don't reference it - it can't seem to find it. If I have:
9
1952
by: Gerry | last post by:
I'm using pyExcelerator and xlrd to read and write data from and to two spreadsheets. I created the "read" spreadsheet by importing a text file - and I had no unicode aspirations. When I read a cell, it appears to be unicode u'Q1", say. I can try cleaning it, like this:
4
9674
by: oveshot16 | last post by:
When I am writing the degree symbol to a text document using the streamwriter it shows up with a Mysterious  in front of it. I know this has something to do with its encoding but I am new to encoding in general and Have no idea how to work it. here is my code StreamWriter sw2; sw2 = File.CreateText("DB\\ioSys.FIL"); string DegreeC = "°C"; IO_file = "t_01_0" + CBA41G1Bus.Text + "_" + StartAddress1 + "_" + temp + "\t" + temp +...
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.