# Thread: Wolfram Mathematica truncates initial 0:s

1. ## Wolfram Mathematica truncates initial 0:s

I'm trying to convert a list with numbers into a string with binary values.

In[168]:= x={10, 10};
IntegerString[x,2]
z=StringJoin[IntegerString[x,2]]
Out[169]= {1010,1010}
Out[170]= 10101010

The desired result would be:
Out[169]= {0001010, 0001010}
Out[170]= 00010100001010

2. ## Re: Wolfram Mathematica truncates initial 0:s

Huh? 10 = 1010; WHY d'ya want 0001010 ?

3. ## Re: Wolfram Mathematica truncates initial 0:s

Ignore Block[], I just use that to keep variables local to notebooks.

4. ## Re: Wolfram Mathematica truncates initial 0:s

To describe the whole problem I'm trying out RSA encryption and want to convert a text string to a number.
So i convert each letter into a ascii number and then convert each number into binary form.

Originally Posted by DenisB
Huh? 10 = 1010; WHY d'ya want 0001010 ?
It's needed to keep the binary intact, the binary codes can be seen here:

https://en.wikipedia.org/wiki/ASCII

Originally Posted by romsek
Ignore Block[], I just use that to keep variables local to notebooks.
Thanks for the suggestion, although it works for this specific example it can't be applied generally.
Basically we have a 7 bit binary space for each letter, ranging from 0000001 to 1111111.
Problem is all initial zeroes get truncated in conversion.

5. ## Re: Wolfram Mathematica truncates initial 0:s

so a routine to take a list of 2 non-negative integers, convert them to 7 bit binary, padding to 7 bits as necessary, and then concatenating them?

6. ## Re: Wolfram Mathematica truncates initial 0:s

Originally Posted by romsek
so a routine to take a list of 2 non-negative integers, convert them to 7 bit binary, padding to 7 bits as necessary, and then concatenating them?
The list will contain many non-negative integers, other than that you're spot on.

7. ## Re: Wolfram Mathematica truncates initial 0:s

Originally Posted by TriForce
The list will contain many non-negative integers, other than that you're spot on.
This should work with an arbitrary list of char codes.

8. ## Re: Wolfram Mathematica truncates initial 0:s

Oh man you're good at this. Thank you!

9. ## Re: Wolfram Mathematica truncates initial 0:s

do you understand how it works?

10. ## Re: Wolfram Mathematica truncates initial 0:s

Sort of, I'm very new to mathematica and not sure about some of it's syntax and operators.

But overall yes, you create a function that checks the input length of each element in the list and adds up to 7 zeroes. Then concatenates it to a larger string.

Thanks!