Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.25/5 (4 votes)
See more:
Hi,

How to convert "!q44499:²о888=º88Š" to "19217134343439393AB205D0BE383838133DBA38388A". I have tried Encoding.Default.GetBytes("!q44499:²о888=º88Š"). But for character "²" I get "3f", instead of "b2".Please help.
Posted
Updated 19-May-22 18:27pm

Maybe this answer would work???

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
class Convert
{

    public void DoConversion()
    {
        string str = "test";
        byte[] byteArray = null;
        System.Text.StringBuilder hexNumbers = new System.Text.StringBuilder();
        byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
        for (int i = 0; i <= byteArray.Length - 1; i++) {
            hexNumbers.Append(byteArray[i].ToString("x"));
        }
        Interaction.MsgBox(hexNumbers.ToString());
        // Out  put will be 74657374

        // The following code will reverse the operation and verify the output:
        string st = hexNumbers.ToString();
        string com = "";
        for (x = 0; x <= st.Length - 1; x += 2) {
            com += Strings.ChrW(Convert.ToInt32("&H" + st.Substring(x, 2)));
        }
        Interaction.MsgBox(com);
    }
}



http://stackoverflow.com/questions/25129402/how-to-convert-ascii-to-hexdecimal-in-vb-net[^]
 
Share this answer
 
v2
C#
public static string AsciiToHex(string asciiString)
       {
           StringBuilder builder = new StringBuilder();
           foreach (char c in asciiString)
           {
               builder.Append(Convert.ToInt32(c).ToString("X"));
           }
         return builder.ToString();
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900