How to write javascript equivalent of Convert.ToBase64String() in javascript? [duplicate] - c#

This question already has answers here:
How can you encode a string to Base64 in JavaScript?
(33 answers)
Closed 9 years ago.
I have byte array and I can convert this usin Convert.ToBase64String() method in c#.
I wrote equivalent of this method in javascript like below. But the result is different.
in c#:
byte[] data = ...
Convert.ToBase64String(data)
in js
function GetStringFromByteArray(array) {
var result = "";
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++)
result += String.fromCharCode(array[i][j]);
}
return result;
}
How can I succeed this in js?

Yes, the result is different, because the Javascript function doesn't do base64 encoding at all.
The base64 encoded data contains six bits of information per character, so the eight bits of a character code is spread out over two characters in the encoded data.
To encode the data, you have to regroup the bits in the bytes into six bit groups, then you can convert each group into a base64 character.
See: Base64

You can use this javascript library

Related

Decoding 'code string' to UTF8 and string

First, sorry about my knowledge ...
Above all I have string type value %B3%F3%C7%F9.
Since I receive it from other processor, I don't know how it made... (just I receive this string)
What I only know are
this is something encoded Korean language (I guess it is 농협)
the encode method is one of utf-8 or euc-kr
What I wnat to do is to decode this strange and coded string to utf-8 string.
(for example, decode %B3%F3%C7%F9 to 농협 and assign it string type variable)
Thanks for your attention
(I'm working in ASP.NET Core 1.1)
Your % values are basically URL encoding, so %F9 represents a byte value of 249, for example.
So the first thing you need to do is convert this into a byte array. I've done this a potentially inefficient way in my example. Once you've done that, you need to convert that byte array into a string using the EUC-KR encoding type.
public static void Main()
{
string data = "%B3%F3%C7%F9";
byte[] bData = new byte[data.Length / 3];
for (int i = 0; i < data.Length; i += 3)
{
int pos = i / 3;
bData[pos] = Convert.ToByte(data.Substring(i + 1, 2), 16);
}
data = System.Text.Encoding.GetEncoding("euc-kr").GetString(bData);
Console.WriteLine(data);
}
Fiddle

C# converting int to hex [duplicate]

This question already has answers here:
Convert integer to hexadecimal and back again
(11 answers)
Closed 8 years ago.
This is a small snippet of the code I have written in Visual Studio.
I need to convert the int add into an hex value (later into a string) as I am accessing the memory of a uC. But when I view the variable during debugging it displays it as a string "add16". What could I be doing wrong?
for (int add = 0; add <= 0xfffff; )
{
for (int X = 0; X <= 15; X++)
{
string address = add.ToString("add16");
addr = Convert.ToString(address);
port.WriteLine(String.Format("WRBK:{0}:{1}", addr, parts[X]));
add++;
}
}
There is a simple and very convenient method that takes an integer and returns a representation of it as a string in hexadecimal notation
string address = Convert.ToString(add, 16);
So, perhaps, your internal loop could be rewritten as
port.WriteLine(String.Format("WRBK:{0}:{1}", Convert.ToString(add++, 16), parts[X]));
and using the standard numeric format strings specifiers reduced to
port.WriteLine(String.Format("WRBK:{0:X}:{1}", add++, parts[X]));
You can tell .NET's String.Format to use hex output for the integer, eliminating any code to hex conversion, use:
port.WriteLine(String.Format("WRBK:{0:X}:{1}", add, parts[X]));

C# Program performance with big bytearrays [duplicate]

This question already has answers here:
byte[] to hex string [duplicate]
(19 answers)
Closed 8 years ago.
I'm trying to create a simple Hex Editor with C#.
For this I'm writing the file into an byte-array, which works fine. But as soon as I put out the bytes to a Textbox in form of a string, the overall performance of the program becomes pretty bad. For example a 190kb file takes about 40 seconds, till it is displayed in the textbox. While that the program is not responding.
The function:
void open()
{
fullstring = "";
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
file = op.FileName;
byte[] fileB = File.ReadAllBytes(file);
long b = fileB.Length;
for (int i = 0; i < fileB.Length; i++)
{
fullstring = fullstring + fileB[i].ToString("X") + " ";
}
textBox9.Text = fullstring;
}
Is there a way to improve performance in this function?
Take a look at this post How do you convert Byte Array to Hexadecimal String, and vice versa?
You can use the code there to output your byte array to text file. One problem you have in your code is that you are using String concatenation instead of StringBuilder. It is better to use StringBuilder otherwise the performance degrades.

How to get character code at < 126

I'm simply creating a 256 byte array in my server-side application and then sending it to client.
C#:
byte[] arr = new byte[256];
for (int i = 0; i < 256;i++ )
{
arr[i] = (byte)i;
}
and then I want to get all character codes (exactly, byte codes from characters) in client-side with JavaScript.
JavaScript:
for(var i = 0;i<data.length;i++) {
console.log(data.charCodeAt(i));
}
The characters after 126, charCodeAt(...) returns 65536.
Exactly I want to know how can I get this character codes after 126?
There are some very good tips on how to parse binary data using charCodeAt in this blog post:
http://fhtr.blogspot.com/2009/12/3d-models-and-parsing-binary-data-with.html
You can also use jDataView if you want to easily read binary data in JavaScript:
http://blog.vjeux.com/2011/javascript/jdataview-read-binary-file.html

Split a result from File.ReadAllBytes

My code here reads all the bytes of a image and stores it in the byte array. Is there a way to convert these bytes into ascii then split them up to 512-char(ascii char) long pieces? Like when you try splitting a string based on the length, you can do that. Can you do something similar to splitting this into 512 lengths? This is to send to the server.
byte[] imagesize;
imagesize = File.ReadAllBytes(#"C:\image.jpeg");
Console.Write(imagesize[1].ToString());
What I really want is to convert these bytes into plain ASCII format (Which in C# would be Encoding.ASCII), then split that long ASCII line from converting all the bytes into 512-char(?) long chunks into a byte array (byte[] chunks). So when I send the packets I can do
for(i=0; i<AmountOfChunks; i++)
{
Packet.payload = chunks[i];
//The "chunks" is the ASCII formated array.
}
If someone knows how to do this, it would greatly help. Thanks, if there's anything more, i'll try to explain it in more detail if i can.
If this is wrong, because i know a byte is 8-bit each. Then somehow to be able to do it, maybe the bytes into a list?
Not clear why you needs this, but you might be looking for Convert.ToBase64String() to get a string representation. For chunking you can just walk over the resulting string and split at the appropriate indexes:
byte[] imagesize = File.ReadAllBytes(#"C:\image.jpeg");
string base64String = Convert.ToBase64String(imagesize);
List<string> chunks = new List<string>();
for (int i = 0; i < base64String.Length; i+=512)
{
chunks.Add(base64String.Substring(i, Math.Min(512, base64String.Length - i)));
}
Try to make this
int i=0;
do
{
sendBytes = imagesize.Skip(512*i).Take(512).ToArray();
//Your function of send
i++;
}
while(imagesize.Count()-512*i>0)

Categories