C#以16进制读取一次一个字节的文件 [英] C# read file as hex one byte at a time

查看:511
本文介绍了C#以16进制读取一次一个字节的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

我是Java的C#的新手,我在最近的两个小时里被困在一些简单的东西上,或者应该如此想知道是否有人会帮助我:)

<在Java中,我使用代码beleow读取文件,它使用十六进制读取给定的文件,一次一个字节?在C#中这样做的方法是什么?

  int hexIn; 

档案档案=新档案(filePath);

FileInputStream fis = new FileInputStream(file);

for(int i = 0;(hexIn = fis.read())!= -1; i ++){

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




$ b $ p
$ b

对不起,如果这看起来很糟糕,我只是卡住了!非常感谢!



:)

解决方案

试试这个,它是从你发布的代码中完全转换而来的:

  using(var file = File.Open(p:\ \t.txt,FileMode.Open))
{
int b; ((b = file.ReadByte())> = 0)

{
string s = b.ToString(X);
if(s.Length <2)
s =0+ s;

}
}


Possible Duplicate:
Read hex in C# using IO

Hi, I'm new to C# from Java and I have been stuck for the last two hours on something simple, or what should be so was wondering if someone would help me please :)

In Java I read in a file using the code beleow, it reads a given file in using hex, one byte at a time? What would be a way to do this in C#?

int hexIn;

File file = new File(filePath);

FileInputStream fis = new FileInputStream(file);

 for(int i = 0; (hexIn = fis.read()) != -1; i++){

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

Sorry if this seems dum I'm just stuck! Many thanks in advance!

:)

解决方案

Try this, it is quite a straight conversion from the code you posted:

        using (var file = File.Open("p:\\t.txt", FileMode.Open))
        {
            int b;
            while ((b = file.ReadByte()) >= 0)
            {
                string s = b.ToString("X");
                if (s.Length < 2)
                    s = "0" + s;

            }
        }

这篇关于C#以16进制读取一次一个字节的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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