How can I convert base64 decode a string to text in javascript file?

Hello my friends :)

``

This is my javascript source code .

``

===========================================================

      statementsTpro: function (choice)	{
		var str = choice;
		switch (str){	 
			case 'PHP1': 
                    String str = "R28gVG8gSGFja2Jhcg=="
					Decoder decoder = Base64.getDecoder();
                    byte[] decodedByte = decoder.decode(encodedString);
                    String decodedString = new String(decodedByte);
                    System.out.println(decodedString);
					break;					
		} 		
		hackBar.setSelectedText( txt ); 
	  },

===========================================================

As a webdeveloper I often have to decode strings in various formats. I creating a new Extension .

If you could add more string decoding options it would be much appreciated.

Please place your code in the red line. Thank you for your help

Hi underceh welcome to the forum

That doesn’t look like any JavaScript I’m familiar with. It looks more like Java to me. Obviously I’m not understanding the situation. Please explain.

Also, I do not know what you’re asking by “add more options - place your code”.

Forum members are not as likely to “do your work for you” as they are to help you do your work. If you explain what problems you are having there is a better chance of getting helpful replies.

That’s because it is Java.

Chrome extensions are coded in Javascript.

The thread topic is ‘how can I convert base64 decode a string to text in javascript file’, so I will answer that question: The function atob decodes base64.

var mystring = "SGVsbG8gV29ybGQ=";
console.log(atob(mystring)); //Logs "Hello World".

The function btoa encodes a string in the same manner.

1 Like

I have trouble with images so I may have made a typo, but I see

String str = "R28gVG8gSGFja2Jhcg==" 
Decoder decoder = Base64.getDecoder(); 
byte[] decodedByte = decoder.decode(encodedString); 
String decodedString = new String(decodedByte); 
System.out.println(decodedString); 
break; 

Going backwards
System.out.println(decodedString);
uses the variable from
String decodedString = new String(decodedByte);
uses the variable from
byte[] decodedByte = decoder.decode(encodedString);
I see no variable named encodedString. A name mismatch ? with
String str = "R28gVG8gSGFja2Jhcg=="
Admittedly my Java is rusty, but shouldn’t there be a statement end for that line?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.