2015-05-28 14 views
0

我使用base64_decode將十六進制值解碼爲其原始內容並將其逗號中的字符串用作數組。打印數組顯示成功,但是當我嘗試使用switch()的值並且它不工作時。我嘗試使用intval()將數字字符串更改爲整數,並返回0.我認爲這與解碼有關,但我不知所措。我也知道,我有一些字符在十六進制值的開頭不會解碼PHP intval或int類型轉換在使用base64_decode解碼的十六進制字符串時失敗

在我的代碼$ 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

解碼的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); 
+0

我想在開始的兩個字節,但是我我無法將它們解碼爲可讀的數據。 (int)typecast和intval()失敗的任何想法? – Laurence

+0

@Laurence您要轉換爲int的字符串包括字符串開頭的兩個字節(之一)。因此,它看起來不像一個數字,所以PHP給你0. – IMSoP

+0

謝謝!這就是爲什麼我的intval()失敗。我希望我知道如何解碼這些第一個字節,但這仍然會讓球繼續滾動。 – Laurence

相關問題