Hello - I'm wanting to convert some old C code to php ... one thing I'm stuck on is the equivalent php functions for the C code functions memset and memcpy

Here's the definiton (in C) for memset

void memset ( void buffer, int c, size_t num );
Fill buffer with specified character.
Sets the first num bytes pointed by buffer to the value specified by c parameter.

Parameters.
buffer
Pointer to block of data to be filled with c.

c
character value to set.

num
Number of bytes to copy.

Here's the definition for memcpy void memcpy ( void dest, const void src, size_t num );

Links for examples and outputs:
http://www.cplusplus.com/ref/cstring/memset.html

http://www.cplusplus.com/ref/cstring/memcpy.html

Thanks - Mark

    Depends on how you're implementing arrays of bytes - as strings or arrays of numbers? As strings they'd probably be something to do with [man]str_repeat[/man] and [man]substr[/man] respectively, as arrays, then [man]array_fill[/man], [man]array_splice[/man], and [man]array_slice[/man] will do the job.

      It would be a string. What I'm trying to do is position certain strings of chars along a single line of text in a text file. The steps:

      1.) initialize the entire line (record) to spaces
      2.) insert characters 'foo' starting at position 4
      3.) insert characters 'foofoo' starting at position 21
      4.) insert characters '360' starting at position 29
      5.) insert characters '\r\n' starting at position 200
      6.) write the line to a file

      repeat ..... until my entie file has been written, thus I'd have several snippets of code looking like the above.

      Each line would have its data at different positions, thus the next line might have "foob" at position 11

        You should take a step back when converting a language such as C which supports pointers and is constructed completely different, to a much simpler language like PHP.

        For example. If we want to as "Hello" to the front of an existing variable $var = World we could do:

        <code>
        $var2 = "Hello " . $var.
        </code>

        $var2 now conatins: "Hello World"

        Now your second problem where youa re trying to copy a string onto another in php we just do $something = $something_else.

          I think I did a poor job of explaining what I want to do ... let me take another crack at it.

          I want to intialize a (300) character line to all spaces. But for this explanation, and for simplification, let's say I want to initialize a 20 character line, and let's use a * instead of a blank space (so that it's visible).

          Thus I want to start with:

          ******************** (20 characters exactly)

          I need the character string to always have exactly 20 characters, and I want to insert data at specific offsets, such as (example only) offset 4 and offset 10. Whatever I insert, and however long it is (within reason) I want written on top of what's already there - as opposed to making the string longer than 20 characters.

          Thus if I need to insert (and write over) the strings "foo" and "hello", it would show up in my file write like this:

          *foohello***** (20 characters exactly)

          In the above, I inserted the strings "foo" and "hello" on top of what used to be there (asterisks).

          I won't always know the length of the new strings, but it's critical that whatever strings get inserted start at the correct offset, so that the applicaton reading the text file knows exactly where to look.

            Strings in PHP dont have a limit in length. Unlike C++ memory space does not need to be reserved. For example, you may start of with 3 characters and you could easily add to 5 without having to create another variable and copy (as in native C). That means that a variable $var could have any size string or numbers in it.

            For your example lets assumer $var = ********************, and $foo = foo and you want to insert foo at offset 4. In this case we could do something like this:

            $offset = 4;
            $i = 0;
            while($foo[$i]){
            $var[$offset + $i] = $foo[$i]
            $i++;
            }

            As you can see we can use $var[5] of the string, to get the 5th character in that string. You should view this similar to the C equivalent of storing chars in arrays. e.g. char string[] = "sample", in this case string[0] will containt s, string[1] wil contain a.

            Unfortunately we can not copy the string dirrectly into it as you can in C while using char as a pointer (char*) where you can just copy into that memory location up to the point of the reserved memmory (thereafter getting a error that you have written beyond the program heap).

            Hope this answers your question.

              Thanks :-) I'm going to give this a try and see if I can get it to fit into my framework.

                String-based...okay.... let's see... This is probably faster than copying stuff across one character at a time. (Incidentally, onno182's loop will fail on your example 4; it will also throw an Uninitialised String Offset notice every time you use it).

                The biggest thing to keep an eye on is that in C arrays (and strings) are identified by pointers into memory, while in PHP they're first-class citizens. So mapping pointer arithmetic can be tricky. In particular, pointers into pre-existing strings are offset relative to the start of the string, not to the start of memory (so the first character of a string is always at position 0). So it seems an extra parameter to memcpy are needed to fully specify the destination (one parameter to identify the string, one to identify its offset).

                function memset(&$buffer, $c, $num)
                {
                	$buffer = str_repeat($c, $num);
                }
                
                
                function memcpy(&$dest, $offset, $src, $num=-1)
                {
                	// Often, we want to copy the entire $src. This saves us having to call
                	// strlen($src) before calling memcpy() in that situation
                	// memcpy($foo, 0, $bar) will copy ALL of $bar into the initial
                	// part of $foo.
                	if($num==-1) $num=strlen($src);
                	$dest = substr($dest, 0, $offset).substr($src, 0, $num).substr($dest, $offset+$num);
                }
                

                The nice thing about this memcpy is that there is no danger of running off the end of the $dest buffer - the string just gets longer.

                1.) initialize the entire line (record) to spaces
                2.) insert characters 'foo' starting at position 4
                3.) insert characters 'foofoo' starting at position 21
                4.) insert characters '360' starting at position 29
                5.) insert characters '\r\n' starting at position 200
                6.) write the line to a file

                $foos = 'foofoo';
                memset($record, ' ', 200);
                memcpy($record, 4, $foos, 3);
                memcpy($record, 14, $foo); // Doubles as an illustration of the default argument.
                memcpy($record, 29, '360');
                memcpy($record, 200, "\r\n"); // Look! No buffer overrun!
                

                  Thanks :-) I'll give this a shot ... and yes, I was getting those offset string notices.

                    Should the last line

                    memcpy($record, "\r\n", 200); // Look! No buffer overrun!

                    be this instead ?

                    memcpy($record, 200, "\r\n"; // Look! No buffer overrun!

                      Sorry for the thick skull, but is the 4th argument critical ? I'm being selfish here, but as fas as coding a lot of stuff, it's easier for me type in 3 args.

                      memcpy($record, 4, $foos, 3); <------------------
                      memcpy($record, 14, $foo);

                        No; that's why I made it optional. Leave it out and the entire source source string is copied.

                          Write a Reply...