Jump to content

String to Int Problems


Go to solution Solved by pally99,

Recommended Posts

Hi I appreciate your help, the code is kinda simple actually but I'm having trouble


 


The following code echo's the WRONG result


 


a5611bcd::0::0::abccef01


 


I need this following result


 


a5611bcd::fffafaff::fefffeef::abccef01


 


 


I suspect the issue is that those two middle values are too large for integer, I know about bcmath, but whats the best way to get correct results into INTEGER form.. must be integer form, basically I


 


 


 


my variables below are closely named sorry about this...



<?php

$KEY = array(0xA5611BCD,0xFFFAFAFF,0xFEFFFEEF,0xABCCEF01);

/*this is the model of how everything below should end up in $SKEY


$KEY = array(0xA5611BCD,0xFFFAFAFF,0xFEFFFEEF,0xABCCEF01);
*/

//so I begin with this string $getSKEY and try to make it into the above model
$getSKEY = "A5611BCDFFFAFAFFFEFFFEEFABCCEF01";

$SKEY =
array(
hexdec(substr($getSKEY,0,),
hexdec(substr($getSKEY,8,16)),
hexdec(substr($getSKEY,16,24)),
hexdec(substr($getSKEY,24,32))
);

echo dechex($SKEY[0])."::".
dechex($SKEY[1])."::".
dechex($SKEY[2])."::".
dechex($SKEY[3])
;
?>
Link to comment
https://forums.phpfreaks.com/topic/277153-string-to-int-problems/
Share on other sites

Why are you converting from the string into numbers and back into a string?

echo implode("::", str_split($getSKEY, ); // possibly with strtolower()

Hi thanks for your help but I'm not sure this answers my question correctly, the echo is really more of a test I suppose

 

I was just trying to make sure that my values were being stored inside the $SKEY array as array(0xA5611BCD,0xFFFAFAFF,0xFEFFFEEF,0xABCCEF01);

 

this is why I was converting from a string back to numbers, I was trying to take the first 8 hexString characters and make them into a decimal for the first integer array etc

 

 

I did not think they were being stored correctly and I still don't think they are?

 

ok so to be much clear just take string as input: "A5611BCDFFFAFAFFFEFFFEEFABCCEF01"

 

output: array containing four integers each with the following hex value array(0xA5611BCD,0xFFFAFAFF,0xFEFFFEEF,0xABCCEF01);

Edited by pally99

If you try to output hex values, they will show up as decimals in your browser.

echo 0xA5611BCD;

If you look at the documentation for dechex(), it actually returns a string, not a number.

 

A number is the same number no matter what base you convert it to. If you actually want the hex string, requinix's solution gives you the exact output you asked for in your first post. If you want the integer values of each hex string, just run hexdec() one time.

$strings = str_split($getSKEY, ;
foreach ($strings as $string)
	echo hexdec($string).'<br/>';

I think the answer to your question relies wholly on what you are trying to accomplish.

Okay.

 

Without actually checking it I think the problem is your use of substr().

hexdec(substr($getSKEY,0,8)),
hexdec(substr($getSKEY,8,16)),
hexdec(substr($getSKEY,16,24)),
hexdec(substr($getSKEY,24,32))
The third argument is a length, not an offset. Edited by requinix
  • Solution

If you try to output hex values, they will show up as decimals in your browser.

echo 0xA5611BCD;

If you look at the documentation for dechex(), it actually returns a string, not a number.

 

A number is the same number no matter what base you convert it to. If you actually want the hex string, requinix's solution gives you the exact output you asked for in your first post. If you want the integer values of each hex string, just run hexdec() one time.

$strings = str_split($getSKEY, ;
foreach ($strings as $string)
	echo hexdec($string).'<br/>';

I think the answer to your question relies wholly on what you are trying to accomplish.

 

lemmin

Thank you for your help this was not correct

 

requinix

 

Thank you for your help but also not correct

 

 

CORRECT ANSWER: I'm an idiot and used substr wrong! AHHHHHHHH... but thank you guys so much for being patient and offering very good suggestions I really appreciate it!

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.