[nevermind了,多谢关心]有一些编程的问题需要大哥/大姐们的帮忙

陪你去看龙卷风

新手上路
VIP
注册
2002-10-12
消息
11,271
荣誉分数
61
声望点数
0
例如我有一个文件,我想,一个字符一个字符的读进来然后转化为hex然后再保存到新的文件去

例如我有个文件叫original.txt
里头有binary data (16 00 00 00 18 00 00 00),我想读进来以后保存到hex.txt
变成(16 18)

C++ 或者 JAVA
随便哪种语言写得都行

:thanks:
 
#include <atlfile.h>
#include <atlenc.h>

int _tmain(int argc, _TCHAR* argv[])
{
CAtlFile src;
if(SUCCEEDED(src.Create(_T("C:\\original.txt"), GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS)))
{
ULONGLONG nSize = 0;
src.GetSize(nSize);
LPBYTE pbyte = new BYTE[nSize];
DWORD dwRead = 0;
if(SUCCEEDED(src.Read(pbyte, nSize, dwRead)))
{
int nBufferSize = AtlHexEncodeGetRequiredLength(dwRead);
LPSTR psz = new CHAR[nBufferSize];
if(AtlHexEncode(pbyte, dwRead, psz, &nBufferSize))
{
CAtlFile dst;
if(SUCCEEDED(dst.Create(_T("C:\\hex.txt"), GENERIC_WRITE, FILE_SHARE_WRITE, CREATE_ALWAYS)))
{
dst.Write(psz, nBufferSize);
dst.Close();
}
}
delete psz;
}
delete pbyte;
src.Close();
}
return 0;
}
 
Re: 有一些编程的问题需要大哥/大姐们的帮忙

最初由 陪你去看龙卷风 发布
例如我有一个文件,我想,一个字符一个字符的读进来然后转化为hex然后再保存到新的文件去

例如我有个文件叫original.txt
里头有ABCDEFG,我想读进来以后保存到hex.txt
变成41 42 43 44 45 46 47

C++ 或者 JAVA
随便哪种语言写得都行

:thanks:

代码:
#include "fstream.h";

int main () {
  ifstream input_data;
  ofstream output_data;
  input_data.open("c:/whatever/whatever.txt");
  output_data.open("c:/whatever/newfile.txt");
  string s;
  int num;
  while (!input_data.eof()){
    getline(input_data, s);
    for (int i = 0; i < s.length; i++) {
      num = <int> s[i]; //some sort of cast here, i don't know the exact syntax
      output_data << num; 
}
}

input_data.close();
out_data.close();
return 0;
}

something like this, can't remember
 
最初由 dpff 发布

#include <atlfile.h>
#include <atlenc.h>

............

咱们这个有点太高深了........
连struct都上了,还有自己的function。。。。
 
import java.io.*;

public class ASCII_convertor
{
private static final char[] HexChars = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};


public static void main( String args[] )
throws Exception
{
File fin = new File("./input.txt");
File fout = new File("./output.txt");

BufferedReader r = new BufferedReader (new InputStreamReader (new FileInputStream(fin)) );
OutputStreamWriter w = new OutputStreamWriter( new FileOutputStream(fout) );
PrintWriter pw = new PrintWriter( w, true );

String line = r.readLine();
while (line != null )
{
String hexLine = toHexString(line.getBytes("UTF-8"));
pw.println( hexLine );
System.out.println( hexLine );
line = r.readLine();

}

r.close();
pw.close();

} // main


public static final String toHexString(byte[] bytes) {

StringBuffer sb = new StringBuffer();

for (int i=0; i < bytes.length; i++) {
sb.append(HexChars[(bytes >> 4) & 0xf]);
sb.append(HexChars[bytes & 0xf]);
sb.append(" ");
}
return new String(sb);
}

}
 
后退
顶部