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

Passing base64 encoded string from C# to javascript

P: n/a

Hi

I am trying to pass an encoded string to a JavaScript

the following is the C# code which convert the string STRING_TO_ENCODE to
base64

byte[] bytIn =
System.Text.UnicodeEncoding.Unicode.GetBytes(STRIN G_TO_ENCODE);
string result=System.Convert.ToBase64String

the STRING_TO_ENCODE contains both characters in Hebrew and English

I understand that JavaScript uses Unicode strings that is why I am using
UnicodeEncoding.Unicode.GetBytes

tha "result" variable is passed in the URL as a parameter
in the HTML i am using UTF8 char set
It DOES NOT WORK

Thanks
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Julia" <co********@012.net.il> wrote in message
news:OL**************@TK2MSFTNGP14.phx.gbl
I am trying to pass an encoded string to a JavaScript

the following is the C# code which convert the string
STRING_TO_ENCODE to base64

byte[] bytIn =
System.Text.UnicodeEncoding.Unicode.GetBytes(STRIN G_TO_ENCODE);
string result=System.Convert.ToBase64String

the STRING_TO_ENCODE contains both characters in Hebrew and English

I understand that JavaScript uses Unicode strings that is why I am
using UnicodeEncoding.Unicode.GetBytes

tha "result" variable is passed in the URL as a parameter

It DOES NOT WORK


What exactly is "It" that "DOES NOT WORK"? What are you trying to do
with the result, and what problem do you have?
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage
Nov 16 '05 #2

P: n/a


-I am trying to decode the encoded text to get the orginial text.
-the problem is that instead of hebrew characters i see other charactes(not
just '?')
after decoding.
-decoding is made using JavaScript.
-I don't have any error or exception.
-English characters are correctly displayed.
Thanks.
"Igor Tandetnik" <it********@mvps.org> wrote in message
news:u0**************@TK2MSFTNGP11.phx.gbl...
"Julia" <co********@012.net.il> wrote in message
news:OL**************@TK2MSFTNGP14.phx.gbl
I am trying to pass an encoded string to a JavaScript

the following is the C# code which convert the string
STRING_TO_ENCODE to base64

byte[] bytIn =
System.Text.UnicodeEncoding.Unicode.GetBytes(STRIN G_TO_ENCODE);
string result=System.Convert.ToBase64String

the STRING_TO_ENCODE contains both characters in Hebrew and English

I understand that JavaScript uses Unicode strings that is why I am
using UnicodeEncoding.Unicode.GetBytes

tha "result" variable is passed in the URL as a parameter

It DOES NOT WORK


What exactly is "It" that "DOES NOT WORK"? What are you trying to do
with the result, and what problem do you have?
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage

Nov 16 '05 #3

P: n/a
"Julia" <co********@012.net.il> wrote in message
news:ej*************@tk2msftngp13.phx.gbl
-I am trying to decode the encoded text to get the orginial text.
-the problem is that instead of hebrew characters i see other
charactes(not just '?')
after decoding.
-decoding is made using JavaScript.


How is decoding made using JavaScript? Show some code.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage
Nov 16 '05 #4

P: n/a
Thanks

code for decoding

var base64 = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
'4', '5', '6', '7', '8', '9', '+', '/' ]; // 56 to 63
function reverseBase64 () {
var r = new Object();
for (var i = 0; i < 64; i++) {
r[base64[i]] = i;
} return r;
}
var reversedBase64 = reverseBase64();

function decode (encStr) {
var charCodes = new Array();
var decStr = "";
for (var i = 0; i < encStr.length; i++)
charCodes[i] = reversedBase64[encStr.charAt(i)];
for (var i = 0; i < encStr.length; i += 4) {
var bits24 = ( charCodes [i] & 0xFF ) << 18;
bits24 |= ( charCodes [i + 1] & 0xFF ) << 12;
bits24 |= ( charCodes [i + 2] & 0xFF ) << 6;
bits24 |= ( charCodes [i + 3] & 0xFF ) << 0;
decStr += String.fromCharCode((bits24 & 0xFF0000) >> 16);
if (charCodes[i + 2]) // check for padding character =
decStr += String.fromCharCode((bits24 & 0xFF00) >> 8);
if (charCodes[i + 3]) // check for padding character =
decStr += String.fromCharCode((bits24 & 0xFF) >> 0);
} return decStr;
}

"Igor Tandetnik" <it********@mvps.org> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
"Julia" <co********@012.net.il> wrote in message
news:ej*************@tk2msftngp13.phx.gbl
-I am trying to decode the encoded text to get the orginial text.
-the problem is that instead of hebrew characters i see other
charactes(not just '?')
after decoding.
-decoding is made using JavaScript.


How is decoding made using JavaScript? Show some code.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage

Nov 16 '05 #5

P: n/a
Julia <co********@012.net.il> wrote:
Thanks

code for decoding

var base64 = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0 to 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8 to 15
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16 to 23
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24 to 31
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32 to 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40 to 47
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48 to 55
'4', '5', '6', '7', '8', '9', '+', '/' ]; // 56 to 63
function reverseBase64 () {
var r = new Object();
for (var i = 0; i < 64; i++) {
r[base64[i]] = i;
} return r;
}
var reversedBase64 = reverseBase64();

function decode (encStr) {
var charCodes = new Array();
var decStr = "";
for (var i = 0; i < encStr.length; i++)
charCodes[i] = reversedBase64[encStr.charAt(i)];
for (var i = 0; i < encStr.length; i += 4) {
var bits24 = ( charCodes [i] & 0xFF ) << 18;
bits24 |= ( charCodes [i + 1] & 0xFF ) << 12;
bits24 |= ( charCodes [i + 2] & 0xFF ) << 6;
bits24 |= ( charCodes [i + 3] & 0xFF ) << 0;
decStr += String.fromCharCode((bits24 & 0xFF0000) >> 16);
if (charCodes[i + 2]) // check for padding character =
decStr += String.fromCharCode((bits24 & 0xFF00) >> 8);
if (charCodes[i + 3]) // check for padding character =
decStr += String.fromCharCode((bits24 & 0xFF) >> 0);
} return decStr;
}


That decoding is the problem. You're decoding it as if each byte is a
character, rather than each *pair* of bytes.

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

P: n/a
"Julia" <co********@012.net.il> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl
for (var i = 0; i < encStr.length; i += 4) {
var bits24 = ( charCodes [i] & 0xFF ) << 18;
bits24 |= ( charCodes [i + 1] & 0xFF ) << 12;
bits24 |= ( charCodes [i + 2] & 0xFF ) << 6;
bits24 |= ( charCodes [i + 3] & 0xFF ) << 0;
decStr += String.fromCharCode((bits24 & 0xFF0000) >> 16);
if (charCodes[i + 2]) // check for padding character =
decStr += String.fromCharCode((bits24 & 0xFF00) >> 8);
if (charCodes[i + 3]) // check for padding character =
decStr += String.fromCharCode((bits24 & 0xFF) >> 0);
} return decStr;
}


How do you expect to get any Unicode characters, if you never ever pass
a value outside [0-255] range to fromCharCode? You treat each byte as
representing a single character. But when you encoded, every character
was encoded as two bytes.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage
Nov 16 '05 #7

P: n/a
Thanks Jon and Igor
I will try to fix it.
thanks for your help.
"Igor Tandetnik" <it********@mvps.org> wrote in message
news:eo**************@TK2MSFTNGP09.phx.gbl...
"Julia" <co********@012.net.il> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl
for (var i = 0; i < encStr.length; i += 4) {
var bits24 = ( charCodes [i] & 0xFF ) << 18;
bits24 |= ( charCodes [i + 1] & 0xFF ) << 12;
bits24 |= ( charCodes [i + 2] & 0xFF ) << 6;
bits24 |= ( charCodes [i + 3] & 0xFF ) << 0;
decStr += String.fromCharCode((bits24 & 0xFF0000) >> 16);
if (charCodes[i + 2]) // check for padding character =
decStr += String.fromCharCode((bits24 & 0xFF00) >> 8);
if (charCodes[i + 3]) // check for padding character =
decStr += String.fromCharCode((bits24 & 0xFF) >> 0);
} return decStr;
}


How do you expect to get any Unicode characters, if you never ever pass
a value outside [0-255] range to fromCharCode? You treat each byte as
representing a single character. But when you encoded, every character
was encoded as two bytes.
--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.