Jump to content

Help with small project


Doctor

Recommended Posts

Okay, let's say I have  a block of text like so;

 

 

00000000 00000000

00000000 00000000

00000000 00000000

00000000 00000000

00000000 00000000

00000000 00000000

 

 

I need to be able to add more/less digits to the text.

 

Like so;

 

0x00000000 0x00000000

0x00000000 0x00000000

0x00000000 0x00000000

0x00000000 0x00000000

0x00000000 0x00000000

 

etc

 

Also if it's greater than a certain number, to subtract. :3

 

How would I go about doing that?

 

 

Thank you,

 

Doctor

Link to comment
Share on other sites

Your explanation isn't very precise. How about some more information?

 

Alright, I need to add 0x, infront of the sets of zero's. I know it doesn't make much sense.

 

For example, when I export the text, it's raw form is

 

01234567 01234567

00000000 00000000

 

Random numbers, for the sake of explanation.

 

I need to be able to add 0x in front of each set.

 

Like;

 

0x01234567 0x01234567

0x00000000 0x00000000

 

I hope that's enough. :3

Link to comment
Share on other sites

This is a bit confusing without providing any code at all. If each of the random numbers is in an array you can loop through them and concatenate a 0x to them, however why they aren't generated with a 0x in the first place is what I'm wondering.

Link to comment
Share on other sites

This seems pretty simple looking at the code that you posted. You will simply need to do something like this:

 

$text = '000000 000000 000000 000000';
$new_text = '0x'.implode(' 0x',explode(' ',$text));
echo $new_text;

 

That will give you what you want. If this is coming from a file you will just have to read each of the lines and do this on it.

Link to comment
Share on other sites

<?php

$data=<<<EOF
00000000 00000000
00000000 00000000
00000000 00000000
00000000 00000000
12345678 1234567890
1234 12
EOF;

function numfmt($val)
{
     return "0x". substr(str_repeat('0',.$val[0],-;
}

echo preg_replace_callback('/\b[\da-fA-F]{1,8}\b/','numfmt',$data);

 

in the 4 test case.

1) was had 8 hex digits (max digit length) so 0x was prefixed

2) had 10 hex digits (exceeds digit length) so was skipped)

3 & 4) didnt have enough hex digits so both were padded with 0's

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.