顯示具有 C++ 標籤的文章。 顯示所有文章
顯示具有 C++ 標籤的文章。 顯示所有文章

2019年5月7日 星期二

Press F5 to produce pseudo C code out of IDA disassembly

這幾天才發現原來用ida pro做逆向,
按F5就可以得到C語言的偽源碼了 XD
參:
https://www.youtube.com/watch?v=gYkDcUO9otQ

2011年1月30日 星期日

c語言 陣列問題

I*V = 3*V3 + 4*V4 + 5*V5 + 6*V6 + 7*V7 + 8*V8 + 9*V9 + 10*V10 + 11*V11 + 12*V12

以上公式
將V3~V12設為陣列並寫入類別

將設定好的數放入
例如
(1,2,3,4,5)
(1,2,3,4)
(1,2,3)
但是在輸出後沒用到的陣列就別輸出了
例如
(1,2,3,4,5)
==>3*V3 + 4*V4 + 5*V5 + 6*V6 + 7*V7 = 3*1+4*2+5*3+6*4+7*5
(1,2,3,4)
==>3*V3 + 4*V4 + 5*V5 + 6*V6 = 3*1+4*2+5*3+6*4

該怎麼寫?

另解:
#include <cstdlib>
#include <iostream>
#include <stdarg.h>

using namespace std;

int IV2(int *V3, ...)
{
    int i, ans=0;

    int *num;
    va_list vl;
    va_start(vl, V3);

    ans = 3*(*V3);
    i=3;
    cout<<i<<"*"<<"V"<<i;

    do
    {
        i++;
        num = va_arg(vl, int*);

        if(num == NULL)goto end;
        ans = ans + i*(*num);

        cout<<" + "<<i<<"*"<<"V"<<i;
    }while(num != NULL);

    end:
    va_end(vl);
    cout<<" = ";
    cout<<ans<<endl;    
}

int main()
{
    int v[5] = {1,2,3,4,5};

    IV2(&v[0], &v[1], &v[2], &v[3], NULL);

    IV2(&v[0], &v[1], &v[2], &v[3], &v[4], NULL);

    system("PAUSE");
    return 0;
}

2010年11月29日 星期一

C超新手常犯的錯誤

通常strncpy(str1, str2, 8);
並不會在str1[8]自動加上string結尾,
所以請自行加上,str1[8] = NULL;

2010年7月12日 星期一

(轉)int 与 byte[] 的相互转换 - 沐枫小筑 - 博客园

int 与 byte[] 的相互转换 - 沐枫小筑 - 博客园:
int 與 byte[] 的相互轉換 - 沐楓小築 - 博客園:
1. 最普通的方法
  • 從byte[] 到 uint
    b = new byte[] {0xfe,0x5a,0x11,0xfa};
    u
    = (uint)(b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24);
  • 從int 到 byte[]
    b[0] = (byte)(u);
    b[
    1] = (byte)(u >> 8);
    b[
    2] = (byte)(u >> 16);
    b[
    3] = (byte)(u >> 24);

2. 使用 BitConverter (強力推薦)

  • 從int 到byte[]
    byte[] b = BitConverter.GetBytes(
    0xba5eba11 );
    //{0x11,0xba,0x5e,0xba}
  • 從byte[]到int
    uint u = BitConverter.ToUInt32(
    new byte[] {0xfe, 0x5a, 0x11,
    0xfa}
    ,0 ); // 0xfa115afe

3. Unsafe代碼 (雖然簡單,但需要更改編譯選項)






unsafe

{

// 從int 到byte[]

fixed ( byte* pb = b );

// 從byte[] 到 int

u = *((uint*)pb);

}






4. 使用Marshal類    
IntPtr ptr = Marshal.AllocHGlobal(4); // 要分配非託管內存
byte[] b= new byte[4]{1,2,3,4};
//從byte[] 到 int
Marshal.Copy(b, 0, ptr, 4);
int u = Marshal.ReadInt32(ptr);
//從int 到byte[]
Marshal.WriteInt32(ptr, u);
Marshal.Copy(ptr,b,
0,4);
Marshal.FreeHGlobal(ptr);
// 最後要記得釋放內 存

使用第4種看起來比較麻煩,實際上,如果想把結構(struct)類型轉換成byte[],則第4種是相當方便的。例如:

int len = Marshal.Sizeof(typeof(MyStruct));
MyStruct o;
byte[] arr = new byte[len];//{};

IntPtr ptr
= Marshal.AllocHGlobal(len);
try
{
// 從byte[] 到struct MyStruct
Marshal.Copy(arr, index, ptr, Math.Min(length, arr.Length - index));
o
= (MyStruct)Marshal.PtrToStructure(ptr, typeof(MyStruct));


// 從struct MyStruct 到 byte[]
Marshal.StructureToPtr(o, ptr, true); // 使用時要注意fDeleteOld參數
Marshal.Copy(ptr, arr, 0, len);
}

finally
{
Marshal.FreeHGlobal(ptr);
}

return o;