---
title: "How to convert a String to hex and calculate binary result in python"  
description: "How to convert a String to hex and calculate binary result in python"  
author: "Pravesh Singh"  
published: 2015-01-27  
updated: 2015-01-27  
canonical: https://www.mindstick.com/forum/12905/how-to-convert-a-string-to-hex-and-calculate-binary-result-in-python  
category: "asp.net"  
tags: ["c#", "python-3.4"]  
reading_time: 2 minutes  

---

# How to convert a String to hex and calculate binary result in python

I got stuck with my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii), tried anything on this side and many other things [Google](https://www.mindstick.com/articles/43833/google-lighthouse-and-how-is-it-changing-the-way-we-development-and-design-websites) showed.

**To the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic):**

I try to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) some code-snips from C# to phyton, but on this special point i got stuck.

```
public static long decode(string data, int size, int offset = 0){    long value = 0;     for (int i = 0; i < size; ++i) {        value <<= 6;        value |= (long)data[offset + i] - 0x30;    }     return value;}
```

The [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) [Data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) could be something like 1Dh. Based on this I convert each char to the hex-equivalent: 0x31, 0x44, 0x68 and subtract 0x30; so I get 0x1, 0x14, 0x38; In the next step I have to convert to the [binary](https://www.mindstick.com/forum/34709/please-write-a-program-for-decimal-to-binary-conversion-in-c-sharp) equivalent 000001, 010100, 111000 and merge this to 000001010100111000. From this I want to get the [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) meaning, in this case 5432.

Is there a possibility to do this in a [smart](https://www.mindstick.com/blog/11895/7-smart-ways-to-have-successful-romantic-getaway-for-married-couples) and easy way in [python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by Royce Roy

It's actually pretty easy, and the translation is pretty straight forward. You can continue to use your bit shifting. The only change is the syntax of for-loop and using ord() to get the integer value from a character.

```
def decode(data, size, offset=0):    value = 0     for ch in data[offset:size]:        value <<=6        value |= ord(ch)
- 0x30     return value
```

Running this in the interpreter, I get 5432:

>>> decode("1Dh", 3)

5432


---

Original Source: https://www.mindstick.com/forum/12905/how-to-convert-a-string-to-hex-and-calculate-binary-result-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
