[return: System.Xml.Serialization.XmlElementAttribute("return", DataType="base64Binary")]
public byte[] get(...)
I am trying to get a xml(utf-8) from this webservice. I have tried multiple things to try and get the xml out of the byte array like:
stream
encoding
decoder
converter
[Extra info]
When decoding the byte array with Encoding.UTF8.GetString(bytes)
I get a string with strange signs and symbols but also with some text
Starting with: %PDF-1.4
[SOLUTION]
Writing the byte array to a pdf file makes it readable.
I think the web service provides a byte stream that is simply base64-encoded data represented as integers instead of chars. I believe that the base64 chars are a subset of ASCII, so you need to convert the byte array to ASCII (i.e. base64 represented as chars), then convert these chars from base64:
var base64AsAscii = Encoding.ASCII.GetString(bytesFromWebService);
var decodedBytes = Convert.FromBase64String(bytesAsAscii);
var text = Encoding.UTF8.GetString(decodedBytes);
You can try Convert.ToBase64String.
Related
Good day!
I convert binary file into char array:
var bytes = File.ReadAllBytes(#"file.wav");
char[] outArr = new char[(int)(Math.Ceiling((double)bytes.Length / 3) * 4)];
var result = Convert.ToBase64CharArray(bytes, 0, bytes.Length, outArr, 0, Base64FormattingOptions.None);
string resStr = new string(outArr);
So, is it little endian?
And does it convert to UTF-8?
Thank you!
You don't have any UTF-8 here - and UTF-8 doesn't have an endianness anyway, as its code unit size is just a single byte.
Your code would be simpler as:
var bytes = File.ReadAllBytes(#"file.wav");
string base64 = Convert.ToBase64String(bytes);
If you then write the string to a file, that would have an encoding, which could easily be UTF-8 (and will be by default), but again there's no endianness to worry about.
Note that as base64 text is always in ASCII, each character within a base64 string will take up a single byte in UTF-8 anyway. Even if UTF-8 did have different representations for multi-byte values, it wouldn't be an issue here.
C# char represents a UTF-16 character element. So there is no UTF-8 here.
Since .net is little endian, and since char is two bytes wide, then the char array, and the string, are both stored in little endian byte order.
If you want to convert your byte array to base64 and then encode as UTF-8 do it like this:
byte[] base64utf8 = Encoding.UTF8.GetBytes(Convert.ToBase64String(bytes));
If you wish to save the base64 text to a file, encoded as UTF-8, you could do that like so:
File.WriteAllText(filename, Convert.ToBase64String(bytes), Encoding.UTF8);
Since UTF-8 is a byte oriented encoding, endianness is not an issue.
I have a piece of data that I am receiving in hexadecimal string format, example: "65E0C8DEB69EA114567954". It was made this way in C# by converting a byte array to a hexadecimal string. However, I am using PHP to read this string and need to temporarily convert this back to the byte array. If it matters, I will be decrypting this byte array, then reconverting it to unencrypted hexadecimal and or plaintext, but I will figure that out later.
So the question is, how do I convert a string like the above back to an encoded byte array/ blob in PHP?
Thanks!
This does the trick:
$validHex = '65E0C8DEB69EA114567954';
$binStr = join('', array_map('chr', array_map('hexdec', str_split($validHex, 2))));
I have a byte array being returned by a rest service. The issue is that it is being encoded into XML. Is there anyway to extract the byte array from the XML without losing its represented file structure?
Or am I doomed to work with a string? If so is there any way to convert the string to the byte array so I can create the file it represents?
Thanks!!!!
To embed a byte array into an XML file, your best option is to encode the array into a string, via a hex encoder, or a base64 encoder. A hex encoder would accept an array like {0xEF, 0x12, 0x5A} into a string like "EF125A". The base64 encoder will do something similar, but will produce a smaller string.
Then you would do the converse in order to read the xml file.
Depending on your language and platform, there are different ways to do the encoding and decoding.
I know how to convert from a string to byte[] in C#. In this particular case, I'm working with the string representation of an HMAC-SHA256 key. Let's say the string representation of this key I get from an OpenID OP is:
"81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8="
I convert it to byte[] like this:
byte[] myByteArr = Encoding.UTF8.GetBytes("81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=");
The problem I have with that is that it seems to be losing the original data. If I take the byte array from the previous step and convert it back to a string, it's different from the original string.
string check = Convert.ToBase64String(myByteArr);
check ends up being:
"ODFGTnliS1dmY001Mzl2Vkd0SnJYUm1vVk14Tm1aSFkzT2dVcm84K3BaOD0="
which is obviously not the same as the original string representation I started with.
With crypto keys, always use Convert.FromBase64String and Convert.ToBase64String. That way you'll be doing it the standard way and will not lose bytes due to encoding problems. Base 64 string may not be space efficient but it is the preferred method for storage and transport of keys in many schemes.
Here is a quick verification:
byte[] myByteArr = Convert.FromBase64String("81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=");
string check = Convert.ToBase64String(myByteArr);
Console.WriteLine(check);
// Writes: 81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=
The first function (Encoding.UTF8.GetBytes) takes a string (of any kind) and returns a byte[] that represents that string in a particular encoding -- in your case, UTF8.
The second function (Convert.ToBase64String) takes a byte array (of any kind) and returns a string in base64 format so that you can store this binary data in any ASCII-compatible field using only printable characters.
These functions are not counterparts. It looks like the string you're getting is a base64-encoded string. If this is the case, use Convert.FromBase64String to get the byte[] that it represents, not Encoding.UTF8.GetBytes.
The bytes you get when using byte[] Encoding.GetBytes(string) and decoding a base64 string are not the same things. The former gives you the bytes that represent the string. You however want to decode a base64 string back to the bytes it represents. In that case you want to use Convert.FromBase64String().
string encoded = "81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=";
byte[] decoded = Convert.FromBase64String(encoded); // this gives the bytes that the encoded string represents
The encoding classes have a GetString method, to convert it from a byte array back to a string.
If you used the UTF8 encoding to create the byte array, you should use the same coding to get it back again.
var original = "81FNybKWfcM539vVGtJrXRmoVMxNmZHY3OgUro8+pZ8=";
var byteArray = Encoding.UTF8.GetBytes(original);
var copy = Encoding.UTF8.GetString(byteArray);
bool match = (copy == original); // This returns true
I have a webservice that returns a binary data as a string. Using C# code how can I store it in byte array? Is this the right way?
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(inputString);
Actually, this didn't work. Let me explain it more:
the web service code converts a string (containing XSLFO data) into byte array using utf8 encoding. In my web service response I only see data something like "PGZvOnJvb3QgeG1sbnM6Zm89Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML0Zvcm1hdCIgeG1sbnMÂ6eGY9Imh0dHA6Ly93d3cuZWNyaW9uLmNvbS94Zi8xLjAiIHhtbG5zOm1zeHNsPSJ1c==". Actually I would like to have the original string value that was converted into byte[] in the service. Not sure if it possible?
No, that's a bad idea.
Unless the input data was originally text, trying to use Encoding is a bad idea. The web service should be using something like base64 to encode it - at which point you can use Convert.FromBase64String to get the original binary data back.
Basically, treating arbitrary binary data as if it were encoded text is a quick way to lose data. When you need to represent binary data in a string, you should use base64, hex or something similar.
This may mean you need to change the web service as well, of course - if it's creating the string by simply treating the binary data as UTF-8-encoded text, it's broken to start with.
If the string is encoded in UTF8 Encoding, then yes that is the correct way. If it is in Unicode it is very similar:
System.Text.Unicode encoding = new System.Text.Unicode();
byte[] bytes = encoding.GetBytes(inputString);
Base64Encoding is a little different:
byte[] bytes = Convert.FromBase64String(inputString);