[已解决]如何在C语言中的字符串和DWORD(无符号长)之间转换? [英] [SOLVED] How to convert between string and DWORD (unsigned long) in C?

查看:184
本文介绍了[已解决]如何在C语言中的字符串和DWORD(无符号长)之间转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

我正在尝试制作一个从用户接收输入字符串的程序(使用gets()函数),并希望以大端顺序将该字符串转换为DWORD(无符号长整数),以便以后我可以以十六进制格式显示值反之亦然.该程序如下所示:

Hai,

I''m trying to make a program that receive input string from user (using gets() function) and want to convert that string into DWORD (unsigned long) in big-endian order so that later i can display the value in hexadecimal format and vice versa. The program is look like this:

#include <stdio.h>
#include <stdlib.h>

#define MAX_STRING_LEN 50

typedef unsigned long DWORD;

/ * How to create this functions? */
void StringToDword(char *inputString, DWORD *outDw);
void DwordToHexString(DWORD inputDw, char *hexString);
void DwordToString(DWORD inputDw, char *outString);

int main(void)
{
  char inputString[MAX_STRING_LEN];
  char hexString[500];
  char strVal[MAX_STRING_LEN];
  DWORD dwVal = 0;
  
  printf("Input some string: ");    // here user can input any string like
                                    // "some string".
  gets(inputString);
  
  StringToDword(inputString, &dwVal);
  printf("\nYour DWORD value    : %ul\n", dwVal);   // will output something like
                                                    // 173849l
  DwordToHexString(dwVal, hexString);
  printf("\nYour DWORD in hex   : %s\n", hexString); // will output something like
                                                     // 23 6D 12 5A
  DwordToString(dwVal, strVal);
  printf("\nYour original string: %s\n", strVal); // will output the original
                                                  // string
  return 0;
}



在此先谢谢您.



Thanks in advance.

推荐答案

您可以使用 sscanf [ ^ ]来转换使用u类型说明符将字符串转换为无符号int.

在另一个方向上, printf [
You can uses sscanf[^] to convert a string to a unsigned int using the u type specifier.

In the other direction, printf[^] could do.

You can also look at functions like atoi, itoa, ultoa, strtoul etc...


经过几次尝试和错误,最终我找到了以下解决方案我的问题,这是我的最终代码:

After a few trial and error, finally i found this solution for my problem, here is my final code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define NEWLINE printf("\n");
#define MAX_STR_LEN 100
#define BYTES_PER_DWORD 4

typedef unsigned char BYTE;
typedef unsigned int UINT;
typedef unsigned long DWORD;

void StringToBytesArray(const char *strIn, BYTE *bOut, const UINT bLen);
UINT BytesArrayToDwords(const BYTE *bIn, DWORD *dwOut, const UINT bCount,
    const UINT dwCount);
void DwordsToBytesArray(const DWORD *dwIn, BYTE *bOut, const UINT dwCount,
    const UINT bCount);
void BytesArrayToString(const BYTE *bIn, char *strOut, const UINT bLen);

int main(int argc, char *argv[])
{
  char strBuff[MAX_STR_LEN];
  UINT iStrLen = 0;
  printf("Input string        : ");
  gets(strBuff);
  iStrLen = (UINT)strlen(strBuff);
  
  char strIn[iStrLen];
  strcpy(strIn, strBuff);
  printf("String length is    : %u chars\n", iStrLen);
  
  BYTE bData[iStrLen];
  StringToBytesArray(strIn, bData, iStrLen);
  printf("Bytes string in hex : ");
  UINT i = 0;
  for(i = 0; i < iStrLen; i++){
    printf("%0.2X ", bData[i]);
  }
  NEWLINE
  
  UINT iByteCount = sizeof(bData);
  printf("Number of bytes     : %d bytes\n", iByteCount);
  UINT iDwordCount = iByteCount / BYTES_PER_DWORD;
  if(iByteCount % BYTES_PER_DWORD > 0){
    iDwordCount++;
  }
  
  DWORD dwData[iDwordCount];
  UINT iByteProcessed = 0;
  iByteProcessed = BytesArrayToDwords(bData, dwData, iByteCount, iDwordCount);
  printf("Number of DWORDs    : %d DWORDs\n", iDwordCount);
  printf("DWORD value         : ");
  for(i = 0; i < iDwordCount; i++){
    printf("%0.8X ", dwData[i]);
  }
  NEWLINE
  
  BYTE bOut[iByteProcessed];
  DwordsToBytesArray(dwData, bOut, iDwordCount, iByteProcessed);
  printf("Bytes string in hex : ");
  for(i = 0; i < iByteProcessed; i++){
    printf("%0.2X ", bOut[i]);
  }
  NEWLINE
  
  char strOut[iStrLen + 1];
  BytesArrayToString(bOut, strOut, iStrLen);
  printf("Original string is  : %s\n", strOut);
  
  system("PAUSE");
  return 0;
}

void StringToBytesArray(const char *strIn, BYTE *bOut, const UINT bLen)
{
  UINT i = 0;
  for(i = 0; i < bLen; i++){
    bOut[i] = strIn[i];
  }
}

UINT BytesArrayToDwords(const BYTE *bIn, DWORD *dwOut, const UINT bCount,
    const UINT dwCount)
{
  bool eob = false;
  DWORD dwBuff = 0;
  UINT i = 0, j = 0, k = 0;
  
  while(!eob){
    if(j == 0 || ((j % BYTES_PER_DWORD == 0) && j < bCount)){
      dwBuff = bIn[j++];
      if(j < bCount){
        dwBuff = dwBuff << 8;
      }
      else if(j == bCount){
        dwBuff = dwBuff << 24;
      }
    }
    else if(j > 0 && j < bCount){
      dwBuff |= bIn[j++];
      if(j % BYTES_PER_DWORD > 0){
        if(j == bCount){
          if(j % BYTES_PER_DWORD == 2){
            dwBuff = dwBuff << 16;
          }
          else if(j % BYTES_PER_DWORD == 3){
            dwBuff = dwBuff << 8;
          }
        }
        else{
          dwBuff = dwBuff << 8;
        }
      }
    }
    
    if(j != 0 && ((j % BYTES_PER_DWORD == 0) || j == bCount)){
      dwOut[i++] = dwBuff;
    }
    
    if(j >= bCount){
      eob = true;
    }
  }
  return j;
}

void DwordsToBytesArray(const DWORD *dwIn, BYTE *bOut, const UINT dwCount,
    const UINT bCount){
  UINT i = 0, j = 0, k = 0, l = 0;
  BYTE bBuff[BYTES_PER_DWORD];
  
  for(i = 0; i < dwCount; i++){
    *(DWORD*)bBuff = dwIn[i];
    for(k = BYTES_PER_DWORD; k > 0; k--){
      if(j < bCount){
        bOut[j++] = bBuff[k - 1];
      }
      else{
        break;
      }
    }
  }
}

void BytesArrayToString(const BYTE *bIn, char *strOut, const UINT bLen)
{
  UINT i = 0;
  for(i = 0; i < bLen; i++){
    strOut[i] = bIn[i];
  }
  NEWLINE
  strOut[bLen] = ''\0'';
}


这篇关于[已解决]如何在C语言中的字符串和DWORD(无符号长)之间转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆