Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-08-2009
Member
 
Join Date: Apr 2008
Posts: 29
help: Convert hex to int

Hi,

How do I convert hex to int? hexadecimal number to decimal need help.

Thanks,
Reply With Quote
  #2  
Old 04-08-2009
Member
 
Join Date: May 2008
Posts: 41
Re: help: Convert hex to int

Convert from Hex to Int

VBScript

Code:
<% 
    Response.Write "<hr>Long -> Hex, VBScript<p>" 
    DecVal = 16777215 
    Response.Write Hex(DecVal) 
 
    Response.Write "<hr>Hex -> Long, VBScript<p>" 
    HexVal = "FFFFFF" 
    Response.Write CLng("&H" & HexVal) 
%>
JScript

Code:
<script language=jscript runat=server> 
 
    Response.Write("<hr>Dec -> Hex, JScript<p>"); 
    DecVal = 16777215; 
    Response.Write(DecVal.toString(16).toUpperCase()); 
 
    Response.Write("<hr>Hex -> Dec, JScript<p>"); 
    HexVal = "FFFFFF"; 
    Response.Write(parseInt("0x"+HexVal)); 
 
</script>
Reply With Quote
  #3  
Old 04-08-2009
Member
 
Join Date: May 2008
Posts: 27
Re: help: Convert hex to int

Convert Hexadecimal number into Integer

The following program takes an integer type data at the console (using the keyboard) and converts the hexadecimal data into an integer type data using valueOf() method. It take a number as a string. It converts the number into an integer data using the Integer.valueOf() method.

Integer.valueOf() :The Integer.valueOf() returns an integer object containing the value extracted from the specified String. The result is an integer that represents the integer value specified by the string.

Code:
import java.io.*;
import java.lang.*;

public class  HexaToInteger{
  public static void main(String[] args) throws IOException{
    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the hexadecimal value:!");
    String s = read.readLine();
    int i = Integer.valueOf(s, 16).intValue();
    System.out.println("Integer:=" + i);
  }
}
Reply With Quote
  #4  
Old 04-08-2009
Member
 
Join Date: May 2008
Posts: 44
Re: help: Convert hex to int

Convert a "Hex String" to an Integer

Here is the code to a small console application.

Code:
#include "stdafx.h"

#include <tchar.h>

#include <malloc.h>


int _httoi(const TCHAR *value)
{
  struct CHexMap
  {
    TCHAR chr;
    int value;
  };
  const int HexMapL = 16;
  CHexMap HexMap[HexMapL] =
  {
    {'0', 0}, {'1', 1},
    {'2', 2}, {'3', 3},
    {'4', 4}, {'5', 5},
    {'6', 6}, {'7', 7},
    {'8', 8}, {'9', 9},
    {'A', 10}, {'B', 11},
    {'C', 12}, {'D', 13},
    {'E', 14}, {'F', 15}
  };
  TCHAR *mstr = _tcsupr(_tcsdup(value));
  TCHAR *s = mstr;
  int result = 0;
  if (*s == '0' && *(s + 1) == 'X') s += 2;
  bool firsttime = true;
  while (*s != '\0')
  {
    bool found = false;
    for (int i = 0; i < HexMapL; i++)
    {
      if (*s == HexMap[i].chr)
      {
        if (!firsttime) result <<= 4;
        result |= HexMap[i].value;
        found = true;
        break;
      }
    }
    if (!found) break;
    s++;
    firsttime = false;
  }
  free(mstr);
  return result;
}

int main(int argc, char* argv[])
{
  TCHAR *test[4] = {_T("0xFFFF"), _T("0xabcd"), _T("ffff"), _T("ABCD")};
  for (int i = 0; i < 4; i++)
    _tprintf(_T("Hex String: %s is int: %d\n\r"), test[i], _httoi(test[i]));
  return 0;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "help: Convert hex to int"
Thread Thread Starter Forum Replies Last Post
Convert CDR to PSD neonxgenesis Windows Software 3 30-10-2009 01:42 PM
Convert dvd-ram to dvd Murena Portable Devices 3 27-05-2009 05:43 PM
How to convert pub to pdf Gretel Windows Software 2 19-05-2009 11:33 PM
Convert rtf to pdf Ebenezer Windows Software 3 14-05-2009 12:10 PM
Convert video to iPod, Convert iTunes video, Convert DivX to MP4 tommyhills MediaCenter 1 26-07-2008 02:30 AM


All times are GMT +5.5. The time now is 04:04 PM.