0

I have an image & when I convert to Base64 using JavaScript and C#, it gives me two different values. what is the reason for this?

JavaScript Code

  function LoadSvg() {
            var main = document.getElementById('svgimg');
            var data = main.innerHTML;
            debugger;
            var base64blob = Base64.encode(data);
            alert(base64blob);
            var image2 = document.getElementById('Img2');
            image2.src = 'data:image/svg+xml;base64,' + base64blob;
        }

C# code

string val=litSvg.Text;
byte[] arr = Encoding.UTF8.GetBytes(val);
string toBaseVal = Convert.ToBase64String(arr);      
ExternalHtml = 
   "<img id=\"dfs\" src=\"data:image/svg+xml;base64," + toBaseVal + "\" />";

in JavaScript main value and c# val values are same. how this happens? if I need to same same value in java script out put, how can I do that?

Edit :

val value :

<svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='470px' height='310px'><line x1='40' y1='10' x2='40' y2='250'style='stroke: rgb(0,0,0); stroke-width: 1' />...

main value :

 <svg id="svg-code" xmlns="http://www.w3.org/2000/svg" version="1.1" width="470px" height="310px"> <line x1="40" y1="10" x2="40" y2="250" style="stroke: rgb(0,0,0); stroke-width: 1"></line>

O/P values :

base64blob : CiAgICAgICAgICAgICAgICA8.....

toBaseVal : PHN2ZyB4bWxucz0naHR0cDov ....

9
  • 1
    What are the values? Try it with a very short, basic example. Commented Nov 5, 2013 at 10:26
  • you might try decoding the base64 value back to see what the difference is - sometimes Base64 algorithms pad the end with 0's Commented Nov 5, 2013 at 10:28
  • main & val contain html value. (same div). problem occurs when i assign that value to img tag, then java script generated image correctly. but when i do same thing with c#, then image is not generate. Commented Nov 5, 2013 at 10:28
  • 2
    you are UTF8 encoding the value in c# Commented Nov 5, 2013 at 10:30
  • @Jimmy - even i use ` System.Text.Encoding.Unicode.GetBytes(val);` it gives different values Commented Nov 5, 2013 at 10:34

2 Answers 2

1

Your Javascript decoded buffer contains whitespace at the beginning

LINEFEED SPACE SPACE SPACE < ...


$ echo "CiAgICAgICAgICAgICAgICA8" | base64 -d | hexdump -C -n 32
00000000  0a 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |.               |
00000010  20 3c                                             | <| 

Your C# decoded buffer is correct

$ echo "PHN2ZyB4bWxucz0naHR0cDov" | base64 -d | hexdump -C -n 16
00000000  3c 73 76 67 20 78 6d 6c  6e 73 3d 27 68 74 74 70  |<svg xmlns='http|
00000010

It is possible that they will both give the desired result though

Sign up to request clarification or add additional context in comments.

2 Comments

in here this base64 should generate image. problem occurs in c# and it is not generated image
the quoting differs between the two (single quotes and double quotes) and the line tag has a closing tag in C# - this might be the next thing you check
1

The problem is spaces here.

Remove leading and trailing spaces from your svg file. In addition - remove all excesive whitespace too ie. collapse 1+ whitespace to one whitespace and in your C# - read+convert your file like this:

byte[] arr = System.IO.File.ReadAllBytes(svf_file);
string toBaseVal = Convert.ToBase64String(arr);

Leave encodings alone.

One code calculates base64 with excesive spaces and the other without (as it removes them implicitly) - read innerHTML

1 Comment

this is not a file. its just text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.