Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How to convert hex value stored in string to binary in string?


  • Please log in to reply
6 replies to this topic

#1 lifanek

lifanek

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationKatowice, Poland

Posted 01 March 2014 - 03:49 PM

Hi all,

I'm trying to accomplish something like this:

http://www.serasidis...scontroller.htm

and now I'm writing some code to decode PDU format to text.

The steps are:

- Some values represented by hex numbers stored in string, i.e. "31584C1E8BC160"

- Reversing it like this ===>  "60C18B1E4C5831"

- Here comes the hardest part - every 7 bits I need to add a 0 bit, because PDU uses 7-bit GSM ASCII.

I can convert it right away beacuse it is stored in hex and everything is shifted by 1 bit every 7 bits.

(see this http://www.serasidis..._conversion.gif )

 

I wanted to do it like this:

 

string hex = "60" (first two chars from the main string)

int x = Convert.ToInt32(hex, 16)

BinaryString = Convert.ToString(x, 2)

 

but it seems that there is no Convert in .NET MF :(

 

Is there any workaround possible for this?

Thanks,

Lifanek

 



#2 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 01 March 2014 - 09:11 PM

The latter direction, Number.ToString("x2") should work.

Might be possible to use something like int.Parse(...) in the other direction?

#3 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 01 March 2014 - 10:18 PM

Don't you need .NETMF 4.3 to get the ToString modifiers?



#4 jrlyman3

jrlyman3

    Advanced Member

  • Members
  • PipPipPip
  • 67 posts
  • LocationNorth St Paul. MN

Posted 04 March 2014 - 11:13 PM

There are a number of useful string functions that are missing ... not to mention the StringBuilder class ... I would use some code like:

        String smsData = "31584C1E8bC160";
        String data = "";
        int bytePos;
        int charPos;
        int nibble0, nibble1, byteVal;
        int carryBits = 0;

        smsData = smsData.ToUpper();   // Not needed if digits will always be uppercase.

        for (bytePos = 0; bytePos < 7; bytePos++) {
            charPos = bytePos * 2;
            nibble0 = smsData[charPos] - '0' - ((smsData[charPos] >> 6) * 7);
            nibble1 = smsData[charPos + 1] - '0' - ((smsData[charPos + 1] >> 6) * 7);
            byteVal = (((nibble0 << 4) + nibble1) << bytePos) | carryBits;
            carryBits = (byteVal & 0x7F80) >> 7;
            data += ((char)(byteVal & 0x7F));
        }
        data += ((char)carryBits);
        Debug.Print("SMS Data = " + data + "\n");

--John



#5 lifanek

lifanek

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationKatowice, Poland

Posted 05 March 2014 - 12:18 AM

Hi,
Thanks all for reply - I made string array with binary equivalents in it ("0000", "0001" etc..) and I convert hex to string from this array :)

#6 jrlyman3

jrlyman3

    Advanced Member

  • Members
  • PipPipPip
  • 67 posts
  • LocationNorth St Paul. MN

Posted 05 March 2014 - 03:27 AM

I'm curious to see how you did that ... I'm always looking for a better way to do things ... could you post the code?



#7 lifanek

lifanek

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationKatowice, Poland

Posted 05 March 2014 - 02:52 PM

No problem - I want to make some ATcommands/PDU library to make it simple in use. My code is simple because I'm a beginner in C# and I'm working on my first bigger project on Netduino and for sure there are a lot of things that could be wrote more efficently.

 

Attached Files

  • Attached File  uGSM.zip   53.24KB   17 downloads





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.