Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i prefix the converted hex value with hex escape value '\x' in c#.The below given function.

C#
       private void button1_Click(object sender, EventArgs e)
       {
           Open();
           string str = "نص";
           string str1 = ToHexString(str);
           textBox1.Text = str1;
           StringBuilder receipt = new StringBuilder();
           receipt.Append("\x1B\x40");         // Initialize Printer
           receipt.Append("\x1B\x74\x28");     // Select Arabic code page (Page 40,1256)
           receipt.Append(str1);               // The Arabic text to be printed
           receipt.Append("\x0A");             // Print and line feed
           receipt.Append("\x1D\x56\x42\x00"); // Select cut mode and cut paper
           string szString = receipt.ToString();
           PrintText(Printer, szString);

       }


public string ToHexString(string str)
       {
           var sb = new StringBuilder();
           var encoding = Encoding.GetEncoding(1256);
           var bytes = encoding.GetBytes(str);
           foreach (var t in bytes)
           {
               sb.Append(t.ToString("X2"));
           }
           return sb.ToString();
       }

the output of str1 is 'E4D5' and i need to get it like '\xE4\xD5'

What I have tried:

I have been trying to print an arabic thermal print and finally got a solution to convert it to arabic charector using windows arabic code 1256. But the printer needs an escape charector '\x' to read each and every hexa values.
Posted
Updated 6-Dec-16 2:38am
v4
Comments
Philippe Mori 6-Dec-16 9:01am    
There is an inconsistency in your code and your question... As presented, it seems that you want to escape only the content of str1 but not commands like Initialize Printer.

If you would hardcode str1, would you write receipt.Append("\xE4\xD5") or receipt.Append("\\xE4\\xD5") or receipt.Append(@"\xE4\xD5") or receipt.Append("E4D5")?

Would that hard-coded string works? Which ones?
Nadir Muhammed 6-Dec-16 9:04am    
when hard coding this works fine 'receipt.Append("\xE4\xD5")'
Philippe Mori 6-Dec-16 9:26am    
Well, then your problem is that you don't seems to realize that when you write "\xE4", it means a string with character code E4 and not the string @"\xE4". Thus you ask an incorrect question. OriginalGriff exactly respond what you have asked but it was not what you wanted. If appending "\xE4\xD5" works, then you need to send the bytes values. Thus even ToHexString function is incorrectly named. You would have to do something like sb.Append((char)t);
Philippe Mori 6-Dec-16 9:16am    
By the way, string in C# are in UTF-16. So there might be some conversion from that encoding to the one used to send data to the printer. We have to know if you are able to properly print using any of the above.

It might also be useful to show how you would print "ABC" and to test if that works.

The information in the link you provide (ESC-POS Arabic letters is separated) are somewhat different that the one here. It would be better to provide all necessary informations.

By the way that question does not make much sense. What does it means that letters are separated? Would you expect E4D5 without a space? It cannot be since this is an hex dump of bytes and bytes are 2 characters wide when printed in HEX.

1 solution

Try:
C#
foreach (var t in bytes)
    {
    sb.AppendFormat("\\x{0:X2}", t);
    }
 
Share this answer
 
Comments
Nadir Muhammed 6-Dec-16 8:26am    
It will return with an escape charector '\\xE4\\xD5' i dont need that escape charector
i need the output look like this '\xE4\xD5'
OriginalGriff 6-Dec-16 8:34am    
No, it won't - it shows that way in the code because backslash is a C# string escape character, and it shows that way in the debugger for the same reason.
Print it to the console, and you'll see what I mean:
Console.WriteLine(str1);
Nadir Muhammed 6-Dec-16 8:39am    
iam passing it to a thermal printer for printing. and need to have only one hexa escape sequence in the last to identify the end and begning..i have improved the question.please check it
OriginalGriff 6-Dec-16 8:46am    
And did you try it?
Nadir Muhammed 6-Dec-16 8:51am    
yes ..but it is not working..its not the readablity it matters

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