Dear friends,

do you know how to convert the following codes into C # ?

Tks,
hendy

BYTE ResultSN[11];
char SN[100];
BYTE TagType;


if ((TagType == 4) || (TagType == 5))
{

  memcpy(SN,ResultSN, 7);
  StrMsg.Format("Card Serial: %02x %02x %02x %02x %02x %02x %02x ",ResultSN[0],ResultSN[1],ResultSN[2],ResultSN[3],ResultSN[4],ResultSN[5],ResultSN[6] );
		 
}

Recommended Answers

All 7 Replies

Do you know either C# or C++ ?

as that code is C, not C++, unlikely.
You can't convert that code from C++ to C# for the same reason.

i will post the conversion for you, give me a minute.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinAPI
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender , EventArgs e) {
            byte[] ResultSN = new byte[11];
            char[] SN = new char[100];
            byte TagType = new byte();
            if ((TagType == 4) || (TagType == 5))
            {
                winapi.memcpy(SN , ResultSN , 7);
            }
        }
    }
    class winapi
    {
        [DllImport("msvcr71.dll" , CharSet = CharSet.Auto)]
        public static extern void memcpy(char[] src , byte[] dest , int count);
    }
}

you should note though that this code actually does nothing, because it has no return type and because when i try to use pointers (via the unsafe context) it throws errors it is probably pointless to continue trying to convert this.

Actually, it's a part of a bigger function..
i only quote some parts which i don't know how to convert it into C#...

yes, you're great but how about these codes :

StrMsg.Format("Card Serial: %02x %02x %02x %02x %02x %02x %02x ",ResultSN[0],ResultSN[1],ResultSN[2],ResultSN[3],ResultSN[4],ResultSN[5],ResultSN[6] );

actually i don't know VC++, so i'm wondering how to have this code in C#...? pls help.

Actually, it's a part of a bigger function..
i only quote some parts which i don't know how to convert it into C#...

yes, you're great but how about these codes :

StrMsg.Format("Card Serial: %02x %02x %02x %02x %02x %02x %02x ",ResultSN[0],ResultSN[1],ResultSN[2],ResultSN[3],ResultSN[4],ResultSN[5],ResultSN[6] );

actually i don't know VC++, so i'm wondering how to have this code in C#...? pls help.

not quite sure on that last part, but i am guessing C# has a similar behavior for formatting strings.

If you're using Strings in C#, I think the Format method will be fine. quick example:

String s = String.Format("Card serial: {0} {1} {2} ...", ResultSN[0], ResultSN[1], ...);

I don't know that this will do the formatting as you want, but there might be ways to do that as well; I've hardly used C# before :P

This works for me:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Memcpy
{
  class Program
  {
    [DllImport("msvcr71.dll" , CharSet = CharSet.Auto)]
    public static unsafe extern void memcpy(int *dest , int *src , int count);


    unsafe static void Main(string[] args)
    {
      int[] ar1 = new int[] { 1, 2, 3, 4, 5, 6, 7 };
      int[] ar2 = new int[10];

      fixed (int* src = &ar1[1])
      fixed (int* dst = &ar2[2])
      {
        memcpy(dst, src, 6 * sizeof(int));
      }
      foreach (int b in ar2)
      {
        Console.WriteLine(b);
      }
    } 
  }
}

Namaste,

Johan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.