Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from string ascii to string Hex

Tags:

string

c#

hex

ascii

Suppose I have this string

string str = "1234"

I need a function that convert this string to this string:

"0x31 0x32 0x33 0x34"  

I searched online and found a lot of similar things, but not an answer to this question.

like image 745
cheziHoyzer Avatar asked Apr 10 '13 08:04

cheziHoyzer


People also ask

How do you convert plain text to hex?

The most common way to convert ASCII text to hexadecimal numbers manually is to first look up the decimal number for the letter in the ASCII table. Then, convert this decimal value to its hexadecimal equivalent. Also you might find a conversion table that directly converts ASCII to hexadecimal.

Which function converts a string of ascii characters to hexadecimal values?

The bin2hex() function converts a string of ASCII characters to hexadecimal values.

How do you convert hex to string?

In order to convert a hex string into a normal string, the hex string has to be converted into a byte array, which is indexed and converted into smaller hex strings of two digits. The smaller hex strings are then concatenated into a normal string. For some values, a two digit hex string will start with a zero.


1 Answers

string str = "1234";
char[] charValues = str.ToCharArray();
string hexOutput="";
foreach (char _eachChar in charValues )
{
    // Get the integral value of the character.
    int value = Convert.ToInt32(_eachChar);
    // Convert the decimal value to a hexadecimal value in string form.
    hexOutput += String.Format("{0:X}", value);
    // to make output as your eg 
    //  hexOutput +=" "+ String.Format("{0:X}", value);

}

    //here is the HEX hexOutput 
    //use hexOutput 
like image 187
internals-in Avatar answered Sep 24 '22 20:09

internals-in