Questions tagged [base-conversion]

Base conversion is the process of changing the base of the textual representation of a number to another base. For example, "1010" in binary to "10" in decimal is a base conversion.

Base conversion is the process of changing the base of the textual representation of a number to another base. For example, 1010 in binary to 10 in decimal is a base conversion.

198 questions
217
votes
5 answers

Converting an integer to a hexadecimal string in Ruby

Is there a built in way to convert an integer in Ruby into its hexadecimal equivalent? Something like the opposite of String#to_i: "0A".to_i(16) #=>10 Like perhaps: "0A".hex #=>10 I know how to roll my own, but it's probably more efficient to use…
Matt Haughton
  • 2,919
  • 3
  • 24
  • 30
39
votes
8 answers

Converting binary to decimal integer output

I need to convert a binary input into a decimal integer. I know how to go from a decimal to a binary: n = int(raw_input('enter a number: ')) print '{0:b}'.format(n) I need to go in the reverse direction. My professor said that when he checks our…
purlinka
  • 399
  • 1
  • 3
  • 4
16
votes
7 answers

Converting binary string to a hexadecimal string JAVA

I want to convert my binary(which is in string) to hexadecimal string also, this is just a program fragment since this program is just a part of another bigger program: //the variable name of the binary string is: "binary" int digitNumber = 1; …
dumas
  • 199
  • 1
  • 2
  • 9
16
votes
2 answers

Base 10 to base n conversions

I'm trying to write a C++ program that does base-conversions. I want to convert a decimal number to all the other integer bases from 2 to 20. Is there an efficient and easy-to-implement algorithm for base conversions?
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
14
votes
1 answer

How to create a 32-bit integer from eight (8) 4-bit integers?

Let's say I have a max 32-bit integer - const a = ((2 ** 32) - 1) const b = parseInt("11111111111111111111111111111111", 2) // 32 bits, each is a one! console.log(a === b) // true console.log(a.toString(2)) //…
Mulan
  • 129,518
  • 31
  • 228
  • 259
13
votes
6 answers

Getting a hexadecimal number into a program via the command line

I can do this: int main(int argc, char** argv) { unsigned char cTest = 0xff; return 0; } But what's the right way to get a hexadecimal number into the program via the command line? unsigned char cTest = argv[1]; doesn't do the trick. That…
Pieter
  • 31,619
  • 76
  • 167
  • 242
12
votes
5 answers

How to convert a decimal base (10) to a negabinary base (-2)?

I want to write a program to convert from decimal to negabinary. I cannot figure out how to convert from decimal to negabinary. I have no idea about how to find the rule and how it works. Example: 7(base10)-->11011(base-2) I just know it is 7 =…
Jason
  • 1,573
  • 3
  • 18
  • 46
12
votes
4 answers

Bash decimal to base 62 conversion

I would like to reverse the operation performed by the following bash command: $ echo $((62#a39qrT)) 9207903953 i.e. convert decimal 9207903953 to base 62, keeping bash standard of {0..9},{a..z},{A..Z}. I know I can do this by using bc, but I will…
Ram
  • 1,161
  • 1
  • 11
  • 34
12
votes
3 answers

Converting number base

Is there a platform function that will do the following? convertBase :: (Num a, Num b) => Int -> Int -> [a] -> [b] Convert a number from base 'a' to base 'b' where each list item is a digit in the number. for example: convertBase 2 10 [1,1,0,1] =…
user181351
11
votes
3 answers

Convert large hex string to decimal string

I need to convert a large (too large for the built-in data types) hex string to a string with it's decimal representation. For example: std::string sHex = "07AA17C660F3DD1D2A1B48F1B746C148"; std::string sDec; // should end up with:…
Flavio
  • 458
  • 1
  • 3
  • 10
11
votes
5 answers

How to Print Hexadecimal Numbers in PHP or Java

I need to print some data (a little bit strange formatted). I was writing it in PHP with if ($num%10==9) but it was impossible for me to get correct output. So take a look at this for example. We have x of files in folder. For this example x=36. X…
Splendid
  • 1,317
  • 4
  • 20
  • 41
10
votes
4 answers

Base 26 as alphabetic using Java's Integer.toString()

So I just learned Integer.toString(int x, int radix); and thought it was pretty awesome since it makes base conversions super easy. However, I'm trying to write with Base-26 phonetically (a - z) and noticed that Integer.toString() follows…
forgetfulbur
  • 103
  • 1
  • 7
10
votes
6 answers

PHP - How to base_convert() up to base 62

I need a base_convert() function that works from base 2 up to base 62 but I'm missing the math I need to use, I know that due to the limitations of PHP I need to make use of bcmath, which is fine. Functions like these convert a number to and from…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
10
votes
1 answer

Convert a very large number from decimal string to binary representation?

I have a very big number, on the order of a thousand decimal digits, and I have to convert this to its binary representation. The numbers are stored as strings. Since few languages have a basic data type to handle numbers this big, I see no easy way…
frodo
  • 1,561
  • 3
  • 21
  • 36
8
votes
6 answers

Print large base 256 array in base 10 in c

I have an array of unsigned chars in c I am trying to print in base 10, and I am stuck. I think this will be better explained in code, so, given: unsigned char n[3]; char[0] = 1; char[1] = 2; char[2] = 3; I would like to print 197121. This is…
endeavormac
  • 659
  • 2
  • 8
  • 18
1
2 3
13 14