0

I convert my Hex to dump to get special character like symbol but when I try to convert my "0x18" i "\u0018" this value. Can anyone give me solution regarding this matter.

Here is my code:

    public static string FromHexDump(string sText)
    {
        Int32 lIdx;
        string prValue ="" ;
        for (lIdx = 1; lIdx < sText.Length; lIdx += 2)
        {
            string prString = "0x" + Mid(sText, lIdx, 2);
            string prUniCode = Convert.ToChar(Convert.ToInt64(prString,16)).ToString();
            prValue = prValue + prUniCode;
        }
        return prValue;
    }

I used VB language. I have a database that already encrypted text to my password and the value is BAA37D40186D like this so I loop it by step 2 and it will like this 0xBA,0xA3,0x7D,0x40,0x18,0x6D and the VB result getting like this º£}@m

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • I would reconsider your approach. Look here: https://stackoverflow.com/a/25639044/3135317 – FoggyDay Jan 17 '20 at 06:06
  • i want to convert my hex into special characters for example like this "0xA3" = £ but when this "0x18" i got like this "\u0018" – confidential account Jan 17 '20 at 06:11
  • Try adopting the example I showed you. – FoggyDay Jan 17 '20 at 06:14
  • Could you provide some dump *examples*? Say, `"abc0x1234def"` and expected results? – Dmitry Bychenko Jan 17 '20 at 07:20
  • 1
    @confidentialaccount .NET strings are Unicode. That's not a matter of opinion. You don't need any kind of escaping to include "special" characters in a Unicode string. `\u0018` is the C# escape sequence used to insert the *single, non printable* [Cancel Character](https://en.wikipedia.org/wiki/Cancel_character), a control character just like CR or LF, in a *string literal*, ie in code. The escape sequence for the pound character is `\u00A3`. Are you sure you aren't confusing the debugger's view for some non-existent escape sequence? – Panagiotis Kanavos Jan 17 '20 at 08:01
  • @PanagiotisKanavos hi sir. im sorry sir because im just a beginner in c# language. I used VB language. I have a database that already encrypted text to my password and the value is "BAA37D40186D" like this so I loop it by step 2 and it will like this 0xBA,0xA3,0x7D,0x40,0x18,0x6D and the VB result getting like this º£}@m – confidential account Jan 17 '20 at 08:13
  • @confidentialaccount it's no different in VB.NET. The runtime is the same, Unicode is the same, the escape sequences are the same. And the byte value that correspondes to `£` is the second one, `,0xA3`. In the string you just posted `0x18` is *invisible*. You posted 6 bytes but the string seems to contain only 5. – Panagiotis Kanavos Jan 17 '20 at 08:17
  • In any case, those bytes seem to be just the Latin1 values of the characters. You can get the string with eg `Encoding.GetEncoding(1252).GetString(new byte[] {0xBA,0xA3,0x7D,0x40,0x18,0x6D}).Dump();`. 1252 is the Latin1 codepage in Windows – Panagiotis Kanavos Jan 17 '20 at 08:34
  • What is the column's type? I suspect it's a binary column (eg varbinary), but the code treated it as a string – Panagiotis Kanavos Jan 17 '20 at 08:35
  • `that already encrypted text to my password` what does that mean? Did the code generate that string instead of storing the byte buffer to the database? You're asking people to reverse what *your* code did, simply by looking at the values. – Panagiotis Kanavos Jan 17 '20 at 08:37

3 Answers3

1

You can use this code:

var myHex = '\x0633';
var formattedString += string.Format(@"\x{0:x4}", (int)myHex);

Or you can use this code from MSDN (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types):

string hexValues = "48 65 6C 6C 6F 20 57 6F 72 6C 64 21";
string[] hexValuesSplit = hexValues.Split(' ');
foreach (string hex in hexValuesSplit)
{
    // Convert the number expressed in base-16 to an integer.
    int value = Convert.ToInt32(hex, 16);
    // Get the character corresponding to the integral value.
    string stringValue = Char.ConvertFromUtf32(value);
    char charValue = (char)value;
    Console.WriteLine("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
                        hex, value, stringValue, charValue);
}
BorHunter
  • 893
  • 3
  • 18
  • 44
0

The question is unclear - what is the database column's type? Does it contain 6 bytes, or 12 characters with the hex encoding of the bytes? In any case, this has nothing to do with special characters or encodings.

First, 0x18 is the byte value of the Cancel Character in the Latin 1 codepage, not the pound sign. That's 0xA3. It seems that the byte values in the question are just the Latin 1 bytes for the string in hex.

.NET strings are Unicode (UTF16LE specifically). There's no UTF8 string or Latin1 string. Encodings and codepages apply when converting bytes to strings or vice versa. This is done using the Encoding class and eg Encoding.GetBytes

In this case, this code will convert the byte to the expected string form, including the unprintable character :

new byte[] {0xBA,0xA3,0x7D,0x40,0x18,0x6D};
var latinEncoding=Encoding.GetEncoding(1252);
var result=latinEncoding.GetString(dbBytes);

The result is :

º£}@m

With the Cancel character between @ and m.

If the database column contains the byte values as strings :

  1. it takes double the required space and
  2. the hex values have to be converted back to bytes before converting to strings

The x format is used to convert numbers or bytes to their hex form and vice versa. For each byte value, ToString("x") returns the hex string.

The hex string can be produced from the original buffer with :

var dbBytes=new byte[] {0xBA,0xA3,0x7D,0x40,0x18,0x6D};
var hexString=String.Join("",dbBytes.Select(c=>c.ToString("x")));

There are many questions that show how to parse a byte string into a byte array. I'll just steal Jared Parson's LINQ answer :

public static byte[] StringToByteArray(string hex) {
    return Enumerable.Range(0, hex.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
}

With that, we can parse the hex string into a byte array and convert it to the original string :

var bytes=StringToByteArray(hexString);
var latinEncoding=Encoding.GetEncoding(1252);
var result=latinEncoding.GetString(bytes);
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
0

First of all u don't need dump but Unicode, I would recomend to read about unicode/encoding etc and why this is a problem with strings.

PS: solution : StackOverflow

EmerG
  • 1,046
  • 8
  • 13