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 322539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:56:53+00:00 2026-05-12T08:56:53+00:00

I have an array of bytes that I would like to store as a

  • 0

I have an array of bytes that I would like to store as a string. I can do this as follows:

byte[] array = new byte[] { 0x01, 0x02, 0x03, 0x04 };
string s = System.BitConverter.ToString(array);

// Result: s = "01-02-03-04"

So far so good. Does anyone know how I get this back to an array? There is no overload of BitConverter.GetBytes() that takes a string, and it seems like a nasty workaround to break the string into an array of strings and then convert each of them.

The array in question may be of variable length, probably about 20 bytes.

  • 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-12T08:56:54+00:00Added an answer on May 12, 2026 at 8:56 am

    Not a built in method, but an implementation. (It could be done without the split though).

    String[] arr=str.Split('-');
    byte[] array=new byte[arr.Length];
    for(int i=0; i<arr.Length; i++) array[i]=Convert.ToByte(arr[i],16);
    

    Method without Split: (Makes many assumptions about string format)

    int length=(s.Length+1)/3;
    byte[] arr1=new byte[length];
    for (int i = 0; i < length; i++)
        arr1[i] = Convert.ToByte(s.Substring(3 * i, 2), 16);
    

    And one more method, without either split or substrings. You may get shot if you commit this to source control though. I take no responsibility for such health problems.

    int length=(s.Length+1)/3;
    byte[] arr1=new byte[length];
    for (int i = 0; i < length; i++)
    {
        char sixteen = s[3 * i];
        if (sixteen > '9') sixteen = (char)(sixteen - 'A' + 10);
        else sixteen -= '0';
    
        char ones = s[3 * i + 1];
        if (ones > '9') ones = (char)(ones - 'A' + 10);
        else ones -= '0';
    
        arr1[i] = (byte)(16*sixteen+ones);
    }
    

    (basically implementing base16 conversion on two chars)

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

Sidebar

Related Questions

I have an SHA-1 byte array that I would like to use in a
I have byte array as input. I would like to convert that array to
I have the following string that I would like to Huffman-encode and store efficiently
I have a byte array that may or may not have null bytes at
I have a byte that is an array of 30 bytes, but when I
I have to create BYTE* array that will store some text and binary data
I have a byte array that can be of size 2,3 or 4. I
Hi all, I have an array of long that I would like to write
I have an X509Certificate2 certificate in my store that I would like to export
I have a UTF-8 byte array of data. I would like to search for

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.