Passing base64 encoded string from C# to javascript

J

Julia

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(STRING_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
 
I

Igor Tandetnik

Julia said:
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(STRING_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
 
J

Julia

-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 said:
Julia said:
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(STRING_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
 
I

Igor Tandetnik

Julia said:
-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
 
J

Julia

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;
} return r;
}
var reversedBase64 = reverseBase64();

function decode (encStr) {
var charCodes = new Array();
var decStr = "";
for (var i = 0; i < encStr.length; i++)
charCodes = reversedBase64[encStr.charAt(i)];
for (var i = 0; i < encStr.length; i += 4) {
var bits24 = ( charCodes & 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 said:
Julia said:
-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
 
J

Jon Skeet [C# MVP]

Julia said:
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;
} return r;
}
var reversedBase64 = reverseBase64();

function decode (encStr) {
var charCodes = new Array();
var decStr = "";
for (var i = 0; i < encStr.length; i++)
charCodes = reversedBase64[encStr.charAt(i)];
for (var i = 0; i < encStr.length; i += 4) {
var bits24 = ( charCodes & 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.
 
I

Igor Tandetnik

Julia said:
for (var i = 0; i < encStr.length; i += 4) {
var bits24 = ( charCodes & 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
 
J

Julia

Thanks Jon and Igor
I will try to fix it.
thanks for your help.


Igor Tandetnik said:
Julia said:
for (var i = 0; i < encStr.length; i += 4) {
var bits24 = ( charCodes & 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top