473,491 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What's happended to ANSI chars 135 and 130 - ToString/GetBytes

Hi,

Here's a little hack I put together to try to get to the bottom of a
problem I'm having with trying to base64 encode a hash value. The hash
value contains character codes 135 and 130 amongst others.

This snippet will set up a string of chars 190, 135, 130, 73, 242, 243,
10. It puts them into a bytearray.

string encodedData;
byte[] andBackAgainBytes;
char a1 = (char)190;
char a2 = (char)135;
char a3 = (char)130;
char a4 = (char)73;
char a5 = (char)242;
char a6 = (char)243;
char a7 = (char)10;
string data = a1.ToString() + a2.ToString() + a3.ToString() +
a4.ToString() + a5.ToString() + a6.ToString() + a7.ToString();
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.Default.GetBytes(data);

However when debugging I look at the resulting byte array I see the
following character codes:

190, 63, 63, 73, 242, 243, 10

Note: the ANSI character "single baseline quote" is char 130 and
"dagger (double)" is char 135.

Any idea what happended to chars 130 and 135. I know a work around but
I'm curious as to why this is the case.

Thanks
Gizmo

May 31 '06 #1
2 3713

gizmo wrote:
Hi,

Here's a little hack I put together to try to get to the bottom of a
problem I'm having with trying to base64 encode a hash value.
The quick answer is to use Convert.ToBase64String on a byte array, but
we will continue for the purposes of enlightenment :)
The hash
value contains character codes 135 and 130 amongst others.
Contains *bytes* 135 and 130. One of the key things about thinking C#
rather than C/++ is knowing that "bytes is bytes and characters is
characters, and only via an Encoding shall the twain meet".

This snippet will set up a string of chars 190, 135, 130, 73, 242, 243,
10.
We'll see what it does in just a moment
It puts them into a bytearray.

string encodedData;
byte[] andBackAgainBytes;
char a1 = (char)190;
char a2 = (char)135;
char a3 = (char)130;
char a4 = (char)73;
char a5 = (char)242;
char a6 = (char)243;
char a7 = (char)10;
OK so far, though semantically dodgy...
string data = a1.ToString() + a2.ToString() + a3.ToString() +
a4.ToString() + a5.ToString() + a6.ToString() + a7.ToString();
Still fine, chars can become strings just fine...
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.Default.GetBytes(data);
And here's the problem. Now, your IP is (like me) in the UK, so I'm
going to guess that your default encoding is the same as mine - good
old Windows Codepage 1252, which is like but not the same as)
iso-8859-1. Now, there's a funny thing about this encoding. Take a look
at <http://en.wikipedia.org/wiki/ISO/IEC_8859-1#Code_table> - and
you'll see a big gap in the middle marked 'unused'. Further down, we
are told "Code values 00-1F, 7F, and 80-9F are not assigned to
characters by ISO/IEC 8859-1". This is our problem - the characters
represented by (char)130 (0x82) and (char)135 (0x87) simply *are not
present* in Encoding.Default, so cannot be converted by it into bytes.
However when debugging I look at the resulting byte array I see the
following character codes:

190, 63, 63, 73, 242, 243, 10
So it helpfully gives us ?s instead - which is where the 63s come from.

Note: the ANSI character "single baseline quote" is char 130 and
"dagger (double)" is char 135.

Any idea what happended to chars 130 and 135. I know a work around but
I'm curious as to why this is the case.


Hopefully I have slightly cleared up the why, but the really important
thing is to get the concept of the separation between characters and
bytes completely embedded in the way you think C#.

--
Larry Lard
Replies to group please

May 31 '06 #2
dmm
I think the problem is that you are mixing your codepages. Casting an
integer value to a char will invoke the unicode codepage (.NET chars are 2
byte unicode chars). You are then using
the Windows 1252 codepage to try to get back to the origonal value.

"gizmo" <ga*********@gmail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Hi,

Here's a little hack I put together to try to get to the bottom of a
problem I'm having with trying to base64 encode a hash value. The hash
value contains character codes 135 and 130 amongst others.

This snippet will set up a string of chars 190, 135, 130, 73, 242, 243,
10. It puts them into a bytearray.

string encodedData;
byte[] andBackAgainBytes;
char a1 = (char)190;
char a2 = (char)135;
char a3 = (char)130;
char a4 = (char)73;
char a5 = (char)242;
char a6 = (char)243;
char a7 = (char)10;
string data = a1.ToString() + a2.ToString() + a3.ToString() +
a4.ToString() + a5.ToString() + a6.ToString() + a7.ToString();
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.Default.GetBytes(data);

However when debugging I look at the resulting byte array I see the
following character codes:

190, 63, 63, 73, 242, 243, 10

Note: the ANSI character "single baseline quote" is char 130 and
"dagger (double)" is char 135.

Any idea what happended to chars 130 and 135. I know a work around but
I'm curious as to why this is the case.

Thanks
Gizmo

May 31 '06 #3

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

Similar topics

24
2373
by: Xah Lee | last post by:
in computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that } in advanced languages such as LISP family, it is not uncommon...
13
6077
by: Dan V. | last post by:
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93? I am trying to create a one line text file with these characters all one one row with no spaces. ...
20
4127
by: Drebin | last post by:
It's a long story really, but the bottom line is we need to encrypt or obfuscate a clear-text 9-digit SSN/taxpayer ID into something less than 21 characters. It doesn't need to be super-secure,...
4
79553
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
2
13851
by: Clay | last post by:
I need to convert a unicode string to an ansi string. This should not be difficult but I simply cannot find a way to do this. Anyone?
1
1267
by: intrader | last post by:
I have a .NET interop assembly Hash.MD5Sum with two methods Identity and GetMD5Sum. I want to call the methods from ASP (JScript), The debugger tells me that object oMD5Sum has one the ToString()...
6
18028
by: msdnuniv | last post by:
Hello everybody, since days i try to convert Unicode-Strings in VB.NET to ANSI which should be processable in VB6 and converted to unicode again. It should be possible with any codepage, e.g....
3
6909
by: Pascal | last post by:
Hello can someone help please ? here is the code in vb6 Dim lChar As Integer lChar =AscB(Mid(sMessage, lByteCount + 1, 1)) i tried lChar = System.Convert.ToUInt16(Asc(Mid(sMessage,...
3
2842
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a byte that holds a bunch of data of type REG_MULTI_SZ in UNICODE,. I must convert this byte to a REG_MULTI_SZ in ANSI code. How can i do this,...? TIA,... Regards
0
6980
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
7157
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,...
0
7192
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...
1
6862
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
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
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...

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.