ccsv 编码转换_编码转换 - antony.net - 博客园

C# GB2312 转 UTF-8

http://blog.chinaunix.net/u1/46538/showart_396923.html

public void EncryptFile(string strInFileName, string strOutFileName)

{

try

{

byte[] data = File.ReadAllBytes(strInFileName);

StreamWriter sw = new StreamWriter(strOutFileName, false, Encoding.Unicode);

char[] buffer2 = Encoding.Unicode.GetChars(data);

sw.Write(buffer2);

sw.Flush();

sw.Close();

}

catch (Exception ex)

{

throw ex;

}

}

public void DecryptFile(string strInFileName, string strOutFileNameV)

{

StreamReader sr = new StreamReader(strInFileName, System.Text.Encoding.Unicode);

StreamWriter sw = new StreamWriter(strOutFileNameV, false, System.Text.Encoding.GetEncoding("Shift-JIS"));

try

{

char[] buffer;

while (!sr.EndOfStream)

{

buffer = System.Text.Encoding.GetEncoding("Shift-JIS").GetChars(System.Text.Encoding.Unicode.GetBytes(sr.ReadLine()));

sw.WriteLine(buffer);

}

sw.Flush();

sw.Close();

sr.Close();

}

catch (Exception ex)

{

throw ex;

}

}

bool writeunicodecsv()

{

StreamReader sr = new StreamReader(@"D:\q.csv", System.Text.Encoding.GetEncoding("Shift-JIS"));

StreamWriter sw = new StreamWriter(@"D:\qUnicode.csv", false, Encoding.Unicode);try

{

string strline;

string  strWrite;

int i = 0;

while (!sr.EndOfStream)

{

i++;

strline = sr.ReadLine();

strWrite = System.Text.Encoding.Unicode.GetString(System.Text.Encoding.GetEncoding("Shift-JIS").GetBytes (strline));

sw.WriteLine(strWrite);

}

sr.Close();

sw.Flush();

sw.Close();

MessageBox.Show(i.ToString());

}

catch (Exception ex)

{

throw ex;

MessageBox.Show(ex.Message);

}

return true;

}

bool readcsv()

{

string strReadCsvFile = @"D:\qUnicode.csv";

string strWriteCsvFile = @"D:\qshiftjis.csv";

StreamReader sr = new StreamReader(strReadCsvFile, System.Text.Encoding.Unicode);

StreamWriter sw = new StreamWriter(strWriteCsvFile, false, System.Text.Encoding.GetEncoding("Shift-JIS"));

try

{

string strline;

string strWrite;

int i = 0;

while (!sr.EndOfStream)

{

i++;

strline = sr.ReadLine();

strWrite = System.Text.Encoding.GetEncoding("Shift-JIS").GetString(System.Text.Encoding.Unicode.GetBytes(strline));

sw.WriteLine(strWrite);

}

sr.Close();

sw.Flush();

sw.Close();

}

catch (Exception ex)

{

throw ex;

MessageBox.Show(ex.Message);

}

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用C语言中的libcsv库来实现将Excel文件转换csv文件的编码转换。 以下是基本的步骤: 1. 使用Excel软件将文件另存为CSV格式。 2. 使用libcsv库中的csv_open函数打开CSV文件。 3. 使用csv_read函数读取CSV文件中的数据行。 4. 使用libiconv库中的iconv函数将读取的每行数据进行编码转换。 5. 使用csv_write函数将转换后的数据写入新的csv文件。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iconv.h> #include <csv.h> int main(int argc, char *argv[]) { char *input_file = "input.csv"; char *output_file = "output.csv"; char *input_encoding = "gb2312"; char *output_encoding = "utf-8"; csv_t *csv_input = csv_open(input_file, "r", ',', '"'); csv_t *csv_output = csv_open(output_file, "w", ',', '"'); char **row; int row_num = 0; while ((row = csv_read(csv_input)) != NULL) { int col_num = csv_get_field_num(csv_input); char **new_row = (char **)malloc(col_num * sizeof(char *)); int i; for (i = 0; i < col_num; i++) { char *old_value = row[i]; size_t old_len = strlen(old_value); size_t new_len = old_len * 4; char *new_value = (char *)malloc(new_len); memset(new_value, 0, new_len); iconv_t cd = iconv_open(output_encoding, input_encoding); char *inbuf = old_value; char *outbuf = new_value; size_t inbytesleft = old_len; size_t outbytesleft = new_len; iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); iconv_close(cd); new_row[i] = new_value; } csv_write(csv_output, new_row); csv_free_row(new_row); csv_free_row(row); row_num++; } csv_close(csv_input); csv_close(csv_output); return 0; } ``` 这段代码仅供参考,具体实现可能需要根据您的具体需求进行修改。同时,需要注意一些细节问题,例如异常处理、内存管理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值