Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi if I got an array of 2 number that represent a hex number
index2 = 0x10
index1 = 0xD

can someone tell me how to combine these two numbers so i can get the hex value 100D

i try the below code



VB
Dim index1, index2, result As Short

    index2 = &H10
    index1 = &HD

 

    result = index1 + (index2 < 8)
Posted
Comments
Sergey Alexandrovich Kryukov 12-Nov-13 16:21pm    
There is no such thing as "hex number". Only a string representing number can be "hex" or "decimal".
—SA

VB
Dim index1, index2, result As UShort
 
index2 = &H10
index1 = &HD
 
result = index1 + (index2 << 8)
 
Share this answer
 
You cannot possibly have an array of numbers representing HEX numbers. You can either have array of string each representing a number, but this is not an array of numbers. Of you can have an array of numbers, but a number cannot be "hex" or "decimal". A number is a number, always "binary". So, the question simply makes no sense.

If you have strings, you need to parse each as "hex" into a number and use numeric arithmetic. If you have array of numbers, do numeric arithmetic on these numbers. You did not even explain what language and platform you are using (I feel that some people think that there is only one platform and one language in the world, aren't you one of them? :-)), so not code for you. You really need to learn some basics of programming, at least to get the skill of asking more correct questions.

Also, if you really use the array of strings, it would reflect the great fallacy of many beginners these days: a bad trend to work with strings representing data instead of data itself. You should try to work with data, not those strings.

—SA
 
Share this answer
 
Comments
Member 8525993 13-Nov-13 10:18am    
HI
I am using vb.net. sorry for the confusion.

i call an api that return an array that has 8 element. The elements are type byte. The first two elements represent an index.

example. array(0) = &H10 and array(1) = &HD

so the index would be &100D
How exactly can I combine the two bytes to represent one number ?

I tried saving the
Array(0) as a short
Array(1) as a short

Result = array(0) + array(1)<<8

--------------------- below is the code i try --------------
Dim index1, index2, result As Byte ' represent the first 2 elements

index2 = &H10
index1 = &HD

Dim index1_s, index2_s As Short
index1_s = index1
index2_s = index2

result = index1_s + (index2_s << 8) 'i get a overflow error
Member 8525993 13-Nov-13 10:45am    
The problem i had with the above code is result was a byte instead of integer.

Thanks Sergey for the knowledge. if you think there a better way let me know. appreciate it ..


------- my working solution is below ------

Dim index1, index2 As Byte ' represent the first 2 elements
Dim result as integer
index2 = &H10
index1 = &HD

Dim index1_s, index2_s As Short
index1_s = index1
index2_s = index2

result = index1_s + (index2_s << 8)
Sergey Alexandrovich Kryukov 13-Nov-13 11:39am    
What do you mean to "combine"? It could be '+' operator, or '|'. Do you know bitwise arithmetic?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900