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

  • SEARCH
  • Home
  • 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 7500293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:07:48+00:00 2026-05-29T20:07:48+00:00

If I convert a Bytearray in Javascript to a string using the following method:

  • 0

If I convert a Bytearray in Javascript to a string using the following method:

convertByteArrayToString: function(byteArray)
{
    var s = '';
    for(var i = 0;i < byteArray.length;i++)
            {
                    s += String.fromCharCode(byteArray[i])
            }
    return s;
},

How do I then convert this string back to a byte array in C#?

I have tried all three of the following:

 System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding().GetBytes(myString);
 System.Text.UTF32Encoding encoding = new System.Text.UTF32Encoding().GetBytes(myString);
 System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding().GetBytes(myString);

The closest is the first one, but it returns a Byte array which is double the length of the original with every other element in the array being 0 and sometimes an incorrect value or two.

As far as I am aware, the String.fromCharCode works in Unicode, so how do get this to interoperate with C#?

This is driving me nuts!

Edit:

Here is an example,

Original Byte array:
[139, 104, 166, 35, 8, 42, 216, 160, 235, 153, 23, 143, 105, 3, 24, 255]

My function converts this to:
“h¦#*Ø ëiÿ”

In C#, attempting to decode this String to a byte array yields the following results:

Using System.Text.UnicodeEncoding gives:
[142,0,139,0,104,0,166,0,35,0,8,0,42,0,216,0,32,0,235,0,153,0,23,0,143,0,105,0,30,24,0,255,0]

  • 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-05-29T20:07:50+00:00Added an answer on May 29, 2026 at 8:07 pm

    crypto-js has Crypto.charenc.UTF8.stringToBytes() and Crypto.charenc.UTF8.bytesToString(), that werk perfectly with UTF-8 encoding in C#.

    Edit

    After discussion it turns out, the OP wants to transport a byte[] from JS into C# – this is better done via base64 encoding (I recomend the same JS toolkit)

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

Sidebar

Related Questions

Can we convert a hex string to a byte array using a built-in function
We try to convert from string to Byte[] using the following Java code: String
Is there a way to convert a byte array to string other than using
How can I convert a ByteArray to string. The toString() is not working as
I want to convert a binary file to a string which could be then
How do I convert a byte array to a string? Is this the correct
I am trying to convert a bytearray into a string . The problem is
I'm trying to convert the byteArray of a Sound Object to an array with
How can you convert a byte array to a hexadecimal string and vice versa?
I'm wonder what the best way to convert a byte array (length 4) to

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.