Question String value to Hex value

engr.ahayee

Member
Joined
Aug 10, 2021
Messages
16
Programming Experience
Beginner
Hi all.
I want to do a simple task. take input from user and display its hex value in output field.
for example user input this string "210H1873DF12345"
The Hex value will be "32 31 30 48 31 38 37 33 44 46 31 32 33 34 35".
Following are the images of my interface and values stored in "data" variable.

image-1.png


image-2.png


program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sampleApplication
{
    public partial class Form1 : Form
    {
        public string input_String;
        public byte[] data = new byte[20];

        public Form1()
        {
            InitializeComponent();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            input_String = txtInput.Text;

            for(int i=0; i < input_String.Length; i++)
            {
                data[i] = Convert.ToByte(input_String[i]);
            }
        }
    }
}

kindly suggest me some solutions
Thanks
 
Convert the byte to hex with either value.ToString("X") or Convert.ToString(value, 16) (where 16 means hex base)

Docs:
 
Convert the byte to hex with either value.ToString("X") or Convert.ToString(value, 16) (where 16 means hex base)
You probably ought to use value.ToString("X2"), so that values below 16 are padded to two characters.
 
I use string builder for displaying data. Following code is running as I want.
Kindly tell if this approach is fine or not.

program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sampleApplication
{
    public partial class Form1 : Form
    {
        public string input_String;
        public byte[] data = new byte[20];
        public int i;

        StringBuilder sb = new StringBuilder();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            input_String = txtInput.Text;

            for(int i=0; i < input_String.Length; i++)
            {
                data[i] = Convert.ToByte(input_String[i]);
            }

            //txtOutput.Text = data[0].ToString("X2");  // This line is For testing purpose. How to display byte value in hex format
            
            //// for displaying data on Output Textbox

            //StringBuilder sb = new StringBuilder(data.Length * 2);

            foreach (byte b in data)
            {
                sb.AppendFormat("{0:X2}",b);
                sb.AppendFormat(" ", b);
            }

            txtOutput.Text = sb.ToString();
            ////---------------------------------------
        }
    }
}

image-3.png


Thanks
 
It's easier than that:
C#:
var hex = string.Join(" ", byteArray.Select(b => b.ToString("X2")));
To go the other way:
C#:
var byteArray = hex.Split().Select(s => Convert.ToByte(s, 16)).ToArray();
 
LOL! I used to think the same way about the functional style of programming espoused by LINQ -- it's too complicated. After a few weeks of using LINQ, I started to see the functional style as less complicated than the traditional procedural style. It takes a little bit of a mental shift to see the beauty and utility of the functional style.
 
Why i am getting this error

View attachment 1652it was running fine but now it is giving this error
Look at the title of this thread. What does this have to do with that? Nothing at all. One thread per topic and one topic per thread please. If you have a question on a different topic, start a new thread with all and only the information relevant to that question. I will be breaking this out into its own thread but please do it yourself in future.
 
Back
Top Bottom