qq_31096909 2015-11-26 08:38 采纳率: 0%
浏览 2347
已结题

将程序中的ascii码改为16进制应该怎么该!求指导

try
{

            // Translate the passed message into ASCII and store it as a Byte array.
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
            // Get a client stream for reading and writing.
            //  Stream stream = client.GetStream();

            NetworkStream stream = client.GetStream();

            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);

            Console.WriteLine("Sent: {0}", message);

            // Receive the TcpServer.response.

            // Buffer to store the response bytes.
            data = new Byte[256];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            return responseData;
            //Console.WriteLine("Received: {0}", responseData);

            // Close everything.
            stream.Close();

        }
        catch (ArgumentNullException e)
        {
  • 写回答

2条回答 默认 最新

  • 小小Q_Forler 2015-11-26 09:03
    关注

    public class TestAscii {

    public static void main(String[] s){
    printAsciiHex('a');
    printAsciiHex('1');

    printHex(1);
    printHex(16);
    printHex(18);
    printHex(255);
    }

    public static void printAsciiHex(char ch){
    int i=(int)ch; //i=a的ASCII
    System.out.println(ch + " ascii:" + i);

    String str=Integer.toString(i,16); //十六进制
    System.out.println(ch + " ascii 的十六进制:" + str);
    }

    //得到一个数的十六进制形式
    public static void printHex(int i){
    String str=Integer.toString(i,16); //十六进制
    System.out.println(i + "的十六进制:" + str);
    }
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?