Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/TypeScript Byte Array getting encoded when sent to client

I am trying to send a byte array pulled from my database via C# to my front end client, which is written in JavaScript/TypeScript, but it seems that the byte array is being encoded somewhere between being sent from the back end and getting to the front end.

The data in the database is a varbinary with the value: 0x00000001. I've set a breakpoint where my C# code is returning the retrieved value and I am getting a byte array with the value of: [0, 0, 0, 1]. However, when it gets to the client, the value is: "AAAAAQ==".

I've tried decoding it using the following code:

let encodedString = encodeURI(encodeURIComponent(flag));

let arr = [];
for (let i = 0; i < encodedString.length; i++) {
    arr.push(encodedString.charCodeAt(i));
}

But that code returns this array of values: [65, 65, 65, 65, 65, 81, 37, 50, 53, 51, 68, 37, 50, 53, 51, 68]

What is causing the data to be encoded, and how can I either decode it in TypeScript or prevent it from encoded when it is being sent to the client?

like image 289
CodyCS Avatar asked Nov 14 '25 23:11

CodyCS


1 Answers

"AAAAAQ==" is the Base64 encoded version of 0x00000001. As such you'll need to convert it back using atob then push each char code in to an array.

let text = 'AAAAAQ==';

let bin = atob(text);

let bytes = [];
for (let i = 0; i< bin.length; i++){
    bytes.push(bin.charCodeAt(i));
}

console.log(bytes);
like image 130
phuzi Avatar answered Nov 17 '25 14:11

phuzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!