hex interview questions
Top hex frequently asked interview questions
I'm working with some example java code for making md5 hashes. One part converts the results from bytes to a string of hex digits:
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0;i<messageDigest.length;i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}
However, it doesn't quite work since toHexString apparently drops off leading zeros. So, what's the simplest way to go from byte array to hex string that maintains the leading zeros?
Source: (StackOverflow)
Just to give an example I have this string: Hello world !!
and I want to print it using python as 48:65:6c:6c:6f:20:77:6f:72:6c:64:20:21:21
, hex()
works only for integers.
How it can be done?
Source: (StackOverflow)
I'm using Visual Studio 2008 and I have just noticed that the debugger is displaying integer values as Hex when I hover over variables and also in the immediate window. I guess I must have hit a shortcut key accidently or something.
Anyone had this before? How do I set it back to display in decimal?
Source: (StackOverflow)
I want to convert a hex string to a 32 bit signed integer in C++.
So, for example, I have the hex string "fffefffe". The binary representation of this is 11111111111111101111111111111110. The signed integer representation of this is: -65538.
How do I do this conversion in C++? This also needs to work for non-negative numbers. For example, the hex string "0000000A", which is 00000000000000000000000000001010 in binary, and 10 in decimal.
Source: (StackOverflow)
Well, I just need to convert RGB values to HEX and the other way around.
I've tried this, but it didn't work.
function RGB2HTML(red, green, blue)
{
var decColor = red + 256 * green + 65536 * blue;
return decColor.toString(16);
}
Example :
#0080C0 to (0,128,192)
Source: (StackOverflow)
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 a built in Ruby function.
Source: (StackOverflow)
I am trying to understand how colors work in Android. I have this color set as the background of my LinearLayout
and I get a background gray with some transparency.
<gradient android:startColor="#b4555555" android:endColor="#b4555555"
android:angle="270.0" />
If I remove the last two characters (55) I get a solid color, losing the transparency. I was trying to find a page where I can see some explanation about this but couldn't find it.
This may be a beginner's question, but I am intrigued by this right now.
Source: (StackOverflow)
How can I convert the following?
2934 (integer) to B76 (hex)
Let me explain what I am trying to do. I have User IDs in my database that are stored as integers. Rather than having users reference their IDs I want to let them use the hex value. The main reason is because it's shorter.
So not only do I need to go from integer to hex but I also need to go from hex to integer.
Is there an easy way to do this in C#?
Source: (StackOverflow)
I need a good HEX editor for Linux, and by good I mean:
- Fast
- Search/replace features
- Can display data not only in hex, but also binary, octal, etc.
- Can work with huge (> 1 gb) files without becoming slow and unresponsive (this requirement is important)
- Optionally, has some compare/diff features
What can you suggest?
Source: (StackOverflow)
I need a utility to diff two binary files. The files are large (6-50 GB).
Note: It needs to be specifically pointed out here: most diff programs work by mapping the file into their virtual address space. On 32-bit Windows, this limits the sizes of files that can be compared to under 1 GB each. (1.5 GB if Windows is run with the /3GB switch, and the program has advertised that it is 3 GB aware; /LARGEADDRESSAWARE).
If a program insists on the technique of mapping the file entirely into its address space, then it must be recompiled as a 64-bit application, which has an address space of 8 TB (which meets my requirements)
Beyond Compare is my favorite diff tool, and I own it, but it cannot handle binary files over what can fit in the process's address space.
HexDiff 3.0 seemed interesting, except the trial version doesn't do diff's. *rolls eyes*
the tool should be free, since I'm not paying money to figure out that it doesn't work.
the tool should be a Windows application.
the tool should not be console based (that is, a Windows application)
the tool should be graphical (that is, a Windows application)
Source: (StackOverflow)
Here is a function I was working on to programmatically lighten or darken a hex color by a specific amount. Just pass in a string like "3F6D2A" for the color (col) and an base10 integer (amt) for the amount to lighten or darken. To darken, pass in a negative number (i.e. -20).
The reason for me to do this was because of all the solutions I found, thus far, they seemed to over-complicate the issue. And I had a feeling it could be done with just a couple lines of code. Please let me know if you find any problems, or have any adjustments to make that would speed it up.
function LightenDarkenColor(col,amt) {
col = parseInt(col,16);
return (((col & 0x0000FF) + amt) | ((((col>> 8) & 0x00FF) + amt) << 8) | (((col >> 16) + amt) << 16)).toString(16);
}
For Development use here is an easier to read version:
function LightenDarkenColor(col,amt) {
var num = parseInt(col,16);
var r = (num >> 16) + amt;
var b = ((num >> 8) & 0x00FF) + amt;
var g = (num & 0x0000FF) + amt;
var newColor = g | (b << 8) | (r << 16);
return newColor.toString(16);
}
And finally a version to handle colors that may (or may not) have the "#" in the beginning. Plus adjusting for improper color values:
function LightenDarkenColor(col,amt) {
var usePound = false;
if ( col[0] == "#" ) {
col = col.slice(1);
usePound = true;
}
var num = parseInt(col,16);
var r = (num >> 16) + amt;
if ( r > 255 ) r = 255;
else if (r < 0) r = 0;
var b = ((num >> 8) & 0x00FF) + amt;
if ( b > 255 ) b = 255;
else if (b < 0) b = 0;
var g = (num & 0x0000FF) + amt;
if ( g > 255 ) g = 255;
else if ( g < 0 ) g = 0;
return (usePound?"#":"") + (g | (b << 8) | (r << 16)).toString(16);
}
Ok, so now its not just a couple of lines, but it seems far simpler and if your not using the "#" and dont need to check for colors out of range, it is only a couple of lines.
If not using the "#", you can just add it in code like:
var myColor = "3F6D2A";
myColor = LightenDarkenColor(myColor,10);
thePlaceTheColorIsUsed = ("#" + myColor);
I guess my main question is, am I correct here? Does this not encompass some (normal) situations? Is there a faster way? Thats still KISS?
Pimp Trizkit
Source: (StackOverflow)
I am looking for a way to convert a long string (from a dump), that represents hex values into a byte array.
I couldn't have phrased it better than the person that posted the same question here:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21062554.html
But to keep it original, I'll phrase it my own way: suppose I have a string "00A0BF" that I would like interpreted as the byte[] {0x00,0xA0,0xBf} what should I do?
I am a Java novice and ended up using BigInteger and watching out for leading hex zeros. But I think it is ugly and I am sure I am missing something simple...
Source: (StackOverflow)
This question already has an answer here:
How do I convert a byte[]
to a string
? Every time I attempt it, I get
System.Byte[]
instead of the value.
Also, how do I get the value in Hex instead of a decimal?
Source: (StackOverflow)
I'm working on implementing a wigdget transparency option for my app widget although I'm having some trouble getting the hex color values right. Being completely new to hex color transparency I searched around a bit although I couldn't find a specific answer to my question.
I want to set transparency by hex color so let's say my hex color id "#33b5e5" and I want it to be 50% transparent. Then I'll use "#8033b5e5" because 80 is 50%.
I found a useful chart here: http://www.dtp-aus.com/hexadeci.htm . With this data I managed to come up with this:
0% = #00
10% = #16
20% = #32
30% = #48
40% = #64
50% = #80
60% = #96
70% = #112
80% = #128
90% = #144
Now the issues start appearing when I get higher than 100 in hex. Hex color codes can only be 8 symbols long right? For example #11233b5e5 (80%) crashes.
What can I do to enable me to use the higher numbers aswell?
Source: (StackOverflow)