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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:26:27+00:00 2026-06-05T19:26:27+00:00

Aim : To convert a integer value first to hexstring and then to byte[].

  • 0

Aim :

To convert a integer value first to hexstring and then to byte[].

Example :

   Need to convert  int:1024 to hexstring:400 to byte[]: 00000100 00000000

Method:

For converting from integer to hex string i tried below code

    int i=1024;
    string hexString = i.ToString("X");

i got hexstring value as “400”. Then i tried converting hex string to byte[] using below code

    byte[] value = HexStringToByteArray(hexValue);

    /* function for converting hexstring to  byte array */
    public  byte[] HexStringToByteArray(string hex)
    {

        int NumberChars = hex.Length;

        if(NumberChars %2==1)
          throw new Exception("Hex string cannot have an odd number of digits.");

        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;

    }

Error:

Here i got the exception “Hex String cannot have a odd number of digits”

Solution: ??

  • 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-06-05T19:26:29+00:00Added an answer on June 5, 2026 at 7:26 pm

    You can force the ToString to return a specific number of digits:

    string hexString = i.ToString("X08");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

AIM : Convert a resultset to XML and assign the result to a variable
my aim is to invoke a shell script from a PHP program and then
My aim is to stream a .htm file via Response.WriteFile(Sample.htm); and then access a
I need to create a porting software that will convert Java code to C#
I need to parse files written in some languages(Java, C, C#...) and then trace
so my aim is to make a IF statement determine what the value of
My ultimate aim is to convert the below code in python to C#, but
my aim is to compare the first names with the title of an img-element
Aim: my UINavigationBar has to be customized with a certain background and a specific
Aim: To have the if statement execute a onclick event through code. I currently

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.