473,385 Members | 1,821 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,385 software developers and data experts.

Passing base64 encoded string from C# to javascript


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
7 6722
"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


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

Similar topics

3
by: Michael | last post by:
I am trying to integrate my ASP page with an external application that sends me a QueryString that is URLEncoded and each Name and Value in the QueryString is Base64 Encoded as well. ...
0
by: BW | last post by:
Sorted my problem. Issue - retrieve Base64 encoded Zlib compressed XML stream. The compressed XML stream was compressed using Zlib on a Java platform. Resolution. (VB.NET) 1) Retrieve...
4
by: John | last post by:
Hi all, I've been going through google and yahoo looking for a certain base64 decoder in C without success. What I'm after is something that you can pass a base64 encoded string into and get back...
6
by: Chris Fink | last post by:
I have an xml document that contains some elements encoded as Base64. How do I dynamically scan the XML Document and pull out the sections that are Base64... My overall goal is to display the...
2
by: kevin | last post by:
DISCLAIMER: I know what the words mean (i.e. by definition), but I in know way pretend to understand the specifics of either, therefore I may need a basic primer before I can accomplish this task,...
0
by: Phil C. | last post by:
(Cross post from framework.aspnet.security) Hi. I testing some asp.net code that generates a 256 bit Aes Symmetric Key and a 256 bit entropy value. I encrypt the Aes key(without storing it as...
4
by: Tamir Weiss | last post by:
I'm trying to send a byte array (JPG image) from javascript to a servlet for processing. I'm using Base64 to encode the byte array in javascript, and then a base64 decoder in the servlet to decode...
13
by: aruna.eies.eng | last post by:
i am currently trying to convert data into binary data.for that i need to know how to achieve it in c language and what are the libraries that we can use. so if any one can send me a sample code or...
4
by: Sam | last post by:
Hi, I am using some functions return a base64 encoded string. The functions write it into an unsigned char buffer. I am a little confused as to why a base64 encoded string would be using an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.