Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:33:15+00:00 2026-06-13T06:33:15+00:00

I’m having trouble converting text to Base64 string in Java (Android) and .NET (Visual

  • 0

I’m having trouble converting text to Base64 string in Java (Android) and .NET (Visual Basic). The plain (readable) form of ASCII characters convert fine. But when it comes to special characters (characters whose code is greater than 128), they’re creating trouble for me.

For example I try converting a character code whose ASCII value is 65 (the character “A”).

My Java code is:

char a = 65;
String c = String.valueOf(a); 
byte bt[] = c.getBytes();               
String result = Base64.encodeToString(bt, Base64.DEFAULT);

And my .NET code is:

Dim c As String = Chr(65)
Dim result as String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(c))

These both return the same result: “QQ==”. This is fine. But when I try converting a special character, for example a character code 153. Then it returns different results.

char a = 153;
String c = String.valueOf(a);               
byte bt[] = c.getBytes();               
String result = Base64.encodeToString(bt, Base64.DEFAULT);

This returns “wpk=”

And my same .NET code:

Dim c As String = Chr(153) 
Dim result as String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(c))

This returns “4oSi”

This is so strange. What’s wrong here. I’m using the native Base64 libraries on both platforms. Is something wrong with my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:33:16+00:00Added an answer on June 13, 2026 at 6:33 am

    Since the data that you are encoding is encrypted data – random data where any byte can be from 0 to 255 and, in its encrypted state, has no character or text meaning, you need to treat this information as -lets call it – true binary data. Both Java and .NET have full support for true binary data via their respective byte array primitives.

    As you know, base64 encoding is the process of converting true binary data (with a range of 0 to 255) into a slightly larger array of binary data (where each byte is guaranteed to have the same value as an ASCII printable character somewhere between 32 and 126). Let’s call this encoded binary. The encoded binary can then safely be converted to text because virtually every known character set agrees on the printable ASCII character set (32 to 126).

    So the main problem with both the Java and VB.NET snippets is that you are attempting to use text primitives – char and String in Java; String in VB.NET to store true binary data. Once you do that it’s too late. There is no way to reliably convert it back to byte arrays because the text primitives are simply not designed to safely store and retrieve binary data. For more on why this is so, please read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

    Fortunately the fix is simple. For Java, don’t use char and String to store binary data. Put the data directly into a byte array. Try the following:

      byte [] bt = new byte[1];
      bt[0] = (byte) 153;
      String result = Base64.encodeToString(bt, Base64.DEFAULT);
    

    I get mQ==

    The fix is conceptually the same in VB.NET. Don’t use String. Use a byte array.

        Dim bytes() As Byte = New Byte() {153}
        Dim result As String = Convert.ToBase64String(bytes)
    

    Again – the answer is mQ==

    Finally, after the encoding, it’s perfectly fine to use Strings. Your characters are in the ASCII subset and any conversion between String and byte array will not corrupt data because all character sets agree on the ASCII subset.

    Remember you will have the same issue going in the reverse order – decoding. You will be decoding to a byte array, at which point you will be back to true binary. From this point on the data must never be stored as a string – until you are finished with it – ex. decrypting it back to the original clear text.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.