使用base64_decode

时间:2015-05-28 02:22:05

标签: php base64 simplexml

我使用base64_decode将十六进制值解码为其原始内容,并将其逗号处的字符串转换为数组。 print数组显示成功,但是当我尝试使用switch()的值时,它不会工作。我尝试使用intval()将数字字符串更改为整数并返回0.我认为它与解码有关但我不知所措。我也知道我在十六进制值的开头有一些不会解码的字符

在我的代码中,$ value是我的十六进制字符串来自解析一个SimpleXMLElement()我有一个十六进制有效负载数组,我正在使用foreach循环这一瞥到一个循环:

//the hex value im decoding comes from parsing my SimpleXMLElement
//I generate $value = gAExLDUyLjMxOTEsLTExMy45ODc=
//decode it and it gives me a strange character a question mark in a diamond
//diamond thing should decode to "[128][3]" but that doesn't come across.
$result = base64_decode(str_replace(" ","+",$value));

//I trim off the strange character
$result = substr($result,1);

//and get the expected string
//1,52.3191,-113.9870
//so I explode it into an array
$param = explode(',', $result);

// can verify the array with print_r -- all good                        
//but this type cast fails so does intval()
$type = (int)$param[0];

//this is all going on inside a foreach loop
//so the decoded value is different everytime and
//i have different operations to perfrom based
//on the value of $type variable using the switch
//cant get the switch to accept the type variable

1 个答案:

答案 0 :(得分:1)

解码base64字符串gAExLDUyLjMxOTEsLTExMy45ODc=会给我这个字符序列:

0#    80 hex = 256 dec = 200 oct = character 256 (outside ASCII range)
1#    01 hex = 001 dec = 001 oct = ASCII SOH (start of heading)
2#    31 hex = 049 dec = 061 oct = 1
3#    2C hex = 044 dec = 054 oct = ,
4#    35 hex = 053 dec = 065 oct = 5
5#    32 hex = 050 dec = 062 oct = 2
6#    2E hex = 046 dec = 056 oct = .
7#    33 hex = 051 dec = 063 oct = 3
8#    31 hex = 049 dec = 061 oct = 1
9#    39 hex = 057 dec = 071 oct = 9
10#    31 hex = 049 dec = 061 oct = 1
11#    2C hex = 044 dec = 054 oct = ,
12#    2D hex = 045 dec = 055 oct = -
13#    31 hex = 049 dec = 061 oct = 1
14#    31 hex = 049 dec = 061 oct = 1
15#    33 hex = 051 dec = 063 oct = 3
16#    2E hex = 046 dec = 056 oct = .
17#    39 hex = 057 dec = 071 oct = 9
18#    38 hex = 056 dec = 070 oct = 8
19#    37 hex = 055 dec = 067 oct = 7

请注意,在1之前的开头没有一个而是两个字节。您正在剥离其中一个,但是当您执行int强制转换时另一个仍然存在,因此PHP认为该字符串不是以数字开头并给你0

如果您确定不想要这些字符,则可以在拆分之前丢弃除数字,逗号和点以外的所有内容:

$result = preg_replace('/[^0-9,.]/', '', $result);