2012-01-06 2 views
0

직렬 포트에서 문자열을 읽은 다음 몇 가지 값 (이 경우 높은 바이트와 낮은 바이트의 표현)을 구문 분석하는 코드가 있는데, 그 다음에 이들을 바꿔서 결합하여 올바른 순서로 배열합니다. 결합 된 값을 십진수 값으로 변환하고 싶습니다.문자열의 16 진수 값을 숫자로 변환하는 방법은 무엇입니까?

16 진수 표현을 문자열로 16 진수로 변환하려고 시도하는 데 어려움을 겪고 있으며 그 결과를 10 진수로 변환합니다.

코드는 여기 :

private void OutputUpdateCallbackclusterTxtBox(string data) 
{ 
string cluster; 
string attribute; 
string tempvalueHighByte; 
string tempvalueLowByte; 
string tempvalueHighLowByte; //switched around 
int temporarytemp; 



if (data.Contains("T00000000:RX len 9, ep 01, clus 0x0201") == true)//find our marker in thestring 
{ 
    if (data.Contains("clus") == true) 
    { 
     int index = data.IndexOf("clus"); //if we find it find the index in the string it occurs 
     cluster = data.Substring(index + 5, 6); //use this index add 5 and read in 6 characters from the number to get our value 
     attribute = data.Substring(index + 5, 1); 
     cluster_TXT.Text = cluster; // post the value in the test box 
    } 
    if (data.Contains("payload") == true) 
    { 
     int index = data.IndexOf("payload"); //if we find it find the index in the string it occurs 
     tempvalueHighByte = data.Substring(index + 20, 2); //use this index add 20 and read in 2 characters from the number to get our value 
     tempvalueLowByte = data.Substring(index + 23, 2); //use this index add 23 and read in 2 characters from the number to get our value 
     tempvalueHighLowByte = tempvalueLowByte + tempvalueHighByte; 


     ConvertToHex(tempvalueHighLowByte); 

     temporarytemp= int.Parse(tempvalueHighLowByte); 
     temperatureTxt.Text = ((char)temporarytemp).ToString(); // post the value in the text box 
    } 
+0

가 왜 진수로 바로 변환 할 수 있습니까? 왜 "문자열로"중간 단계? – Adrian

+0

그것의 나의 결함있는 논리, 한 번에 한 걸음 씩 작업, 확실하게 16 진수로 변환 할 필요가 없습니다. –

답변

5

핵사 다시 C에서 #는, 예를 간단로 변환

string hex = "FFFFFFFF"; 

// From hex... 
long l = Convert.ToInt64(hex, 16); 

// And back to hex... 
string hex2 = l.ToString("X"); 
+0

안녕하세요, 저는 해결책을 찾았습니다. 텍스트로 표시 될 수 있도록 lont를 문자열로 변환해야합니다. 상자, 고마워 Nick –

3

이 시도 :

int value = Convert.ToInt32("0x0201", 16); 
+0

스티브, 위의 시도했지만 컴파일러가 "암시 적으로 형식 'int' '문자열' –

+0

@ user1134631 변환 할 수 없습니다. 불편을 끼쳐 드려 죄송합니다. 코드를 업데이트했습니다. –

관련 문제