Skip to content
Advertisement

Conversion Between Base 64 String and Byte Array Varies in C# and Python

I have a C# byte array b_a that has 16 byte length. When I convert b_a to base 64 string aaa, it’s length returns 24

JavaScript

I want to convert aaa string to byte array in Python. But When I convert it, it’s length still returns 24.

JavaScript

I want to get initial b_a with all. What is the point that I miss?

Advertisement

Answer

If you have base 64 encoded information, you need to decode it to get bytes. In Python 3 (and in Python 2):

JavaScript

Given that your code apparently works without an error, you must be using Python 2. Using bytearray on a str object in Python 3 would raise an exception because it doesn’t know how to translate a string to bytes without a string encoding information. But in Python 2, str objects are really sequences of bytes under the hood. So bytearray just returns the str as bytes in Python 2:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement