使用IO在C#中读取十六进制 [英] Read hex in C# using IO

查看:231
本文介绍了使用IO在C#中读取十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C#转移到Java,似乎无法找到关于此的任何信息。我试图读取一个文件,如果在Java中使用十六进制,我使用...

  String s = Integer.toHexString(合心); 
if(s.length()<2){
s =0+ Integer.toHexString(hexIn);





$ b

因为我确定你知道这一点,所以如果读入的字节是1 characater long它增加了一个零来使它成为十六进制,我想为c#做同样的事情,到目前为止我有...

  StreamReader reader = new StreamReader(fileDirectory); 
long stickNoteLength = fileDirectory.Length;

int hexIn;
字符串十六进制=;

for(int i = 0;(hexIn = reader.Read())!= -1; i ++)
{

}

现在我被困住了,对不起,如果这是一个简单的问题,并且感谢您的帮助:)

解决方案

  string hex = String.Format({0:X2},hexIn); 

使用此格式掩码,您将获得0到32之间的数字(例如):

  00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F

我已添加 2 0:X2 ),因为您提到您正在读取字节
请注意,对于表示十六进制数字,在字符串开始处添加 0x 也是正确的:

  string hex = String.Format(0x {0:X2},hexIn); 


I'm moving from C# to Java and can't seem to find any information on this. I'm trying to read if a file using hex, in Java I use...

        String s = Integer.toHexString(hexIn);
        if(s.length() < 2){
            s = "0" + Integer.toHexString(hexIn);
        }

As I'm sure you know thats so if the byte read in is one characater long it adds a zero to make it hex, I wanna do the same thing for c# so far i have...

        StreamReader reader = new StreamReader(fileDirectory);
        long stickNoteLength = fileDirectory.Length;

        int hexIn;
        String hex = "";

        for (int i = 0; (hexIn = reader.Read()) != -1; i++)
        {

        }

Now I'm stuck, sorry if this is a simple question and thanks for you help :)

解决方案

string hex = String.Format("{0:X2}", hexIn);

With this formatting mask you will get for numbers from 0 to 32 (for example):

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F

I have added 2 (0:X2) because you mentioned that you are reading bytes. Note, that for representing hex numbers also will be correct to add 0x at the string beginning:

string hex = String.Format("0x{0:X2}", hexIn);

这篇关于使用IO在C#中读取十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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