c#从控制台读取希伯来语文本 [英] c# reading Hebrew text from console

查看:62
本文介绍了c#从控制台读取希伯来语文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows 10(也许是问题所在:-))

I'm using windows 10 (maybe this is the problem:-) )

我有一个简单的代码,可以从控制台读取希伯来语中的文本,并打印为十六进制\ DEC值

I have a simple code that reads text in Hebrew from console them print it's HEX\DEC value

但是他一直都给我00在控制台窗口上,我可以看到希伯来字母

but he give me 00 all the time on the console window I can see the Hebrew letters

为什么有什么原因?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Timers;
using System.IO;

namespace HebTest
{
    class Program
    {
        static public void Main(string[] args)
        {

            Console.WriteLine("Write your message here  - ");

            string StringMessage = Console.ReadLine();


            Console.WriteLine("print string  - " + StringMessage);
            ///message in HEX
            byte [] ByteMessage = Encoding.Default.GetBytes(StringMessage);
            string HexMessage = BitConverter.ToString(ByteMessage); 

            Console.WriteLine("MSG in HEX -  " + HexMessage);
            Console.Write( 
                          Encoding.Default.GetString(ByteMessage)                   
                          );

            Console.WriteLine();
            foreach (byte p in ByteMessage)
            {
                Console.Write((char)p + "  -  " +  p );
                Console.WriteLine("");


            }

}

例如,我输入此文本测试אבגד"这就是我得到的:

for example I enter this text "test אבגד" and this is what I got :

Write your message here  -
test אבגד                         ---> this I wrote on the console
print string  - test
MSG in HEX -  74-65-73-74-20-00-00-00-00
test     
t  -  116
e  -  101
s  -  115
t  -  116
   -  32
   -  0
   -  0
   -  0
   -  0

我想念什么?

谢谢,

推荐答案

您正在使用 Encoding.Default 将字符串转换为二进制.这几乎总是一个坏主意-这意味着相同的代码可能在某些机器上可以工作,而在其他机器上却不能.当您要在该计算机上读取/写入文本文件时,很有用,并且确保确保系统默认编码是正确的选择任何运行它的机器.真是难得.

You're using Encoding.Default to convert the string into binary. That's almost always a bad idea - it means the same code may work on some machines and not on others. It's pretty much only useful when you want to read/write a text file on that machine, and you're sure that the system default encoding is the right one to use for any machine that runs it. That's rare.

特别是,您尝试与外部设备通话-这意味着您需要使用 期望的编码.您应该找出答案,并使用适当的编码.例如,如果设备需要UTF-8,请使用:

In particular, you're trying to talk to an external device - which means you need to use the encoding it expects. You should find that out, and use the appropriate encoding. For example, if the device expects UTF-8, use:

// Local variable names changed to be more idiomatic C#
string text = Console.ReadLine();
byte[] bytes = Encoding.UTF8.GetBytes(text);

正如您现在所指示的,该设备期望使用Windows-1255,即代码页1255,因此您将获得如下所示的编码:

As you've now indicated that the device expects Windows-1255, that's code page 1255, so you get the encoding like this:

Encoding.GetEncoding(1255)

为澄清起见,我可能为此使用一个单独的变量:

For clarify, I'd probably use a separate variable for that:

string text = Console.ReadLine();
Encoding encoding = Encoding.GetEncoding(1255);
byte[] bytes = encoding.GetBytes(text);

这篇关于c#从控制台读取希伯来语文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆