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

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're adding the UILabel to the cell's contentView, but asking… May 12, 2026 at 10:12 am
  • Editorial Team
    Editorial Team added an answer The IAsyncCallback interface does not exist, so you cannot implement… May 12, 2026 at 10:12 am
  • Editorial Team
    Editorial Team added an answer It is the default in the latest CTP release and… May 12, 2026 at 10:12 am

Related Questions

I have a code base that makes extensive use of files to represent a
I have an array of unsigned chars in c I am trying to print
I have created a dynamic typing system in C in order to create a
I am trying to create a graphical spectrum analyzer in python. I am currently
I have c++ code that attempts to dynamically allocate a 2d array of bytes

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.