获取字符串索引

时间:2017-04-13 05:59:08

标签: c string loops

我的代码如下:

cc_string = "378282246310005"
for (int i = 0, n = strlen(cc_string); i < (n / 2); i++)
     {
         index = cc_string[i];
         sum += (index * 2);
         printf("%i\n", sum);
     }

但我的输出总是(字符串中的每个数字都加在一起):

102
212
324
424
536
636
736

而不是:

22
38
42
50
56
56
56

1 个答案:

答案 0 :(得分:1)

问题是你使用ASCII数字作为数字,这是不正确的。

ASCII '1'0x31十六进制或49十进制。

因此,您的第一个循环会为您提供102 = '3'(0x33, 51) * 2

要修复它,请简单地将“0”减去ASCII数字:

index = cc_string[i] - '0';

显然,你问题中的被驱逐输出没有任何意义,因为如果sum被初始化为6,那么首先sum0不是sum

因此预期输出可以是以下加上6 20 36 40 56 60 64 初始值

的偏移量
// Existing Geometry Collection
pExGeomColl = (IGeometryCollection)new PolylineClass();
pExGeomColl = (IGeometryCollection)pBaseFeat.ShapeCopy;
// New Geometry collection
pNewGeomColl = (IGeometryCollection)new PolylineClass();
pMAware = (IMAware)pNewGeomColl;
pMAware.MAware = true;
pZAware = (IZAware)pNewGeomColl;
pZAware.ZAware = bHasZ;

for (int i = 0; i < hGeomToFrom.Count; i++)
{
    iTo = i;
    iFrom = (int)hGeomToFrom[i];
    pGeom = pExGeomColl.Geometry[iFrom];
    pGeom.SpatialReference = pSpRef;
    pMAware = (IMAware)pGeom;
    pMAware.MAware = true;
    pZAware = (IZAware)pGeom;
    pZAware.ZAware = bHasZ;
    // Convert the geometry to a polyline
    pPolyline = (IPolyline6)new PolylineClass();
    if (pGeom.GeometryType != esriGeometryType.esriGeometryPolyline)
    {
        pPolyline = geometryToPolyline(pGeom, bHasZ, true, ref sError);
        if (sError.Length > 0)
        {
            sError = "cmdReset\r\n" + sError;
            clsMain.write_log(sError, clsMain.m_eLogType.FATAL);
            MessageBox.Show(sError);
            return;
        }
    }
    else
    {
        pPolyline = (IPolyline)pGeom;
    }
    pMAware = (IMAware)pPolyline;
    pMAware.MAware = true;
    pZAware = (IZAware)pPolyline;
    pZAware.ZAware = bHasZ;


    dLen = pPolyline.Length;
    dFrom = dMeasure;
    dTo = dFrom + dLen;
    pSeg = (IMSegmentation4)pPolyline;
    pSeg.SetAndInterpolateMsBetween(dFrom, dTo);

    IGeometryCollection pXGeomColl = new PolylineClass();
    pMAware = (IMAware)pXGeomColl;
    pMAware.MAware = true;
    pZAware = (IZAware)pXGeomColl;
    pZAware.ZAware = bHasZ;
    pXGeomColl = (IGeometryCollection)pPolyline;
    for (int j = 0; j < pXGeomColl.GeometryCount; j++)
        pNewGeomColl.AddGeometry(pXGeomColl.Geometry[j]);

    dMeasure = dTo;

    ProgressBar1.Value = iCount;
    iCount++;
}

pGeom = (IGeometry)pNewGeomColl;
pGeom.SpatialReference = pSpRef;
pMAware = (IMAware)pGeom;
pMAware.MAware = true;
pZAware = (IZAware)pGeom;
pZAware.ZAware = bHasZ;
pBaseFeat.Shape = pGeom;

pBaseFeat.Store();