2016-06-09 72 views
0

我試圖將A1表示法轉換爲int類型行和列,0起始索引。我得到了一個用C語言編寫的代碼,並試圖製作一個C#代碼。列是好的,但我在行中遇到問題。c#將A1表示法轉換爲R1C1算法

異常詳細信息:System.FormatException:輸入字符串的不正確的格式

row_col.Add( 「行」,int.Parse(cellAddr + II));

private Dictionary<string, int> ParseA1Notation(string cellAddr) 
{ 
    Dictionary<string, int> row_col = new Dictionary<string, int>(); 
    int ii = 0, jj, colVal = 0; 
    while (cellAddr[ii++] >= 'A') { }; 
    ii--; 

    for (jj = 0; jj < ii; jj++) colVal = 26 * colVal + cellAddr[jj] - 'A' + 1; 
    row_col.Add("col", colVal); 
    row_col.Add("row", int.Parse(cellAddr + ii)); 
    return row_col; 
} 

繼承人原來的C代碼..

void rc(char * cellAddr, int *row, int *col) { 
    int ii=0, jj, colVal=0; 
    while(cellAddr[ii++] >= 'A') {}; 
    ii--; 
    // ii now points to first character of row address 
    for(jj=0;jj<ii;jj++) colVal = 26*colVal + toupper(cellAddr[jj]) -'A' + 1; 
    *col = colVal; 
    *row = atoi(cellAddr+ii); 
} 
+0

什麼是你的'cellAddr'輸入什麼樣子的? –

+0

字符串.. Excel像「A1」,「B2」,「C3」..即時嘗試將其轉換爲(0,1),(2,3)..類似的東西.. –

+1

看看'row_col.Add (「行」,Int32.Parse(cellAddr.Substring(ii)))'作品 –

回答

1

請更改線路row_col.Add("row", int.Parse(cellAddr + ii))row_col.Add("row", Int32.Parse(cellAddr.Substring(ii)))